What is the Visual Basic code for setting a field value from a secondary data source? - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

What is the Visual Basic code for setting a field value from a secondary data source?

Last post 09-06-2010 11:24 PM by Jimmy. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 09-06-2010 02:03 AM

    What is the Visual Basic code for setting a field value from a secondary data source?

    Hi,

    I understand that rules can be set to populate fields using data retrieved from a secondary data source. I'm just wondering if this can be done via Visual Basic code?

    Reason is because I kind of have alot of fields and using code might be faster to populate my form?

    The replaceself function can be used to replace nodes but is there code to replace fields themselves?

     

    Thanks!

  • 09-06-2010 04:55 AM In reply to

    Re: What is the Visual Basic code for setting a field value from a secondary data source?

    To add on, what I'm trying to do is something like:

    Me.CreateNavigator().SelectSingleNode("/my:myFields/my:Date_Letter", Me.NamespaceManager).SetValue("abcd")

    Except that SetValue is not applicable for xpath input. It only takes string values. I hope to be able to replace "abcd" with a xpath to my secondary data.

     

    Thanks!

  • 09-06-2010 06:07 AM In reply to

    Re: What is the Visual Basic code for setting a field value from a secondary data source?

    Me.CreateNavigator().SelectSingleNode("/my:myFields/my:Date_Letter", Me.NamespaceManager).SetValue(Me.DataSources("DataSourceName").CreateNavigator().SelectSingleNode("xpath", Me.NamespaceManager).Value)

    To put it simply. Naturally, the ideal would be to use variables to store the two namespace managers so you don't need to keep creating them.

    But what makes you think that this would be faster than using rules?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 09-06-2010 06:38 PM In reply to

    Re: What is the Visual Basic code for setting a field value from a secondary data source?

    Hi Jimmy,

    Thanks for your help again. When you say variabled, do you mean:

    Dim oItem As XPathNavigator Dim oSecData As String

    oItem = Me.CreateNavigator().SelectSingleNode("/my:myFields/my:Date_Letter", Me.NamespaceManager)

    oSecData = Me.DataSources("Get_Existing_Engagement_Code_info").CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/ns1:GetColumnsXMLStrQueryResponse/ns1:GetColumnsXMLStrQueryResult/Rows/row/BudgetCost", Me.NamespaceManager).Value

    oItem.SetValue(oSecData)

    -------------------------------------

    And then reusing the variables oItem and oSecData for the other fields? But during each declaration of variable values like "oItem = Me.CreateNavigator().SelectSingleNode("/my:myFields/my:Date_Letter", Me.NamespaceManager)", won't the Namespacemanagers be created as well? Unless I am coding it inefficiently.

    Actually, I suspect it takes the same amount of time as compared to using rules. Just that it is faster to set all your values using code rather than rules (when you have many fields). Saves time for the form creator.

  • 09-06-2010 07:15 PM In reply to

    Re: What is the Visual Basic code for setting a field value from a secondary data source?

    Hi Jimmy,

    I decided to just use rules :) but for knowledge sake, I'll still like to know how I can go about this in code?

    Is it more efficient to use rules instead of code?

     

    Thanks!

  • 09-06-2010 07:25 PM In reply to

    Re: What is the Visual Basic code for setting a field value from a secondary data source?

    Jimmy:
    But what makes you think that this would be faster than using rules?

    I think it was my fault. I say that on a context that he have a lot of field to reinitialize, so instead of use a rules for each fiel.d I suggest to use code to replace the parent node with a empty reference node in this case using code is faster but if you just want to asign a value to a field it's the same.

  • 09-06-2010 07:47 PM In reply to

    Re: What is the Visual Basic code for setting a field value from a secondary data source?

     

    Hi Potemkine,

    Nice to hear from you again. Ok, noted about the rules vs code part. I'm still using your code to refresh this new form though :)

    I was thinking of using code because my form has multiple sections and nested sections and thus using rules can sometimes take quite a lot of mouse clicks. Was just trying to find out if it can be done via code which allows me to copy and paste xpaths which I thought can save abit of form development time.

  • 09-06-2010 11:24 PM In reply to

    Re: What is the Visual Basic code for setting a field value from a secondary data source?

    What I meant was something like this:

    Dim mainNavigator As XPathNavigator = Me.CreateNavigator()

    Dim codeInfoNavigator As XPathNavigator = Me.DataSources("Get_Existing_Engagement_Code_info").CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/ns1:GetColumnsXMLStrQueryResponse/ns1:GetColumnsXMLStrQueryResult/Rows/row")

    once you have that, the code is more concise and efficient.

    mainNavigator.SelectSingleNode("/my:myFields/my:Date_Letter", Me.NamespaceManager).SetValue(codeInfoNavigator.SelectSingleNode("BudgetCost").Value)

     

    I think there's probably very little difference in speed versus setting a field value in code vs. in rules, but at Qdabra it's our policy to use rules rather than code whenever possible because it is more straightforward, makes the form easier to maintain, and doesn't require coding experience to make modifications.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
Page 1 of 1 (8 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.