command button w/ tag required

teiben

Registered User.
Local time
Today, 12:59
Joined
Jun 20, 2002
Messages
462
I'm using the following code, has worked fine on the beforeupdate on a forms control. Problem is I'm trying to use this as validation on a command button. When the command button is selected I want the code to go through and validate the fields in which the tag property is set at "required". When I run this it errors at Cancel = true, variable not defined.

Is there anyother method to validation on a command button.

Private Sub cmdSaveandmail_Click()
Dim blnContinue As Boolean
Dim ctl As Control
blnContinue = True
For Each ctl In Me.Controls
If ctl.Tag = "Required" Then
If IsNull(ctl) Then
MsgBox "Required Information Missing", vbCritical + vbOKOnly + vbDefaultButton1, "You must fill in the field indicated with red"

Cancel = True
ctl.SetFocus
Exit For
End If
End If
Next ctl
Set ctl = Nothing
end sub
 
I just finished a wrestling match with Cancel myself, what a nightmare. One dumb question first, I am confused at why this line is here:
If ctl.Tag = "Required" Then
The next line should handle everything, I would think. What I found in the out, because yours looks a little like mine, is that the .SetFocus handled everything and that the Cancel was not really needed. Try commenting it out. hth.
 
Your cmdSaveandmail_Click() sub routine does not support the cancel event. The BeforeUpdate event does. You need to exit the sub after the message box. Something like this...

Code:
Private Sub cmdSaveandmail_Click()

Dim blnContinue As Boolean
Dim ctl As Control

blnContinue = True

For Each ctl In Me.Controls

If ctl.Tag = "Required" Then
If IsNull(ctl) Then
MsgBox "Required Information Missing", vbCritical + vbOKOnly + vbDefaultButton1, "You must fill in the field indicated with red"
'Cancel = True 'invalid option for this event
Exit Sub
ctl.SetFocus
Exit For
End If
End If

Next ctl

Set ctl = Nothing

end sub
 
Last edited:

Users who are viewing this thread

Back
Top Bottom