The beforeupdate form event give you the ability to review the data entered by the user just before it is stored in the database.
BeforeUpdate event code is most often associated with data validation through visual basic script programming. BeforeUpdate event is activated in VBA before changed data in a
record is saved to the record source.
BeforeUpdate Event Example:
Visual Basic BeforeUpdate event is most often used to validate form
fields - here's a database programming example:
Private Sub Due_Date_BeforeUpdate(Cancel As Integer)
If Me.Due_Date<Me.Start_Date then
Msgbox "The Due Date must not come
before the Start Date"
Cancel=True
' caused the changes to not be saved
End If
End Sub
Here is another beforeupdate example from our downloadable Access inventory
database demo:
Private Sub Barcode_Combo_BeforeUpdate(Cancel As Integer)
Dim icount As Long
'check to see if this is a duplicate value before updating
icount = Nz(DLookup("barcode", "m_inv_out_items", "barcode=" & Me.BarCode), 0)
If icount <> 0 Then
' this item has already been selected
MsgBox "You have already selected this item."
Cancel = True
Undo
End If
End Sub
The purpose of the beforeupdate code above is to prevent the nasty Access
error message that pops up when a user enters an item in the list that has
already been entered. The code checks to see if the item has already been
selected. If it has then a message pops up telling the user about the
problem.
Finally the code clears out the value that has been selected.
To try this beforeupdate example out on your computer you can go to our
download center and download the
single-user inventory demo database.