VB Script accessing Secondary Data XML Sources - InfoPath Dev
in

InfoPath Dev

VB Script accessing Secondary Data XML Sources

Last post 11-10-2008 01:11 PM by Jeff3425. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 11-06-2008 11:09 AM

    VB Script accessing Secondary Data XML Sources

    Good afternoon,

    I am developing a form where I want to set a fields value to the default selection only if the form is new.  When the form is opened after it has been saved, I want that same field to display the selected value instead of the default value.  I have been given the code for the "isNew" function in vbscript during the onload event, but I am having some trouble creating the default value through VBScript.  Essentially, I have Field = "SecondaryXML Field where Infopath Field1 = XML data1 and Infopath Field 2 = XML Data 2"  The way this is represented in the xpath fucntion of the Infopath Datasource is xdXDocument:GetDOM("SecondaryXMLDataSource")/OfferingMaster/Server/Function/ServerTools/StdTools/XMLField[xdXDocument:get-DOM()/my:myFields/my:InfoPathField1 = ../../../XMLField1 and xdXDocument:get-DOM()/my:myFields/my:InfoPathField2= ../../../../XMLField2.

    My question is how would I convert the xpath function from InfoPath into VBscript?  Thank you.

  • 11-06-2008 05:45 PM In reply to

    Re: VB Script accessing Secondary Data XML Sources

    I'm not too experienced with VBScript and there might be a better way, but something like the following should work:

    Dim Field1Value
    Dim Field2Value
    Dim SecondaryDataSource
    Dim MatchingXMLField
    Dim DestinationField

    Field1Value = XDocument.DOM.selectSingleNode("/my:myFields/my:InfoPathField1").text
    Field2Value = XDocument.DOM.selectSingleNode("/my:myFields/my:InfoPathField2").text

    Set SecondaryDataSource = XDocument.DataObjects("SecondaryXMLDataSource").DOM
    Set MatchingXMLField = SecondaryDataSource.selectSingleNode("/OfferingMaster/Server/Function/ServerTools/StdTools/XMLField['" & Field1Value & "' = ../../../XMLField1 and '" & Field2Value & "'= ../../../../XMLField2]")
    Set DestinationField = XDocument.DOM.selectSingleNode(XPath of destination node)

    DestinationField.text = MatchingXMLField.text

    I'm using string concatenation here to include the field values from the main DOM into the XPath statement, and since it's hard to tell, the '" + Field1Value + "' part consists of single quote, double quote & Field1Value & double quote, single quote.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-07-2008 08:58 AM In reply to

    Re: VB Script accessing Secondary Data XML Sources

    Thank you Jimmy,

    I have set this up and all works fine until it gets to the last line: DestinationField.text = MatchingXMLField.text

    When doing this, I generate an error saying "Object Required for MatchingXMLField.text" I have tried various methods such as dropping the .text with no luck.  So essentially the destination field won't associate with the Matching XML field.  Thoughts?

  • 11-07-2008 05:15 PM In reply to

    Re: VB Script accessing Secondary Data XML Sources

    Hey Jeff,

     The most likely cause of that error is that for whatever reason, the XPath in the line that starts with Set MatchingXMLField = is not finding any node.  Please take a careful look at that line and make sure everything is sound, and that you didn't forget the single quotes.

    One thing you can do to begin troubleshooting this is to inspect the XPath being used there. If you add a new node called "XPath" beneath myFields in your form and show it in a text box, you can use it to observe the XPath being used, by changing the end of the code to something like this:

    Dim XPathDisplayNode
    Dim XPathToUse
    Set XPathDisplayNode = XDocument.DOM.selectSingleNode("/my:myFields/my:XPath")
    XPathToUse = "/OfferingMaster/Server/Function/ServerTools/StdTools/XMLField['" & Field1Value & "' = ../../../XMLField1 and '" & Field2Value & "'= ../../../../XMLField2]"
    Set MatchingXMLField = SecondaryDataSource.selectSingleNode(XPathToUse)
    Set DestinationField = XDocument.DOM.selectSingleNode(XPath of destination node)

    If MatchingXMLField Is Nothing Then  'If no matching node was found, then display the XPath in the XPath textbox
        XPathDisplayNode.text = XPathToUse
    Else   'If it was a success, set the DestinationField value and clear the XPath Display textbox
        DestinationField.text = MatchingXMLField.text
        XPathDisplayNode.text = ""
    End If

    This should enable you to troubleshoot the issue a bit more.

    I noticed that in the XPath you wrote in the initial post, XMLField1 has three ../s before it and XMLField2 has four.  It that correct for the hierarchy of your data source?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-10-2008 01:11 PM In reply to

    Re: VB Script accessing Secondary Data XML Sources

    Good afternoon,

     Thank you very much for the testing code - I had done some testing previously to see if I could at least write, but this allowed me to troubleshoot further.  The main error of DestinationField.text = MatchingXMLField.text was due to the fact that they were based off of ['" & Field1Value & "' = ../../../XMLField1 and '" & Field2Value & "'= ../../../../XMLField2]" combined with the OnLoad function I was using.

    This kept failing because Field1Value and Field2Value had default values of "Please Select..." which caused the MatchingXMLField.text to error out because object wasn't supported with the default text values of "Please Select..."

    To avoid this, I changed the trigger from onload to an onclick event that runs the code when a button is clicked.  Everything works like a charm now.  I am also sure I could have removed the default text values and used the onload event as well.  Thanks again for the assistance Jimmy.

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