DoCmd.Save? What is being saved?

hollyquinn

Registered User.
Local time
Today, 12:38
Joined
Oct 7, 2008
Messages
17
Hi I have a question about a VBA Module that I'm looking at for a form. There are some conditional statements that checks to see if some drop down lists are have null values, and then if they do it displays an error message. The part that is confusing me, is that at the end of the sub routine, the DoCmd.Save command is called. I don't see that anything in particular is saved, so I'm unsure what's going on. Here's the code if someone can help. Thanks in advance.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not IsNull(Me.BidPeriod) And IsNull(Me.Crew) Then
MsgBox "Please enter data for BOTH the the Bid Period and Crew Group, or blank for both!" _
& Chr(10) & " " _
& Chr(10) & "ASOK requires a Bid Period and a Crew Group to update the Training Schedule Matrix.", vbCritical + vbOKOnly, "ASOK Session Save"
Me.BidPeriod = Null
Me.Crew = Null
Cancel = True
Exit Sub
End If
If IsNull(Me.BidPeriod) And Not IsNull(Me.Crew) Then
MsgBox "Please enter data for BOTH the the Bid Period and Crew Group, or blank for both!" _
& Chr(10) & " " _
& Chr(10) & "ASOK requires a Bid Period and a Crew Group to update the Training Schedule Matrix.", vbCritical + vbOKOnly, "ASOK Session Save"
Me.BidPeriod = Null
Me.Crew = Null
Cancel = True
Exit Sub
End If
DoCmd.Save
End Sub
 
A brief visit to Help would reveal that the Save method will save an object. If not specified, it saves the active object (the form in this case). I would guess that the person who wrote that code erroneously thought that would save the data.
 
A brief visit to Help would reveal that the Save method will save an object. If not specified, it saves the active object (the form in this case). I would guess that the person who wrote that code erroneously thought that would save the data.

Oh sorry, I went to MSDN library but it didn't explain what it meant if no object were specified. I'm a .NET developer, not used to using help. :)

But thank you for your answer. So, am I right that the only thing that's going on here is that a message box is displayed based on a condtion?
 
Not quite. The before update event of a form is often used to validate data. If the validation fails (the user didn't input data correctly), adding this line:

Cancel = True

stops the update and leaves the data unsaved. The Save line that I assume was meant to save the data is actually unnecessary, as if the "Cancel = True" line is not executed the data will be saved automatically (presuming a bound form).
 
Not quite. The before update event of a form is often used to validate data. If the validation fails (the user didn't input data correctly), adding this line:

Cancel = True

stops the update and leaves the data unsaved. The Save line that I assume was meant to save the data is actually unnecessary, as if the "Cancel = True" line is not executed the data will be saved automatically (presuming a bound form).


Great, thanks very much for your help.
 
There is even more wrong with the code than that. The -
Me.BidPeriod = Null
Me.Crew = Null
statements should be
Me.BidPeriod.Undo
Me.Crew.Undo
to remove the invalid values. As the lines are written, they are changing the stored value from what it was to null.
 

Users who are viewing this thread

Back
Top Bottom