Transferring data between two forms - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Transferring data between two forms

Last post 09-19-2011 02:16 PM by gdgonzal. 23 replies.
Page 1 of 2 (24 items) 1 2 Next >
Sort Posts: Previous Next
  • 08-07-2009 08:39 AM

    Transferring data between two forms

    Hi all-

    I am using two forms. I would like to use data from a text box I have on form #1 and use that same data in a text box on form #2.

    Currently, (on form #1) the user enters their name in a text box. I have a button that uses rules that queries an Access database for that name and if the name is valid, it then opens form #2.

     It would be great if I can set the value of the text box on form #2 ,with the value from the textbox on form #1, through rules.

     Any help or suggestions would be greatly appreciated.

    Thanks in advance.

    RhysAZ

     

  • 08-07-2009 10:04 AM In reply to

    • Daniels
    • Not Ranked
      Male
    • Joined on 07-28-2009
    • Melbourne, Australia
    • Posts 18

    Re: Transferring data between two forms

    I am not sure exactly how this can be done using rules, but it can be done using code in the "form #1" template: JScript example:

     

    //Set the path to the "form #2" template:

    var strNewFormPath = "http://path-to-infopath-form-template";

    //Open the new form:
    var objNewForm = Application.XDocuments.NewFromSolution(strNewFormPath);

    //Set the name spaces for the new form:
    objNewForm.DOM.setProperty("SelectionNamespaces", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-01-29T23:32:25" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"');

    //Load the XML Document of "form 2":
    var objExternalData = new ActiveXObject("MSXML2.DomDocument.5.0");
    objExternalData.validateOnParse = false;
    objExternalData.loadXML( objNewForm.DOM.xml );

    //Set the name space for the XML data object:
    objExternalData.setProperty("SelectionNamespaces", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-01-29T23:32:25" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:ns1="http://schemas.microsoft.com/sharepoint/soap/directory/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"');

    //Update the value of fields in the main or secondary data source of "form #2" with values from "form#1":
    objNewForm.DOM.selectSingleNode("/my:myFields/my:Name").text = XDocument.DOM.selectSingleNode("/my:myFields/my:Name").text;
    objNewForm.DOM.selectSingleNode("/my:myFields/my:Description").text = XDocument.DOM.selectSingleNode("/my:myFields/my:Description").text;

    Let me know if this is any help at all.

    Blogs:
    SharePoint Admin & Dev - SharePoint and InfoPath Development

    Web Development & Programming - Php, C#, .NET Framework, SEO Tips

    WebmasterHub.net - Free SEO, Webmaster Resources
  • 08-07-2009 11:47 AM In reply to

    Re: Transferring data between two forms

    Daniels- Thanks for the quick reply. A few questions:

    1. Do I paste this code in the button on form #1 (Edit Form Code)? And if so, does it run before or after the rules?

    2. In the //Set the name spaces for the new form:  &    //Set the name space for the XML data object: sections:Will I need to use what I have for the xmlns?

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:q="http://schemas.microsoft.com/office/infopath/2003/ado/queryFields" xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-08-04T20:26:18" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"')

    3. In //Update the value of fields in the main or secondary data source of "form #2" with values from "form#1" section:

    objNewForm.DOM.selectSingleNode("/my:myFields/my:Name").text = XDocument.DOM.selectSingleNode("/my:myFields/my:Name").text;

        This is the path to the text box on form #2, right?         =         This is the path to the text box on form #1, right?                                              

  • 08-07-2009 05:55 PM In reply to

    • Daniels
    • Not Ranked
      Male
    • Joined on 07-28-2009
    • Melbourne, Australia
    • Posts 18

    Re: Transferring data between two forms

    RhysAZ:
    1. Do I paste this code in the button on form #1 (Edit Form Code)? And if so, does it run before or after the rules?

    There are a few ways you can get the code to run.  The easiest would probably be an event handler function associated with a button on the form, as the function is generated by InfoPath when you select "Edit form code" from the "Button Properties" dialog.  Other options are to add the code to the submit function, which is generated by InfoPath when the form is submitted.  If using "Rules and Custom Code", a combination of rules and the example above can be used to submit both form #1 and #2.

     

    RhysAZ:
    2. In the //Set the name spaces for the new form:  &    //Set the name space for the XML data object: sections:Will I need to use what I have for the xmlns?

    I don't think you will need all of the name spaces from my example. To find the name space for a data source, go to the properties of a field in the data source, then the details tab.  The name space in the details tab should be all that you need.

    You will need to use the name spaces from your data sources.

     

    RhysAZ:
       This is the path to the text box on form #2, right?         =         This is the path to the text box on form #1, right?    

    Yes.  You should just be able to replace the XPath with that of the fields in form1/form2.

    Let me know how is goes.


    Also, an alternative may be to replace the entire XML structure (or part of) including data contained in form #2 with data from form #1, but only if the XML structures form each form match.


    Blogs:
    SharePoint Admin & Dev - SharePoint and InfoPath Development

    Web Development & Programming - Php, C#, .NET Framework, SEO Tips

    WebmasterHub.net - Free SEO, Webmaster Resources
  • 08-07-2009 06:40 PM In reply to

    Re: Transferring data between two forms

    Thanks for the explanations. I'll give it a try and let you know how it goes. Thanks again Daniels.

  • 08-10-2009 10:08 AM In reply to

    Re: Transferring data between two forms

    Hi Daniels- I'm having trouble with the path name to Form2 at the beginning of the code.

    The form is located in my H:\My Projects\Employee Records project\Start Again\Form2.xsn 

    I attempted to get the network path name to it by making a button on the form, putting in a rule  to open a form and I found the path to Form2 (see below)

     file:///\\dtchyb-azpx002\C_CHR_Users\rfhallo\My%20Projects\Employee%20Records%20project\Form2.xsn

     Please tell me the correct way I need to put the path name into the code.

    Thanks,

    RhysAZ

  • 08-11-2009 02:21 AM In reply to

    • Daniels
    • Not Ranked
      Male
    • Joined on 07-28-2009
    • Melbourne, Australia
    • Posts 18

    Re: Transferring data between two forms

    Hi RhysAZ,

    Instead of opening the second form directly using a rule, you can make the button execute an OnClick event handler.  The code executed then creates an AciveX (XML Document) Object which is what loads the second form programmatically.  Try the following:

     1. From the "Button Properties..." dialog, select "Edit Form Code..."
    - If not already created, InfoPath will then generate an OnClick event handler function in the form's code (make sure the form code is set to JScript to run the code above)

     2. Copy the code from above (use the namespaces from the main data source of you form#1 and form#2) into the OnClick function.

     3. Set the path of the form template to "file:///\\dtchyb-azpx002\C_CHR_Users\rfhallo\My%20Projects\Employee%20Records%20project\Form2.xsn"

     Note: As this seems to be on your personal network drive, the form template may not be available to others once in use.  Both templates will need to be stored/published to a location which is accessible to all users of the form.  You may also need to restrict access to read-only for form users so unauthorised changes are less likely.  My apologies for stating the obvious as you are probably already aware of this, but worth a mention in case the current location is not just for development purposes.  Someone else may find this useful in future though hopefully.

    4. Replace the example XPaths with the correct value for the fields in form#1 and form#2, as you mentioned in a previous post.

    Let me know how this goes.

    Thanks

    Daniel

    Blogs:
    SharePoint Admin & Dev - SharePoint and InfoPath Development

    Web Development & Programming - Php, C#, .NET Framework, SEO Tips

    WebmasterHub.net - Free SEO, Webmaster Resources
  • 08-11-2009 12:40 PM In reply to

    Re: Transferring data between two forms

    Hi Daniels-  I created two plain forms with a texbox on each (Form1-Textbox1, Form2-Textbox2).  I did add the button on Form1 (no rules attached). I've selected Edit Form code and pasted in your code above, using 

     'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-08-10T16:14:34" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"');

    as the namespaces (there are no errors using this).

    However, I get the following error when I push the button:

    The following error occurred:

    The following URL, path, or file name is invalid: file:///\dtchyb-azpx002C_CHR_UsersfhalloMy%20ProjectsEmployee%20Records%20projectForm2.xsn
    File:script.js
    Line:29 

    If you'll notice, it deleted a "backslash" in front of dtchyb-azpx002 and dropped the lowercase 'r" from "rfhallo"

    The actual path I put in was:

    var strNewFormPath = file:///\\dtchyb-azpx002\C_CHR_Users\rfhallo\My%20Projects\Employee%20Records%20project\Form2.xsn;

     

    I think this path name is what is hanging me up. Any suggestions?

     

    RhysAZ

     

     

     

     

  • 08-12-2009 02:54 AM In reply to

    • Daniels
    • Not Ranked
      Male
    • Joined on 07-28-2009
    • Melbourne, Australia
    • Posts 18

    Re: Transferring data between two forms

     The backslash here is used to "escape" special characters.  Where the "\\" is near the start of the url was interpreted as a single "\" when processed. 

     You will need to replace each "\" backslash with double "\\", which will then be interpreted as a single backslash when used in the url to the form template.

     Try the following:  file:///\\\\dtchyb-azpx002\\C_CHR_Users\\rfhallo\\My%20Projects\\Employee%20Records%20project\\Form2.xsn

     You may need to adjust the "\\\\" part, but if you get the error message again you will see the the difference that escaping each backslash has on the url.  Let me know how this goes, including the invalid file name if this still isn't working.

     Thanks

     Daniel

    Blogs:
    SharePoint Admin & Dev - SharePoint and InfoPath Development

    Web Development & Programming - Php, C#, .NET Framework, SEO Tips

    WebmasterHub.net - Free SEO, Webmaster Resources
  • 08-12-2009 10:27 AM In reply to

    Re: Transferring data between two forms

    Daniel- It worked!!!!!!!!!! I didn't have to adjust anything. I pasted in the path as you wrote it . I am very happy.

    Thank you very much again for your help!

     

    RhysAZ

  • 08-13-2009 02:03 AM In reply to

    • Daniels
    • Not Ranked
      Male
    • Joined on 07-28-2009
    • Melbourne, Australia
    • Posts 18

    Re: Transferring data between two forms

     Excellent, good to hear!  I have found this to be very useful when applied in different scenarios and hope that you will too.

     Glad that I was able to help.

     Thanks

     Daniel

    Blogs:
    SharePoint Admin & Dev - SharePoint and InfoPath Development

    Web Development & Programming - Php, C#, .NET Framework, SEO Tips

    WebmasterHub.net - Free SEO, Webmaster Resources
  • 08-14-2009 02:25 PM In reply to

    Re: Transferring data between two forms

    Daniel- Hi again. Need your help again about this. I'm using our solution in a project I'm doing. I'm using a Access 2003 database as my Main source in both forms. I'm running into an error at the end of the code. it's not setting the value on "form2" textbox 2 with the value from "form1" textbox 1. I think it's the way I'm writing the paths to both text boxes is causing the error.

    The following error occurred:

    'DOM.selectSingleNode(...)' is null or not an object
    File:script.js
    Line:46

     The first form (we'll call it form1) has queryFields and dataFields sections. In the dataFields section, there is a repeating table called d:qrytblUsers, with 8 fields in the table. There are no other repeating tables. I have added a textbox to the MyFields group, called SetUserName (it displays as my:SetUserName in the data source window).

    In the second form (we'll call it form2) has queryFields and dataFields sections. In the dataFields section, there is a repeating table called d:tblMain, along with 8 other repeating tables connected to d:tblMain. I have added a textbox to the MyFields group, called SetName (it displays as my:SetName in the data source window).

    The code I put in at the end of the code is this:

    objNewForm.DOM.selectSingleNode("/my:myFields/my:dataFields/my:SetName").text = XDocument.DOM.selectSingleNode("/my:myFields/my:dataFields/my:SetUserName").text;

    I have tried different variations of the code (using "dfs:" instead of "my:", putting in the table names (d:qrytblUsers and d:tblMain) after "/my:dataFields", but I still keep getting that same error code.

    If you could please help me get the correct code for this I would appreciate it. BTW, the rest to the code works fine. I can open "form2" using the button, but it just errors out when trying to set the value of text box 2.

    Thanks Daniel.

    RhysAZ 

     

  • 08-16-2009 06:52 AM In reply to

    • Daniels
    • Not Ranked
      Male
    • Joined on 07-28-2009
    • Melbourne, Australia
    • Posts 18

    Re: Transferring data between two forms

    Hi RhysAZ,

    As long as you have se the name spaces to the one stated in the properties of the "SetName" and "SetUserName" fields.  I don't think it is the name spaces either.

    Try setting a default value for the fields in both forms to ensure that they are selectable in the XML DOM when using the DOM.selectSingleNode() function.

     

    You can also try assigning the fields in each form as variables separately, which will tell you which field there are issues with (see below).  Before you do this, make sure you have the correct XPaths by copying from each field:

    var  form1Field = XDocument.DOM.selectSingleNode("/my:myFields/my:dataFields/my:SetUserName");

    var  form2Field = objNewForm.DOM.selectSingleNode("/my:myFields/my:dataFields/my:SetName");

    Then set the value of the form2 field using the following:

    form2Field.text = form1Field.text;

    If you get an error at one of the first two lines then it was not able to find the required field.  You will also know which form has the problem depending on if it is the first or second line.

    Are you setting the value of the "SetName" and "SetUserName" fields using a calculation of other field values, or rules from other fields and controls? if so, you should check to make sure the correct value is in the field before it is being transferred to form2. If the "SetName" and "SetUserName" fields aren't bound to a control on the form's view, you should add them to each form so that it is clear what values are being copied.


    Blogs:
    SharePoint Admin & Dev - SharePoint and InfoPath Development

    Web Development & Programming - Php, C#, .NET Framework, SEO Tips

    WebmasterHub.net - Free SEO, Webmaster Resources
  • 08-17-2009 02:05 PM In reply to

    Re: Transferring data between two forms

    Hi Daniel- Tried setting the default values on both forms. No good that way. I then added the two "var" lines and the set the value of form2 field lines of code. Got the following error:

    The following error occurred:

    'null' is null or not an object
    File:script.js
    Line:51

     

    Line 51 is the set the value of form2 line of code (form2Field.text = form1Field.text;). When I took out that line of code, there was no more error. So, that is where the problem lies, in that line of code.

     

    FYI- I am presently not setting the value of the SetName and SetUserName using a calculation of or fields or by rules (just using a default value in textbox1 on form1). However, I will want to do that in the near future. Ultimately, when I have verified my user on form1, I want to use the users name from textbox1 on form1 and put that in textbox2 on form2.

     

     Is there an easy way to determine if I have the correct XPaths to textbox1 and textbox2? I have a feeling that this is my problem. And, if you can tell me any else about the line 51 error, that would be great. Thanks.

  • 08-18-2009 02:48 AM In reply to

    • Daniels
    • Not Ranked
      Male
    • Joined on 07-28-2009
    • Melbourne, Australia
    • Posts 18

    Re: Transferring data between two forms

     Hi RhysAZ,

     

    RhysAZ:
    Is there an easy way to determine if I have the correct XPaths to textbox1 and textbox2?

     You can get the correct XPaths for the fields in each form by opening both in design mode, then right clicking on the field in the main data source and select "Copy XPath".

     

    RhysAZ:
    And, if you can tell me any else about the line 51 error, that would be great. Thanks.

     It shouldn't matter if the value of the field in form1 or form2 are NULL, as long as they have been instantiated as an accessible object/XML Element.  It is only when the form is being submitted or if validation has been configured for a field that setting a value as NULL might become a problem.  The field from the main data source in form2 will be "not an object" if the XPath for the field is incorrect, as with the field in form1.  If the name spaces are not correct, the field will also be inaccessible, which may also result in the "null or not an object" error.

     It definitely seems that it is the field which is a null object, and not the value itself.  Setting the initial value using rules may help, but setting the default value or a calculation should be enough to make sure the field is accessible.

    You could also try adding text box controls for the fields to a view and manually enter a value in the form1 field before executing the code to load the second form, to ensure that the fields are initiated in the data source.

    Check to make sure you are setting the namespace for both the form2 (objNewForm) object/variable and the data source object for form2 (objExternalData).  The name space for the main data source in form1 should already have been set by InfopPath at the top of the code so you shouldn't have to worry about this.  Check that the namespace information from the field's properties of the field in form2 mastches the selection name spaces being set in the code.

    Let me know if you have any luck with this.

    Thanks

    Daniel

    Blogs:
    SharePoint Admin & Dev - SharePoint and InfoPath Development

    Web Development & Programming - Php, C#, .NET Framework, SEO Tips

    WebmasterHub.net - Free SEO, Webmaster Resources
Page 1 of 2 (24 items) 1 2 Next >
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.