A
Applebaum
Hey, thanks again for the prompt help with the last post.
I have a main form with a subform. This subform has some required fields.
The user should not be able to leave the subform if either of these fields
are empty. I've done this successfully in another database, but in that
context the form was not a subform, that's the only difference I can
discern. I'm able to get my messagebox to come up allright, but clicking
okay doesn't return the user to the problem.
The main form is designed with tabs and a single subform; the subform's
source changes according to the tab selected.
The subform is for filling out a person's name information. FirstName and
LastName are required.
If one of those fields is missing, and the user selects a different tab, the
message box comes up, the user presses OK, but then the next tab's subform
loads anyway.
I have the following code:
Private Sub Form_Unload(Cancel As Integer)
If IsNull(Me.txtFirstName) Then
DoCmd.CancelEvent
Cancel = True
MsgBox "When adding a person, you must fill in First Name"
Else
If IsNull(Me.txtLastName) Then
Cancel = True
MsgBox "When adding a person, you must fill in Last Name"
End If
End If
'I'm not sure if the following is necessary?
If Cancel Then
strMsg = strMsg & vbCrLf & _
"Correct the entry, or press <Esc> twice to undo."
MsgBox strMsg, vbExclamation, "Invalid entry"
DoCmd.CancelEvent
End If
End Sub
I don't know the difference between Cancel = True and DoCmd.CancelEvent, and
I'm not sure what is required to cancel the intended action of selecting a
different tab on the main form, thus changing the subform's source.
Again, many thanks in advance!
Matthew
I have a main form with a subform. This subform has some required fields.
The user should not be able to leave the subform if either of these fields
are empty. I've done this successfully in another database, but in that
context the form was not a subform, that's the only difference I can
discern. I'm able to get my messagebox to come up allright, but clicking
okay doesn't return the user to the problem.
The main form is designed with tabs and a single subform; the subform's
source changes according to the tab selected.
The subform is for filling out a person's name information. FirstName and
LastName are required.
If one of those fields is missing, and the user selects a different tab, the
message box comes up, the user presses OK, but then the next tab's subform
loads anyway.
I have the following code:
Private Sub Form_Unload(Cancel As Integer)
If IsNull(Me.txtFirstName) Then
DoCmd.CancelEvent
Cancel = True
MsgBox "When adding a person, you must fill in First Name"
Else
If IsNull(Me.txtLastName) Then
Cancel = True
MsgBox "When adding a person, you must fill in Last Name"
End If
End If
'I'm not sure if the following is necessary?
If Cancel Then
strMsg = strMsg & vbCrLf & _
"Correct the entry, or press <Esc> twice to undo."
MsgBox strMsg, vbExclamation, "Invalid entry"
DoCmd.CancelEvent
End If
End Sub
I don't know the difference between Cancel = True and DoCmd.CancelEvent, and
I'm not sure what is required to cancel the intended action of selecting a
different tab on the main form, thus changing the subform's source.
Again, many thanks in advance!
Matthew