PDA

View Full Version : Validating Required Data in Microsoft Access Text boxes



knifelong
05-30-2012, 05:33 AM
Hi

I have this code which allows you to place a "*" in the tab property of a form text box and will prompt you to enter a value before you go to the next record if the text box is empty. It can be run from in the BeforeUpdate Event of the form to validate before moving to the next record but I want to add it to an on click submit button which takes you to the next record. I want to add an else to the if statement with DoCmd.GoToRecord , , acNext which will go to the next record if all the text boxes are validated correctly.

But I can't seem to make it work as I keep getting an exception. Any ideas.


Thanks








'Place an asterisk (*) in the Tag Property of the text
'boxes you wish to validate.
'Then in the BeforeUpdate Event of the form, copy/paste the following:


Dim msg As String, Style As Integer, Title As String
Dim nl As String, ctl As Control


nl = vbNewLine & vbNewLine

For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "*" And Trim(ctl & "") = "" Then
msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
Style = vbCritical + vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus
Cancel = True
Exit For
End If
End If
Next