Adding a MsgBox when criteria are true

G

Gabriella777_2

I have a a form that contains 2 text boxes that are not used every time the
form is used. However, if the first box is used the second must be used. So
do I create a MsgBox to pop up based on an if then equation(?)?
The end result would be that when the user exits the second text box
(OnExit) it would look at the previous box and determine if the default value
($0.00) was changed (always a > 0 amount). If yes, and the exited text box
is still empty, then put up a pop up to remind that it must be filled in.
Does that makes sense or is there an easier way?
 
D

Douglas J. Steele

Since it's possible that they might not actually tab through the second
textbox, I'd recommend putting the logic in the form's BeforeUpdate Event:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.Textbox1 <> 0 And IsNull(Me.Textbox2) = True Then
MsgBox "You must provide a value for both Textbox1 and Textbox2"
Cancel = True
End If

End Sub
 
G

Gabriella777_2

Okay, I did as you suggested and it worked accept . . .
1. I got a pop up saying I can't go to the specified record after clicking
on the command button I created to go to the next record.
2. When I clicked the Close Form command button, I got the msgbox but after
clicking okay it closed the form anyway.
I added a SetFocus line to what you had me put in, with it setting Textbox 2
as the focus. That worked on the next record button but not on the close form
button.
--
Thanks and God bless you and yours,
Gabriella777_2



Douglas J. Steele said:
Since it's possible that they might not actually tab through the second
textbox, I'd recommend putting the logic in the form's BeforeUpdate Event:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.Textbox1 <> 0 And IsNull(Me.Textbox2) = True Then
MsgBox "You must provide a value for both Textbox1 and Textbox2"
Cancel = True
End If

End Sub
 
G

Gabriella777_2

Okay I put the code in the text box 2 OnExit rather than the form and it
worked perfectly. Just what I wanted it to do!
Thanks for the help!
--
Thanks and God bless you and yours,
Gabriella777_2



Douglas J. Steele said:
Since it's possible that they might not actually tab through the second
textbox, I'd recommend putting the logic in the form's BeforeUpdate Event:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.Textbox1 <> 0 And IsNull(Me.Textbox2) = True Then
MsgBox "You must provide a value for both Textbox1 and Textbox2"
Cancel = True
End If

End Sub
 
D

Douglas J. Steele

You must correct the error before you can proceed. If you decide you don't
want to save the erroneous record, hit the Esc key (you might have to hit it
twice).

It just occurred to me that the following would be safer:

If Nz(Me.Textbox1, 0) <> 0 And IsNull(Me.Textbox2) = True Then


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Gabriella777_2 said:
Okay, I did as you suggested and it worked accept . . .
1. I got a pop up saying I can't go to the specified record after clicking
on the command button I created to go to the next record.
2. When I clicked the Close Form command button, I got the msgbox but
after
clicking okay it closed the form anyway.
I added a SetFocus line to what you had me put in, with it setting Textbox
2
as the focus. That worked on the next record button but not on the close
form
button.
 

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

Top