BaldyWeb

Validate data before saving

There are a number of scenarios that share the same solution.  In general, data validation is best done in the before update event of your form.  That presumes the form is bound to a table or query of course.  You may want to make sure certain fields are filled out, that dates meet certain criteria, or you may have fields that only need to be filled out based on the contents of other fields. 

Note that as is often the case, there are other ways to solve some of these issues.  In the case of data validation, you can also use field properties such as required, validation rules, etc, but those are not the focus of this page.

The beauty of the before update event is that it can be cancelled, using "Cancel = True" in your code.  This stops the update from occurring and leaves the user on the form.  The code might look something like this:

If Len(Me.SomeControl & vbNullString) = 0 Then
  MsgBox "You need to fill out SomeControl"
  Cancel = True
  Me.SomeControl.SetFocus
End If

Home