Using Set command in vbscript - InfoPath Dev
in

InfoPath Dev

Using Set command in vbscript

Last post 08-25-2006 07:41 AM by hensleydl. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 08-24-2006 09:37 AM

    Using Set command in vbscript

    I am new to using infopath. I am looking at converting existing online forms created in Accelio into infopath. My question concerns the script (object) code. I noticed that using vbscript with infopath, you have to use Set commands (i.e. Set txtUid = XDocument.DOM.selectSingleNode("//my:txtUID"). It seems that you have to issue this command for each field. This can get quite cumbersome when you have a form with many fields. Also I noticed that when you have a lot of forms, you need to use the "SET" statement multiple times in the script code for it to work. Any suggestions as to how to work successfully with the "SET" command in vbscript? Any suggestions would be greatly appreciated.
  • 08-24-2006 11:35 AM In reply to

    Re: Using Set command in vbscript

    Hey,

    I did the following on a training form so that the user could click a button and the data would be populated in the field or removed from the field. Not sure if it will help you...

    All code is VBScript...

    Sub ClearDataFields_OnClick()
    'Write your code here
    PopDefaults xDocument.view.name, True
    End Sub

    Sub PopDataFields_OnClick()
    PopDefaults xDocument.view.name, False
    End sub

    Sub PopDefaults(ByVal sViewName, bRemoveData)
    'purpose to populate default values based on which view you
    ' are in. If bRemoveData is true, clear fields
    Dim sTxt

    Select Case UCase(sViewName)
    Case "FORMATFIELDS"
    PopFieldValue "//my:ChainID", "1001-11-11", bRemoveData
    PopFieldValue "//my:CostCenter", "01255", bRemoveData
    PopFieldValue "//my:DateControl", "2006-06-01", bRemoveData
    PopFieldValue "//my:DIVNum", "3015", bRemoveData
    PopFieldValue "//my:DeptDivNo", "3001", bRemoveData
    PopFieldValue "//my:EMPID", "101256", bRemoveData
    PopFieldValue "//my:EIN", "11-5533667", bRemoveData
    PopFieldValue "//my:SAP_GLCode", "1111-12345678-22668", bRemoveData
    PopFieldValue "//my:PhoneNumber", "(336) 741-5000", bRemoveData
    PopFieldValue "//my:EmpName", "Jane Q Doe", bRemoveData
    PopFieldValue "//my:SISNo", "100111", bRemoveData
    PopFieldValue "//my:SSN", "333-22-3333", bRemoveData
    PopFieldValue "//my:TerritoryNo", "400112", bRemoveData
    Case "REPEAT"
    If bRemoveData = False Then
    AddRepeatingSection 2, "QTYS"
    AddRepeatingSection 2, "PLANNED_MEETS"
    AddRepeatingSection 2, "EMPLOYMENT_HISTORY"
    'AddRepeatingSection 3, "POSITIONS_HELD"
    Else
    RemoveRepeatingSection 1, "QTYS"
    RemoveRepeatingSection 1, "PLANNED_MEETS"
    RemoveRepeatingSection 1, "EMPLOYMENT_HISTORY"
    End If
    case Else
    End Select
    End Sub

    Sub PopFieldValue(sFieldName, sValue, bRemoveData)
    'to populate the value in the field that is off the root node
    Dim oField

    Set oField = XDocument.DOM.SelectSingleNode(sFieldName)

    'must remove xsi:nil attribute if necessary
    'required for dates and numeric values


    'add the value or remove it
    If bRemoveData = False then
    If oField.getAttribute("xsi:nil") Then
    oField.removeAttribute("xsi:nil")
    End If
    oField.Text = sValue
    Else
    If sFieldName <> "//my:DateControl" Then
    oField.Text = ""
    Else
    Dim xmlNil
    Set xmlNil = oField.ownerDocument.createNode(2, "xsi:nil", "http://www.w3.org/2001/XMLSchema-instance")
    xmlNil.text = "true"
    oField.Text = ""
    oField.setAttributeNode(xmlNil)
    End if

    end If

    End Sub

    Function AddRepeatingSection(num, elmName)
    'Adds specified number of elements to repeating table or section.
    'Parameters:
    'num is number of sections/rows to add
    ' elmName is name of repeating area as shown in datasource task pane (without the "my:" prefix).

    Dim secPath, oSecGrp, oSecItem, oClone

    secPath = "//my:" & elmName

    Set oSecGrp = XDocument.DOM.selectNodes(secPath)

    If oSecGrp.length > 0 Then

    Set oSecItem = oSecGrp.Item(0)
    i = 0

    Do Until i = num
    'Clone the first row/section.
    Set oClone = oSecItem.cloneNode(True)
    'Append section at end of nodeset.
    oSecItem.parentNode.insertBefore oClone, oSecGrp.Item(oSecGrp.length + i + 1)
    i = i + 1
    Loop

    End If

    End Function

    Function RemoveRepeatingSection(num, elmName)
    'Removes elements to repeating table or section leaving 1 default section or table
    'Parameters:
    ' elmName is name of repeating area as shown in datasource task pane (without the "my:" prefix).

    Dim secPath, oSecGrp, oSecItem, oClone

    secPath = "//my:" & elmName

    Set oSecGrp = XDocument.DOM.selectNodes(secPath)

    If oSecGrp.length > 0 Then

    Set oSecItem = oSecGrp.Item(0)
    'i = 0
    i = oSecGrp.length

    Do Until i = num
    'Remove section at end of nodeset.
    oSecItem.parentNode.removeChild oSecGrp.Item(oSecGrp.length - i + 1)
    i = i - 1

    Loop

    End If

    End Function
  • 08-24-2006 11:44 AM In reply to

    Re: Using Set command in vbscript

    This is a copy of the code that I used for this test form. This form works, I'm just wondering if there's a better way instead of all those "SET" commands

    Sub XDocument_OnLoad(eventObj)
    ' Write your code here
    Dim RecCnt2
    Dim RecNbr2

    '==========
    ' Link Script Variables to Fields in Forms
    '==========
    Set txtUId = XDocument.DOM.selectSingleNode("//my:txtUId")
    Set txtCnn = XDocument.DOM.selectSingleNode("//my:txtCnn")
    Set txtAward_To = XDocument.DOM.selectSingleNode("//my:txtAward_To")
    Set txtOrg = XDocument.DOM.selectSingleNode("//my:txtOrg")
    Set txtFor = XDocument.DOM.selectSingleNode("//my:txtFor")
    Set txtSubmit_By = XDocument.DOM.selectSingleNode("//my:txtSubmit_By")
    Set txtOrg_By = XDocument.DOM.selectSingleNode("//my:txtOrg_By")
    Set txtTele = XDocument.DOM.selectSingleNode("//my:txtTele")
    Set dtmDate_Sub = XDocument.DOM.selectSingleNode("//my:dtmDate_Sub")
    Set txtRecNbr = XDocument.DOM.selectSingleNode("//my:txtRecNbr")
    Set txtRecCnt = XDocument.DOM.selectSingleNode("//my:txtRecCnt")
    Set lstSearch = XDocument.DOM.selectSingleNode("//my:lstSearch")
    Set lstCntPinTries = XDocument.DOM.selectSingleNode("//my:lstCntPinTries")
    Set txtSearch = XDocument.DOM.selectSingleNode("//my:txtSearch")

    TrimUserId()
    TrimCnnStr()
    OpenDbConnection()
    InitializeForm()
    DetermineRecNbrAndCount RecNbr2, RecCnt2


    This forms accesses data from a SQL database. The SQL side works fine with no problems.
  • 08-25-2006 07:41 AM In reply to

    Re: Using Set command in vbscript

    mbenbow - thanks for your reply, I see where it helps. I have another question. Do you have to execute PopFieldValue any time you need to change the value from the script? We're linking the form from an SQL table so we do a lot of updating of fields through the script (object code) This would not be much of a problem on small forms but it could be a problem when you have to load values to the script from the form and from the SQL table. Any thoughts would be appreciated.
Page 1 of 1 (4 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.