VBScript works with Button control but not with OnLoad

M

mdsmitty

I have a line of code that works with a button control but I can't get it to
work with the OnLoad Event. I have been going around and around with this
for several days and need help.

This is the line of code I am using to have an Optional Section included by
default.

XDocument.View.ExecuteAction "xOptional::insert", "form_data_103"

I have tested this with a button control and the section opens, however, if
I add the same line of code to the OnLoad event, I get an error message. The
following shows my entire code for my OnLoad event.

'=======
' 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):
/dfs:myFields/dfs:queryFields/q:form_data/@Document_ID
' Note: Information in this comment is not updated after the function
handler is created.
'=======
Sub XDocument_OnLoad(eventObj)
XDocument.QueryAdapter.Query()
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103"
End Sub

I get an error message stating:

A run-time error has occured
Do you want to debug?

The following error occured:

Object required: 'XDocument.View'
File:script.vbs
Line:40

What I really don't understand is the fact that I have the Optional Section
control included in the form by default through the section's properties
dialog. I am querying a data source and in some cases there may not be any
information returned from the query and the user will enter a new
record/information to be submitted to the data source. It is in the case
that there is no info returned from the query that my optional section will
not display. I need the section to display at all times reguardless of the
query's result. If there is another method to accomplish the same result I
am open to suggestions. Unfortunately, I am a novice at writing script and
will need as much step by step as possilble. Further background on my
question can also be found with posting "Open Optional Section with VBScript"
on 6/8/05.

If anyone can help it would be greatly apprecitated. Thanks.
mdsmitty
 
M

mdsmitty

Thanks Greg, that worked as needed. Now the issue is that if the Optional
Section is displayed because the query DID return a record/result, I get an
error message:

The specified bstrAction value for the View.ExecuteAction call is not
applicable for the current selection.

Hopefully, I can make this all work by solving this final problem. The
issue is that when querying the data source sometimes the query will return
results if there is a matching record and the Optional Section will display
automatically as it is supposed to. Other times the query may return no
results, ie there is no record in the data source and the Optional Section
will not display which requires writing code in order to display. In short,
what I think I need now is an "If...Then..." statement to go with the
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103"

If the query returns a result then do not execute
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103".
If the query does not return a result then execute
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103".

Is it possible to use an "If...Then..." statement (I'm using VBScript) based
on the value of a specific field within the Optional Section and if it is
null or blank then run the code to insert the Optional Section?

Another silly question...how do I press the Tab key using code? The code
from my VBScript book does not seem to work in InfoPath. SendKeys {Tab}

Sorry for the lack of detail on the whole process. Thanks again for all
your help.

mdsmitty

Greg Collins said:
In short, the View object doesn't exist during OnLoad.
Check this out: http://www.infopathdev.com/howto/tips/default.aspx?i=9de539adb4e846a5900a2687debb8963

--
Greg Collins [InfoPath MVP]
Please visit: http://www.InfoPathDev.com



I have a line of code that works with a button control but I can't get it to
work with the OnLoad Event. I have been going around and around with this
for several days and need help.

This is the line of code I am using to have an Optional Section included by
default.

XDocument.View.ExecuteAction "xOptional::insert", "form_data_103"

I have tested this with a button control and the section opens, however, if
I add the same line of code to the OnLoad event, I get an error message. The
following shows my entire code for my OnLoad event.

'=======
' 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):
/dfs:myFields/dfs:queryFields/q:form_data/@Document_ID
' Note: Information in this comment is not updated after the function
handler is created.
'=======
Sub XDocument_OnLoad(eventObj)
XDocument.QueryAdapter.Query()
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103"
End Sub

I get an error message stating:

A run-time error has occured
Do you want to debug?

The following error occured:

Object required: 'XDocument.View'
File:script.vbs
Line:40

What I really don't understand is the fact that I have the Optional Section
control included in the form by default through the section's properties
dialog. I am querying a data source and in some cases there may not be any
information returned from the query and the user will enter a new
record/information to be submitted to the data source. It is in the case
that there is no info returned from the query that my optional section will
not display. I need the section to display at all times reguardless of the
query's result. If there is another method to accomplish the same result I
am open to suggestions. Unfortunately, I am a novice at writing script and
will need as much step by step as possilble. Further background on my
question can also be found with posting "Open Optional Section with VBScript"
on 6/8/05.

If anyone can help it would be greatly apprecitated. Thanks.
mdsmitty
 
M

mdsmitty

I have tried this:
'=======
' 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):
/dfs:myFields/dfs:dataFields/d:Account/d:form_data/@provider
' Note: Information in this comment is not updated after the function
handler is created.
'=======
Sub XDocument_OnSwitchView(eventObj)
If XDocument.DOM.selectNodes(XPath)= null then
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103"
End If
XDocument.DOM.selectNodes(XPath) is true
End Sub

But I get this error message:

Expresstion must evaluate to a node-set.

<--
File:script.vbs
Line:48

Am I close? Thanks.
mdsmitty

mdsmitty said:
Thanks Greg, that worked as needed. Now the issue is that if the Optional
Section is displayed because the query DID return a record/result, I get an
error message:

The specified bstrAction value for the View.ExecuteAction call is not
applicable for the current selection.

Hopefully, I can make this all work by solving this final problem. The
issue is that when querying the data source sometimes the query will return
results if there is a matching record and the Optional Section will display
automatically as it is supposed to. Other times the query may return no
results, ie there is no record in the data source and the Optional Section
will not display which requires writing code in order to display. In short,
what I think I need now is an "If...Then..." statement to go with the
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103"

If the query returns a result then do not execute
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103".
If the query does not return a result then execute
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103".

Is it possible to use an "If...Then..." statement (I'm using VBScript) based
on the value of a specific field within the Optional Section and if it is
null or blank then run the code to insert the Optional Section?

Another silly question...how do I press the Tab key using code? The code
from my VBScript book does not seem to work in InfoPath. SendKeys {Tab}

Sorry for the lack of detail on the whole process. Thanks again for all
your help.

mdsmitty

Greg Collins said:
In short, the View object doesn't exist during OnLoad.
Check this out: http://www.infopathdev.com/howto/tips/default.aspx?i=9de539adb4e846a5900a2687debb8963

--
Greg Collins [InfoPath MVP]
Please visit: http://www.InfoPathDev.com



I have a line of code that works with a button control but I can't get it to
work with the OnLoad Event. I have been going around and around with this
for several days and need help.

This is the line of code I am using to have an Optional Section included by
default.

XDocument.View.ExecuteAction "xOptional::insert", "form_data_103"

I have tested this with a button control and the section opens, however, if
I add the same line of code to the OnLoad event, I get an error message. The
following shows my entire code for my OnLoad event.

'=======
' 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):
/dfs:myFields/dfs:queryFields/q:form_data/@Document_ID
' Note: Information in this comment is not updated after the function
handler is created.
'=======
Sub XDocument_OnLoad(eventObj)
XDocument.QueryAdapter.Query()
XDocument.View.ExecuteAction "xOptional::insert", "form_data_103"
End Sub

I get an error message stating:

A run-time error has occured
Do you want to debug?

The following error occured:

Object required: 'XDocument.View'
File:script.vbs
Line:40

What I really don't understand is the fact that I have the Optional Section
control included in the form by default through the section's properties
dialog. I am querying a data source and in some cases there may not be any
information returned from the query and the user will enter a new
record/information to be submitted to the data source. It is in the case
that there is no info returned from the query that my optional section will
not display. I need the section to display at all times reguardless of the
query's result. If there is another method to accomplish the same result I
am open to suggestions. Unfortunately, I am a novice at writing script and
will need as much step by step as possilble. Further background on my
question can also be found with posting "Open Optional Section with VBScript"
on 6/8/05.

If anyone can help it would be greatly apprecitated. Thanks.
mdsmitty
 
R

Renee in Juneau

Just curious but, as a non-code option, have you tried putting the
query-related fields in a "regular" section that's embedded within the
optional section?

The regular section, which will have the results of the user's input,
should always show, right? And the encasing optional section can be
used to determine whether or not the query-related section should be
included at all?
 
M

mdsmitty

Thanks for your input Renee. Unfortunately if the query does not return
results for that section then the section will not be displayed.
Additionally if the section is a "regular" section then there is no way to
insert it because it is basically hidden. I am going to look into different
combinations of sections to see if something pans out.

I'll keep you posted. Thanks agian
mdsmitty
 

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