Cancelling event not working

  • Thread starter Thread starter Applebaum
  • Start date Start date
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
 
The user is brought to this form after using another form which forces the
user to check and see if the vendor they're about to add to the database
already exists. If it does, they're brought to that vendor's record. If
not, they're brought to the data entry form, with Last Name already filled
out. The problem is, at this point the form has not been updated yet. The
record was added and the form opened, but nothing changed on the form yet.
They can still tab out without filling in First Name, and the Before Update
code won't get triggered.

Maybe I should do away with the previous form and incorporate it into this
one?

Thanks!

Matthew
 
Hi,
Okay , I think I follow what's happening.
How about checking in the tab's Click event (?)
or wherever your code is that loads the next subform?
Simply check to see if your FirstName control is empty,
if it is, don't load the next subform but bring the user back to the
FirstName control.
 
Thanks for the suggestion. I like that thought, but I can't figure out the
correct syntax. Here's what I tried:

If IsNull(Me.Child4!txtFirstName) Then
MsgBox "hi"
Else

Select Case (Me.TabCtl)
Case 0
' First tab selected
Me.Child4.SourceObject = "sbfrmFindPO"
....and so on...


It tells me it can't find the field "txtFirstName".

Is there a way to only use code when Child4.SourceObject = "frmVendors"?

Again, many thanks in advance for your thoughts.

Matthew
 
Back
Top