close button causes on exit events (1 Viewer)

cadillac

Registered User.
Local time
Today, 10:48
Joined
May 9, 2001
Messages
34
in about 4 of my textboxes i have code in the on exit event to prevent the user from not entering data the code is a follows

If IsNull([Text90]) Then
MsgBox "You Must make an entry"
Me![Text90].SetFocus
DoCmd.CancelEvent
End If

my problem is that when i press the close button, i get the msgboxes popping up for each textbox that has this code. the form closes but i have to press ok on each of the msgboxes.

thanks
 

D-Fresh

Registered User.
Local time
Today, 15:48
Joined
Jun 6, 2000
Messages
225
Okay, two things.. First you should put the code in the BeforeUpdate event of the textboxes. If the value is null, then just set Cancel=true and it will not allow the user to leave the control and will prevent the msgboxes popping up on closing the form.

Secondly, you should use more descriptive names for your textboxes, i.e. ReceiveDate or First_Name. It makes the code a lot easier to understand and read when you look back. Just a quick tip for you. Hope that helps.

Doug
 

cadillac

Registered User.
Local time
Today, 10:48
Joined
May 9, 2001
Messages
34
here is the code i have now

Private Sub Text84_BeforeUpdate(Cancel As Integer)
If IsNull([Text84]) Then
Cancel = True
MsgBox "You Must make an entry"
Me![Text84].SetFocus

End If

is this what you ment? if so, it's not working. if not, what did you mean?
 

D-Fresh

Registered User.
Local time
Today, 15:48
Joined
Jun 6, 2000
Messages
225
First off, I'm sure you just forgot to copy it, but there is no End Sub. Also, you don't have to use SetFocus at the end, when you cancel the update, it will keep the focus on the control.

Doug
 

cadillac

Registered User.
Local time
Today, 10:48
Joined
May 9, 2001
Messages
34
that code isn't working. i can enter and exit the textbox without entering any information. maybe i did something wrong? i have the code in the before update section in all of the boxes and when i close the form i still get 4 msgboxes.

????
 

D-Fresh

Registered User.
Local time
Today, 15:48
Joined
Jun 6, 2000
Messages
225
Yeah, I'm sorry, I didn't realize what you were trying to do. The code I gave you won't fire unless if you update the field. So if you pop into it and right out, the event will never fire. You could always try putting the code in the BeforeUpdate event of the form. And just use an if statement down to check on the boxes

if isnull(me!field1) then
msgbox "You must enter Field1"
cancel = true
elseif isnull(me!field2) then
msgbox "You must enter Field2"
cancel = true
elseif isnull(me!field3) then
msgbox "You must enter Field3"
cancel = true
elseif isnull(me!field4) then
msgbox "You must enter Field4"
cancel = true
end if

This way, you'll only have 1 box pop up and the user has to enter data... The only problem with using BeforeUpdate is that the user has to change something else in the form in order for the event to fire. Hope that helps.

Doug

[This message has been edited by D-Fresh (edited 06-25-2001).]
 

cadillac

Registered User.
Local time
Today, 10:48
Joined
May 9, 2001
Messages
34
is there any way to automatically close those msgboxes when the form closes? (if i use my original code)
 

Users who are viewing this thread

Top Bottom