Assigning values to an InfoPath field using VBScript

C

craigster76

I am trying to obtain the network username when an InfoPath form loads, then
assign that username to a field in the form. I have tried many different
variations of examples that I have found, but none have worked. Below is
some of the code I have tried in the "Sub XDocument_OnLoad(eventObj)" event:

XDocument.DOM.selectSingleNode("/my:myFields/my:field2") = objNet.UserName
XDocument.DOM.selectSingleNode("../my:Username") = objNet.UserName
XDocument.DOM.selectSingleNode("field2").Text = objNet.UserName

Does anyone have any other suggestions?

Thank you in advance for any help you can provide.

Craig
 
S

Scott L. Heim [MSFT]

Hi Craig,

What you are probably running into is an issue with your "XPATH" expression
to get to the correct node. Try these steps so you can see this work then
you can apply it to your own solution:

- Create a new, blank InfoPath form
- Add a text box to the form (by default it will be: field1)
- Add a button to your form
- Right-click on the button, choose Properties and click the Edit Form Code
button
- Add this code to the button's click event:

Dim objField1
Set objField1 =
XDocument.DOM.selectSingleNode("//my:myFields/my:field1")
objField1.text = "Scott"

- Preview the form and test - you should see "Scott" in the text box.
- Now go back to the form in Design View, click the Tools menu, choose
Programming, select "On Load Event" and add the same code to the load event
- what is the result?

In most cases I see, the issue of not being able to set text in a field
revolves around not having the appropriate XPATH expression. One trick that
you may not be aware of: if you want to get the XPATH to a control use the
Expression Box. You don't need to actually add one to your form - simply
choose that control, click the "fx" button, go select the field you want
and then enable the "Edit XPath" box - this will show you the complete path
(including namespaces!) to your control...except for the root node (i.e.
myFields.)

I hope this helps!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top