Check a customer form
In this tutorial, you add a validator to a customer form: the name is required, the ZIP code must have five digits, and every customer needs a phone number or an email address. A broken rule keeps the focus in the field, and a record that breaks a rule isn't saved.
Prerequisites
- Microsoft 365 Access or the Access Runtime, 32-bit or 64-bit, with the WKL Forms Object Library 1.31 or later installed and registered for the current Windows user (How to: Install the released package).
- A new or existing database in a trusted location.
- Every WKL control opens its designer the same way: in Design view, open the property sheet (F4), go to the Other tab, and click
[...]in the Custom row (Opening the designer).
Steps
1. Create the sample table
Paste this procedure into a standard module and run it once from the Immediate window
(CreateValidatorSample):
Public Sub CreateValidatorSample()
With CurrentDb
.Execute "CREATE TABLE tblCustomer (ID COUNTER CONSTRAINT PK_Customer PRIMARY KEY, Name TEXT(50), " & _
"ZIP TEXT(10), Phone TEXT(30), Email TEXT(100))"
.Execute "INSERT INTO tblCustomer (Name, ZIP, Phone) VALUES ('Miller', '10001', '212 555 0100')"
End With
End Sub
2. Create the form with a module
Create a single form named frmCustomer that's bound to tblCustomer, with the text boxes Name,
ZIP, Phone, and Email. Set the form's Has Module property (Other tab) to Yes. The
module can stay empty, but without one, Access doesn't pass any events to the validator.
3. Insert the validator
Choose Form Design › Controls › ActiveX Controls › WKL Validator and draw the control below the
fields, about as wide as the form and 0.6 cm high. Name it Validator1. Save the form and leave it
open in Design view.
4. Add the rules
Paste this procedure into a standard module, run it from the Immediate window (SetCustomerRules)
while the form is open in Design view, and then save the form:
Public Sub SetCustomerRules()
Forms!frmCustomer!Validator1.Object.Rules = _
"{""fields"":[" & _
"{""field"":""Name"",""required"":true}," & _
"{""field"":""ZIP"",""pattern"":""[0-9]{5}"",""text"":""The ZIP code has five digits.""}]," & _
"""records"":[{""atLeastOne"":[""Phone"",""Email""],""text"":""Enter a phone number or an email address.""}]}"
End Sub
You can also enter the same rules on the designer's Field rules and Record rules pages
(property sheet, Other tab, Custom row, [...]).
5. Try it out
Open the form in Form view. Type 123 in ZIP and press Tab: the focus stays in the field, the
field gets a red border, and the validator shows “1 error – The ZIP code has five digits.” Change
the value to 10002. Clear Phone and try to save the record (Shift+Enter): the save is
rejected, and the summary names the record rule. Enter an email address, and the record is saved.
Expected result
- No message box appears; the errors show next to the fields and in the summary.
- A record with an error isn't saved when the user saves it or moves to another record. If the user closes the form while an unsaved record breaks a rule, Access asks whether to close without saving, as it does whenever a save is rejected.
- After Undo (Esc twice), all error marks are gone.
Next steps
- How to: Check an Access form with WklValidator: rules that apply only to a certain kind of record, data service errors in the summary, and a docked edit form.