Save using custom code question

D

Dan

Hello,

I want to add custom code (VB) to my Access database connected form which
will generate a filename and path when the user clicks Save. I've added the
following code to the save behaviour but I'm having trouble figuring out the
syntax to pull data out of fields on the form. This is my code:


Public Sub FormEvents_Save(ByVal sender As Object, ByVal e As
SaveEventArgs)
' Write your code that will run before a Save or Save As
operation here.
Dim flname As String
flname =
MainDataSource.CreateNavigator().SelectSingleNode("/myFields/dataFields/TimeTable/ID").ToString
Me.SetSaveAsDialogFilename(flname)

Me.SetSaveAsDialogLocation("g:\!workgrp\tenures\tenuretracking\savedforms")

' The Dirty property will be set to false if save is successful.
e.PerformSaveOperation()

' Write your code that will run after saving here.

e.CancelableArgs.Cancel = False
End Sub


It works if I comment out the flname = statement. This is the error I get:


System.NullReferenceException
Object reference not set to an instance of an object.
at Road_Permit_Amendment.FormCode.FormEvents_Save(Object sender,
SaveEventArgs e)
at
Microsoft.Office.InfoPath.Internal.FormEventsHost.OnSaveRequest(SaveEvent
pEvent)
at
Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnSaveRequest(SaveEvent pEvent)

Can someone please tell me what I'm doing wrong?
 
S

S.Y.M. Wong-A-Ton

Use .Value instead of .ToString ( see
http://www.bizsupportonline.net/inf...faq.htm#how-to-retrieve-field-value-from-code )

Like this...

flname =
MainDataSource.CreateNavigator().SelectSingleNode("/myFields/dataFields/TimeTable/ID").Value

But your NullReferenceException could also be related to the fact that the
XPath to the field is incorrect. Use the "Copy XPath" functionality to get
the correct XPath expression ( see
http://www.bizsupportonline.net/infopath2007/retrieve-value-infopath-form-field-code.htm )
 

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