command button validation

A

alex

I have the following code in an Access Form:
It works well; however, if a user enters the form and hits the
command
button below (54) (without entering any info) the command triggers
the
Msgbox event, without checking form/table validation.

Conversely, if a user enters the form and types even a keystroke,
then
hits the command button, Access knows to kick in the validation that
I've set up and instructs the user to populate required fields.


Question: how to make the code below check for form/table validation
even if the user types nothing into the form?


Thanks for your help ~ alex


Private Sub Command54_Click()
On Error GoTo Err_Command54_Click

Dim strMessage As String
Dim intOptions As Integer
Dim bytChoice As Byte

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70

strMessage = "RECORD SAVED! ARE YOU FINISHED?"
intOptions = vbQuestion + vbYesNo
bytChoice = MsgBox(strMessage, intOptions)

If bytChoice = vbYes Then
DoCmd.PrintOut
DoCmd.Close
Else
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl ("RECEIPT")
End If

Exit_Command54_Click:
Exit Sub

Err_Command54_Click:
MsgBox Err.Description
Resume Exit_Command54_Click

End Sub
 
J

J

Somtimes it's just faster to show (modify the if statement to match
your textboxes):



Private Sub Command54_Click()
On Error GoTo Err_Command54_Click

IF Nz(TEXTBOX1_TEXTFORMAT,"")<>"" AND Nz(TEXTBOX2_NUMBERFORMAT,0)<>0
AND Nz(TEXTBOX3_DATEFORMAT,#1/1/2000#)<>#1/1/2000# THEN

Dim strMessage As String
Dim intOptions As Integer
Dim bytChoice As Byte


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70


strMessage = "RECORD SAVED! ARE YOU FINISHED?"
intOptions = vbQuestion + vbYesNo
bytChoice = MsgBox(strMessage, intOptions)


If bytChoice = vbYes Then
DoCmd.PrintOut
DoCmd.Close
Else
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl ("RECEIPT")
End If

ELSE
MSGBOX "Please fill in all the fields before clicking the button"
END IF


Exit_Command54_Click:
Exit Sub


Err_Command54_Click:
MsgBox Err.Description
Resume Exit_Command54_Click
End Sub


~J

PS. Change your command button name in properties, then choose the
"OnClick" event so you can have more meaningfully named subroutines.
Don't forget to use the proper prefixes (cmdValidate etc)
 
A

alex

Somtimes it's just faster to show (modify the if statement to match
your textboxes):

Private Sub Command54_Click()
On Error GoTo Err_Command54_Click

IF Nz(TEXTBOX1_TEXTFORMAT,"")<>"" AND Nz(TEXTBOX2_NUMBERFORMAT,0)<>0
AND Nz(TEXTBOX3_DATEFORMAT,#1/1/2000#)<>#1/1/2000# THEN

Dim strMessage As String
Dim intOptions As Integer
Dim bytChoice As Byte

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70

strMessage = "RECORD SAVED! ARE YOU FINISHED?"
intOptions = vbQuestion + vbYesNo
bytChoice = MsgBox(strMessage, intOptions)

If bytChoice = vbYes Then
DoCmd.PrintOut
DoCmd.Close
Else
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl ("RECEIPT")
End If

ELSE
MSGBOX "Please fill in all the fields before clicking the button"
END IF

Exit_Command54_Click:
Exit Sub

Err_Command54_Click:
MsgBox Err.Description
Resume Exit_Command54_Click
End Sub

~J

PS. Change your command button name in properties, then choose the
"OnClick" event so you can have more meaningfully named subroutines.
Don't forget to use the proper prefixes (cmdValidate etc)






- Show quoted text -

Thanks J for the advice.

alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top