Form fields and views

G

Gary

I'm new to infopath and I have what I hope is an easy question.

I'm trying to add text to a field that is only on the view that is not the
default view. If I use the following sytax I receive an error message (Object
reference not set)..

thisXDocument.DOM.selectSingleNode("//my:ToField").text = "Test"

and if I use the following sytax I don't receive an error but the field is
blank...

e.Site.text = "Test"

The first is in the OnLoad function for the form, the second is on
OnAfterChange

Thanks for the help!!
 
S

S.Y.M. Wong-A-Ton

If the field is in the Main data source of your form, you should be able to
set it using the first syntax (irrespective of the view that it's on).
Perhaps you should double-check whether the XPath to the field is correct or
whether you are using the correct namespace.
 
G

Gary

Thanks for the reply.

I noticed that I have this problem with couple of fields. All of them a part
of a group or folder.

datafields
d:tblTimesheet
:TimeSheetID

I'm not sure how to secify that in XPath
 
S

S.Y.M. Wong-A-Ton

Oh yes, the namespaces are causing the problem. Did you add the fields you're
trying to set yourself, or did InfoPath do that for you when you bound it to
a database?

Most likely an XPath expression like

"/dfs:myFields/dfs:dataFields/d:tblTimesheet/d:TimeSheetID"

would work, but since I don't know how the XML for your form is composed, I
cannot tell for sure. Check out this article
http://enterprise-solutions.swits.net/infopath/programmatically-add-db-record-vbscript.htm
on my website. It uses a database and contains sample XPath expressions in
code. It might give you an idea of how you could go about finding the right
XPath expression for your form.
 
G

Gary

Thanks for the reply.

I read your article and I tried to break my code down to what you have
coded. The following statement...

objFieldsData =
thisXDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields")

gave me this message...

Unable to cast COM object of type 'System.__ComObject' to interface type
'Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMCharacterData'. This
operation failed because the QueryInterface call on the COM component for the
interface with IID '{2933BF84-7B36-11D2-B20E-00C04F983E60}' failed due to the
following error: No such interface supported (Exception from HRESULT:
0x80004002 (E_NOINTERFACE)).

The following 2 statements were as follows...

objTimeSheetNode =
thisXDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTimesheet")

objTimeSheetId =
thisXDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTimesheet/TimeSheetID")

both of these fields were set to 'nothing'.

Thanks for the help!
 
S

S.Y.M. Wong-A-Ton

Since you're using managed code, try debugging your code with the Visual
Studio debugger and see what

thisXDocument.DOM.xml

returns. Based on the XML returned, you should be able to find the correct
XPath expression (and namespaces) to the necessary fields.

If the objects are "Nothing" it means that the XPath expressions are still
incorrect.
 
G

Gary

I can not figure this out. Listed below is the results from the xml
statement...

<?xml version="1.0"?>
<?mso-infoPathSolution initialView="View 1" productVersion="11.0.6565"
PIVersion="1.0.0.0"
href="file:///C:\Documents%20and%20Settings\gmonk\Local%20Settings\Application%20Data\Microsoft\InfoPath\Designer\500b178fec844896\manifest.xsf"
solutionVersion="1.0.0.644" ?>
<?mso-application progid="InfoPath.Document"?>
<dfs:myFields my:UserLookup="" my:payPeriod=""
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:tns="http://tempuri.org/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-03-28T09:46:16"
xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us"
xmlns:xdado="http://schemas.microsoft.com/office/infopath/2003/adomapping">
<dfs:queryFields>
<q:tblTimeSheet TimeSheetID="" EmpID="" Date=""></q:tblTimeSheet>
<q:tblTimeSheetHours TimeSheetDetailID="" TimeSheetID="" Exempt=""
TotalHours="" HoursSunday="" HoursMonday="" HoursTuesday="" HoursWednesday=""
HoursThursday="" HoursFriday="" HoursSaturday="" HoursSunday1=""
HoursMonday1="" HoursTuesday1="" HoursWednesday1="" HoursThursday1=""
HoursFriday1="" HoursSaturday1="" TotalHours1=""></q:tblTimeSheetHours>
<q:tblTimeReviewer TimeSheetID="" Reviewer="" Approval=""
Date=""></q:tblTimeReviewer>
</dfs:queryFields>
<dfs:dataFields>
<d:tblTimeSheet TimeSheetID="" EmpID="" Date="2006-07-22">
<d:tblTimeSheetHours TimeSheetDetailID="" TimeSheetID="" Exempt=""
TotalHours="" HoursSunday="" HoursMonday="" HoursTuesday="" HoursWednesday=""
HoursThursday="" HoursFriday="" HoursSaturday="" HoursSunday1=""
HoursMonday1="" HoursTuesday1="" HoursWednesday1="" HoursThursday1=""
HoursFriday1="" HoursSaturday1="" TotalHours1=""></d:tblTimeSheetHours>
<d:tblTimeReviewer TimeSheetID="" Reviewer="" Approval=""
Date=""></d:tblTimeReviewer>
</d:tblTimeSheet>
</dfs:dataFields>
<my:Name></my:Name>
<my:Department></my:Department>
<my:EmailAddress></my:EmailAddress>
<my:ToField></my:ToField>
<my:HelperW1Tot></my:HelperW1Tot>
<my:Notes></my:Notes>
<my:FormSigning></my:FormSigning>
<my:Week1>
<my:InSun xsi:nil="true"></my:InSun>
<my:InMon xsi:nil="true"></my:InMon>
<my:InTue xsi:nil="true"></my:InTue>
<my:InWed xsi:nil="true"></my:InWed>
<my:InThu xsi:nil="true"></my:InThu>
<my:InFri xsi:nil="true"></my:InFri>
<my:InSat xsi:nil="true"></my:InSat>
<my:OutSun xsi:nil="true"></my:OutSun>
<my:OutMon xsi:nil="true"></my:OutMon>
<my:OutTue xsi:nil="true"></my:OutTue>
<my:OutWed xsi:nil="true"></my:OutWed>
<my:OutThu xsi:nil="true"></my:OutThu>
<my:OutFri xsi:nil="true"></my:OutFri>
<my:OutSat xsi:nil="true"></my:OutSat>
<my:Lunch></my:Lunch>
</my:Week1>
<my:Week2>
<my:InSun1 xsi:nil="true"></my:InSun1>
<my:InMon1 xsi:nil="true"></my:InMon1>
<my:InTue1 xsi:nil="true"></my:InTue1>
<my:InWed1 xsi:nil="true"></my:InWed1>
<my:InThu1 xsi:nil="true"></my:InThu1>
<my:InFri1 xsi:nil="true"></my:InFri1>
<my:InSat1 xsi:nil="true"></my:InSat1>
<my:OutSun1 xsi:nil="true"></my:OutSun1>
<my:OutMon1 xsi:nil="true"></my:OutMon1>
<my:OutTue1 xsi:nil="true"></my:OutTue1>
<my:OutWed1 xsi:nil="true"></my:OutWed1>
<my:OutThu1 xsi:nil="true"></my:OutThu1>
<my:OutFri1 xsi:nil="true"></my:OutFri1>
<my:OutSat1 xsi:nil="true"></my:OutSat1>
</my:Week2>
<my:HelperW2Tot></my:HelperW2Tot>
<my:Excel>-4105</my:Excel>
<my:WeekOf1></my:WeekOf1>
<my:WeekOf2></my:WeekOf2>
<my:payperiods></my:payperiods>
<my:signatures5></my:signatures5>
<my:signatures2></my:signatures2>
</dfs:myFields>

I would think that the following path would be correct...

thisXDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTimesheet/TimeSheetID").text but I receive the same error.

I have a Monday deadline for this project and I can not figure this out.

Thanks for the help!!!
 
S

S.Y.M. Wong-A-Ton

Gary,

"TimeSheetID" is an attribute and not an element, so you should try an XPath
expression like:

/dfs:myFields/dfs:dataFields/d:tblTimesheet/@TimeSheetID

Also, since you have a list of namespace prefixes in your XML that may not
be automatically recognized when running your code, you should set the
namespaces on the DOM using the "setProperty" method and the
"SelectionNamespaces" property, before trying to retrieve nodes. Search this
newsgroup on the "SelectionNamespaces" to find samples on how to get this
done.
 
S

S.Y.M. Wong-A-Ton

Since the XML node cannot be found, it is not an object, so calling a "text"
property on something that does not have a "text" property will result in an
error.
 
G

Gary

Thanks again for the help.
I tried your first suggestion

/dfs:myFields/dfs:dataFields/d:tblTimesheet/@TimeSheetID

and then I tried the following statements, and they didn't work

Dim oDoc As IXMLDOMDocument3 = CType(thisXDocument.DataObjects(0).DOM,
IXMLDOMDocument3)

oDoc.setProperty("SelectionNamespaces",
"xmlns:dfs='http://schemas.microsoft.com/office/infopath/2003/dataFormSolution' xmlns:d='http://schemas.microsoft.com/office/infopath/2003/ado/dataFields'")

a =
thisXDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblTimeSheet/@TimeSheetId").text

Am I doing something different form other developers? I can access any
element, just not the attributes.
 

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