code to validate data in Access withOUT using messageboxes UserPageVisits:111 active 80 80 DaniWeb 561 60 2005-04-23T00:46:24+00:00

code to validate data in Access withOUT using messageboxes


This code uses field level validation to check for data in fields in an Access form, and instead of using message boxes (as the "Validation Rule" and "Validation Text" properties of a field (in the table design window) do, it turns the associated label and control (textbox/listbox/combobox) background color to RED, and then freezes the cursor on the field that requires attention

About the Author
code snippet
Private Sub txtZipCode_Exit(Cancel As Integer)
Trim (txtZipCode)
If IsNull(txtZipCode) Then
Cancel = 1
Else: Cancel = 0
End If
If Cancel = 1 Then
txtZipCode.BackColor = vbRed
lblZIPCode.ForeColor = vbRed
txtZipCode.BackColor = vbWhite
lblZIPCode.ForeColor = vbBlack
End If
End Sub
Use the "Exit" routine - you need the "Cancel" property of that routine to freeze the cursor on the field.  Trim the field - that removes leading and trailing spaces (" " is not the same as "") - then you check for a null value...If the value is null, set the cancel (as integer) property to one...Otherwise set it to zero....If the cancel property is one, then (the tab was canceled) set the label and field control color properties (forecolor is the text color of the label, backcolor is the background color of the field) to red, and freeze the cursor...otherwise, set the background color (backcolor) of the field control to white, and the foreground color (forecolor) of the label to black...and allow a tab to the next field
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.