BeforeUpdate Event Access Example
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
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
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.
With Access 2010 comes new table-level events. There is a before change event which can be used as you use the before update event in forms. The before change table event gives you the ability to write macro code to perform data manipulation and validation before a data change takes place.
To try this beforeupdate example out on your computer you can go to our download center and download the single-user inventory demo database.
MS Access 2003
Microsoft Office VBA, MS Access 2003, 2007, 2010, 2013, 2016