code to validate data in Access withOUT using messageboxes

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
rhenerlau rhenerlau is offline Offline Apr 22nd, 2005, 3:46 pm |
0
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
Quick reply to this message  
Visual Basic 4 / 5 / 6 Syntax
  1. Private Sub txtZipCode_Exit(Cancel As Integer)
  2. Trim (txtZipCode)
  3. If IsNull(txtZipCode) Then
  4. Cancel = 1
  5. Else: Cancel = 0
  6. End If
  7. If Cancel = 1 Then
  8. txtZipCode.BackColor = vbRed
  9. lblZIPCode.ForeColor = vbRed
  10. txtZipCode.SetFocus
  11. Else
  12. txtZipCode.BackColor = vbWhite
  13. lblZIPCode.ForeColor = vbBlack
  14. txtEmailAddress.SetFocus
  15. End If
  16. End Sub
  17. ======================================================
  18. 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

Message:


Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC