Unable to reference a data source element in script?

J

JC

Hi,

I am trying the following in VBScript:

MsgBox XDocument.DOM.selectSingleNode("//my:field1").text

But I get an "Object Required: " error on that line. AFAIK I have the
namespace declared properly... and I do have a "field1" field in my data
source.

Something else I tried, was to right-click the textbox, go to Properties >
Data Validation > Events: OnBeforeChange > Edit, at which point InfoPath
switched to the script editor and inserted the following comment... Note the
3rd line:

'=======
' 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:myFields/my:field1
' Note: Information in this comment is not updated after the function
handler is created.
'=======

So I edited the line of code to read instead:

MsgBox XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text

However I receive the exact same error. What am I doing wrong?

Thanks in advance,
JC
 
S

Scott Roberts [MSFT]

I just tried this with a textbox bound to my:field1 and it works. Can you
explain in more detail how you built your form?

- Scott
 
S

Scott L. Heim [MSFT]

Hi JC,

I placed a text box (named field1) and a button on my form.On the click
event of the button, I have the following:

MsgBox XDocument.DOM.selectSingleNode("//my:myFields/my:field1").text

I then Preview the form, enter Scott in the text box and when I click the
button the message box displays the correct text. However, if I remove one
of the intial slashes ("/") so that I have: "/my:myFields/my:field1" I get
an empty message box.

Hopefully this will help!

Scott Heim
Microsoft
 
S

Scott Roberts [MSFT]

That's odd b/c when I try it with just one slash it works. Here is what I
did. Let me know if this matches what you did.

1. Create a new blank form (Set language to VBScript.)
2. Insert a text box
3. Open the properties dialog of the textbox
4. Click on the data validation button and choose to edit the OnBeforeChange
event
5. Enter MsgBox
XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text in the event
handler
6. Preview the form
7. Enter text in the text box and tab away

At this point I see the alert.

- Scott
 
J

JC

Scott,

Thank you for your reply. I am going crazy here :)

When I first started designing the form, I built it by modifying the Issue
Tracking (Simple) sample. Could this be the cause for the problem?

I created a blank form, and the code below works.... So why would it not
work in my actual form?

Thanks again,
JC
 
S

Scott Roberts [MSFT]

The Issue Tracking form is pretty complex. It's possible that something
else in the form is causing this to fail. If I get a few minutes, I'll try
this in the Issue Tracking form.

- Scott
 
S

Scott L. Heim [MSFT]

Hi JC,

Well modifying one of the built-in samples could be the problem but without
knowing all the modifications you made, it's difficult to tell. However,
let me document the steps I just followed with the same template:

1) Chose to design the Issue Tracking (Simple) Sample
2) Displayed the Datasource Task Pane
3) Right-clicked on the root "issue" element and chose Add
4) Entered a name of: Field1 and clicked OK
5) Dragged Field1 onto the Issue Tracking View
6) Added a button to the Issue Tracking View
7) Edited the code for the button and entered the following (jscript):

var myFld = XDocument.DOM.selectSingleNode("//my:Field1");
XDocument.UI.Alert(myFld.text);

8) Previewed the solution, entered Scott in Field1 and clicked the button -
result: a message box was displayed with Scott.

Now I realize these are very basic changes but try these with a new copy of
the sample and let me know the results.

Best Regards,

Scott L. Heim
Microsoft

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