|
Question : Access Form Validation
|
|
Hi
I have a simple Access form that is linked to a table. The user can enter a new record simply by typing into the form as they would into a table. I need to do certain validation checks but am not sure where to put the code Should it be in the before update event? What code do I write to prevent the update occurring. Thanks
|
|
Answer : Access Form Validation
|
|
You can either put validation code in the Form_BeforeUpdate event to validate the entire form, but I much prefer to treat each control in it's own right by placing validation code in each individual control's BeforeUpdate event.
The kind of code you put there will depend on the (expected) data type and range of the underlying data; e.g.
Private Sub txtFirstName_BeforeUpdate(Cancel As Integer)
if(txtFirstName.Text & vbNullString = vbNullString) Then MsgBox "Firstname is required!", vbOkOnly Cancel = True txtFirstName.Undo End if
End Sub
Alternatively, a very effective way to handle data validation is to use the underlying Table's Validation Rule and Validation Text properties for the field to be validated; e.g.
Table tbl_person_data
Firstname Text(20) Validation Rule (Is Not Null) And (<> "") Validation Text "Firstname is a required field"
DateOfBirth Date/Time Validation Rule (> #01-Jan-1930#) and (< Now()) Validation Text "Date of Birth cannot refer to a date in the future" etc.
NumberOfChildren Integer Validation Rule (>=0) And (<=10) Validation Text "System cannot cope with less than 0, or more than 10 children"
RangeOfNumbers Validation Rule IN(2,3,4,5,6,7) Validation Text "Number range outside acceptable range (should be in the range 2-7 inclusive)"
|
|
|
|