OnAfterChange process 2 times, Why? - InfoPath Dev
in

InfoPath Dev

OnAfterChange process 2 times, Why?

Last post 01-02-2007 09:03 AM by jweir. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-02-2007 07:57 AM

    • jweir
    • Not Ranked
    • Joined on 12-14-2006
    • USA
    • Posts 15

    OnAfterChange process 2 times, Why?

    Hello all

    I have written some code for a form that will do some updates to a repeating table. The code is written in VBscript.

    What happens is the when I add a Name in the field for POC and then ‘OnAfterChange#%92 would like that name updated in Contributor field also. This all works now. Problem I am having now is that went I add a second POC name, Contributor table has three records in the table.

    Example


    POC Joe Jones

    Contributor Joe Jones

    Next entry
    POC Fred Smith
    Contributor Joe Jones
    Fred Smith
    Fred Smith

    Here is the Code I am using

    sub msoxd_my_POC_Name_SingleName_OnAfterChange(eventObj)

    Dim ObjPOCName
    Dim iloop
    iloop = 0
    display("this is Iloop " & Cstr(iloop))

    ' Write code here to restore the global state.

    If eventObj.IsUndoRedo Then
    ' An undo or redo operation has occurred and the DOM is read-only.
    Exit Sub
    End If

    Set ObjPOCName = XDocument.DOM.selectSingleNode("/my:Project/my:POC/my:Name/my:SingleName")
    ' A field change has occurred and the DOM is writable. Write code here to respond to the changes.
    if iloop = 0 then
    AddtoTable(ObjPOCName.text)
    iloop = 1
    end if

    'Display("POC Name is " & ObjPOCName.text)
    End Sub

    Function Display(ByVal data)

    XDocument.UI.Alert(data)

    End Function
    '=======
    ' The following function handler is created by Microsoft Office InfoPath.
    ' Do not modify the name of the function, or the name and number of arguments.
    ' This function is associated with the following field or group (XPath): /my:Project/my:Contributors/my:Contributor/my:Name/my:SingleName
    ' Note: Information in this comment is not updated after the function handler is created.
    '=======
    Sub msoxd_my_Contributor_Name_SingleName_OnAfterChange(eventObj)

    ' Write code here to restore the global state.
    Dim ObjContName
    Dim oSampleRow
    Dim iSelectedIndex

    If eventObj.IsUndoRedo Then
    ' An undo or redo operation has occurred and the DOM is read-only.
    Exit Sub
    End If

    ' A field change has occurred and the DOM is writable. Write code here to respond to the changes.
    'Set ObjContName = XDocument.DOM.selectSingleNode("/my:Project/my:Contributors/my:Contributor") '/my:Name/my:SingleName")
    'Display("Inside COntributor Name Node Contributor Name is " & ObjContName.text)

    'iSelectedIndex = XDocument.DOM.selectNodes("/my:Project/my:Contributors/my:Contributor").length 'GetSelectedPartsIndex("/my:Project/my:Contributors/my:Contributor") '/my:Name/my:SingleName")
    'Display (iSelectedIndex)
    End Sub


    Sub AddtoTable(strValue)

    Dim iSelectedIndex
    Dim objSelectedNode
    dim objNode
    dim objNewRow
    dim objSimpleRow


    Set objNode = XDocument.DOM.selectNodes("/my:Project/my:Contributors/my:Contributor" )
    Set objSimpleRow = objNode.nextNode()
    Set objNewRow = objSimpleRow.cloneNode(true)

    objNewRow.selectSingleNode("/my:Name/my:SingleName").text = strValue
    'iSelectedIndex = XDocument.DOM.selectNodes("/my:Project/my:Contributors/my:Contributor").length 'GetSelectedPartsIndex("/my:Project/my:Contributors/my:Contributor") '/my:Name/my:SingleName")
    XDocument.DOM.selectSingleNode("/my:Project/my:Contributors").appendChild(objNewRow)
    'Display("The recond Number is " & Cstr(iSelectedIndex) & " The Record is " & XDocument.DOM.selectSingleNode( "/my:Project/my:Contributors/my:Contributor[" & Cstr(iSelectedIndex) & "]/my:Name/my:SingleName" ).text)

    end sub


    Thanks in advance for all your help
    Jim
    InfoPath 2003 SP2
    Office 2003 SP2
    SharePoint 2003
    VS 2003

    Jim Weir
  • 01-02-2007 08:07 AM In reply to

    Re: OnAfterChange process 2 times, Why?

    Hi Jim,

    The OnAfterChange fires once for deleting the old value, and then again for inserting the new value. To look at only the times when it is inserting the new value (which will likely be all you will ever need), try modifying this section of your code:

    If eventObj.IsUndoRedo Or eventObj.Operation Not Equal To "Insert" Then
    ' An undo or redo operation has occurred and the DOM is read-only.
    Exit Sub
    End If


    Pardon if my VB syntax is incorrect - I normally only do C# or JScript, and this is how you'd do it there:

    if( e.Operation != "Insert" )
    return;
    Matt Faus / Microsoft InfoPath MVP
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 01-02-2007 09:03 AM In reply to

    • jweir
    • Not Ranked
    • Joined on 12-14-2006
    • USA
    • Posts 15

    Re: OnAfterChange process 2 times, Why?

    Thanks Matt,

    that worked...

    Thanks for all your help
    InfoPath 2003 SP2
    Office 2003 SP2
    SharePoint 2003
    VS 2003

    Jim Weir
Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.