InfoPath 2003 Toolkit
To bind the Submit Request event to a managed code event handler, first select the “Enable Submit commands and buttons” radio button and then change the Submit To drop-down list to “Custom submit using form code.” Normally, like all the other events discussed here, this would generate a JavaScript routine to handle the event, but with the Toolkit it switches to Visual Studio 2005 and creates the event handler there, instead using the language you’ve chosen.
If you uncheck the Edit Form Code option, the event handler is added to your FormCode.vb file in the project, but the InfoPath designer remains active. As the generated code in the following listing illustrates, the OnSubmitRequest event is associated with submitting a form and contains a DocReturnEvent argument (the same object type used in the OnLoad event):
<InfoPathEventHandler(EventType:=InfoPathEventType.OnSubmitRequest)> _ Public Sub OnSubmitRequest(ByVal e As DocReturnEvent)
‘If the submit operation is successful, set
‘e.ReturnStatus = true
‘Write your code here.
End Sub
When you handle the OnSubmitRequest event, saving the data contained in the InfoPath form is left up to your code. However, because the InfoPath designer can display messages indicating whether the submission was successful or not (accessed through the Submit Options button shown in Figure 54-7), you should set the ReturnStatus property appropriately.
Rather than submit the form, what happens if the user edits and then saves the form? The answer is that there is yet another form-wide event called OnSaveRequest that you can intercept, and then perform your own functionality (including rejecting their changes).
To generate the event handler for this event, open the Form Options dialog and switch to the Open and Save tab (shown in Figure 54-8). Check the Save Using Custom Code option to generate the event handler routine and click the Edit button alongside it to switch to the Visual Studio IDE and locate the routine in the FormCode.vb module.
The automatically generated code for the OnSaveRequest event appears similar to the following listing:
<InfoPathEventHandler(EventType:=InfoPathEventType.OnSaveRequest)> _ Public Sub OnSaveRequest(ByVal e As SaveEvent)
‘Write the code to be run before saving here.
e.IsCancelled = e.PerformSaveOperation
‘Write the code to be run after saving here.
e.ReturnStatus = true End Sub
The SaveEvent argument contains a number of useful properties when saving the form. Because intercepting the event means that InfoPath won’t perform its own default Save (or Save As) functionality, you need to set the ReturnStatus property to indicate whether your own save process worked. If you do not set this property to True, InfoPath displays an error message to the end user indicating that the save was unsuccessful.