IF....Then..Else

G

Guest

Hi,
I have successfully created code that will cancel an event based on the
value of a control. However, after the Event is Cancelled, I would like a
MsgBox to display. I do not know the right code.

The following code is on the BeforeUpdate Event of a control chkBoxControl,
referencing the [txtCompleteDate] control.

If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
DoCmd.CancelEvent
End If

after DoCmd.CancelEvent, I would like
MsgBox (The reason the event has been cancelled)
Can that be done? I tried it and it didn't work for me.
Thank you
Capt OhNo
 
T

tina

If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
DoCmd.CancelEvent
End If

your code is saying
"if txtCompleteDate is null, then set its' value to Date.
but if txtCompleteDate is *not* null, then cancel the control's Update
event."
that means that if the user enters a date in the control, he/she cannot get
out of the control. the only way to exit the control is to press the Esc key
to undo his/her changes. is that really what you want to happen?

or are you simply trying to ensure that if the user deletes the date from
this control, the code will replace it with today's date? can you clarify
what you're trying to accomplish? (once we help you get that done, adding
the message box will be easy.)

hth
 
G

Graham Mandeno

Change DoCmd.CancelEvent to Cancel=True.

There is no reason why you should not call MsgBox from within a BeforeUpdate
event procedure - it is common practice! If you are still having trouble,
post all the code of the EP (including the MsgBox call.
 
F

fredg

Hi,
I have successfully created code that will cancel an event based on the
value of a control. However, after the Event is Cancelled, I would like a
MsgBox to display. I do not know the right code.

The following code is on the BeforeUpdate Event of a control chkBoxControl,
referencing the [txtCompleteDate] control.

If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
DoCmd.CancelEvent
End If

after DoCmd.CancelEvent, I would like
MsgBox (The reason the event has been cancelled)
Can that be done? I tried it and it didn't work for me.
Thank you
Capt OhNo

In the control's BeforeUpdate event:

If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
msgBox "The update is canceled because ...."
Cancel = True
End If
 
G

Guest

Thank you Fred - it works flawlessly.

fredg said:
Hi,
I have successfully created code that will cancel an event based on the
value of a control. However, after the Event is Cancelled, I would like a
MsgBox to display. I do not know the right code.

The following code is on the BeforeUpdate Event of a control chkBoxControl,
referencing the [txtCompleteDate] control.

If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
DoCmd.CancelEvent
End If

after DoCmd.CancelEvent, I would like
MsgBox (The reason the event has been cancelled)
Can that be done? I tried it and it didn't work for me.
Thank you
Capt OhNo

In the control's BeforeUpdate event:

If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
msgBox "The update is canceled because ...."
Cancel = True
End If
 
G

Guest

Tina,
Thanks for the reply. Fred provided the code but I will give you the
interpretation of what I wanted. The code was on a Chk Box. When the Chk
Box is clicked, the date is automatically filled into the date field. If
someone tries to un-check the check box, I do not want them to be able to and
I do not want the date to change in the date field. It may not be very
sophisticated logic, but I'm learning.
Thanks for your reply.
Capt OhNo

tina said:
If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
DoCmd.CancelEvent
End If

your code is saying
"if txtCompleteDate is null, then set its' value to Date.
but if txtCompleteDate is *not* null, then cancel the control's Update
event."
that means that if the user enters a date in the control, he/she cannot get
out of the control. the only way to exit the control is to press the Esc key
to undo his/her changes. is that really what you want to happen?

or are you simply trying to ensure that if the user deletes the date from
this control, the code will replace it with today's date? can you clarify
what you're trying to accomplish? (once we help you get that done, adding
the message box will be easy.)

hth


Captain OhNo said:
Hi,
I have successfully created code that will cancel an event based on the
value of a control. However, after the Event is Cancelled, I would like a
MsgBox to display. I do not know the right code.

The following code is on the BeforeUpdate Event of a control chkBoxControl,
referencing the [txtCompleteDate] control.

If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
DoCmd.CancelEvent
End If

after DoCmd.CancelEvent, I would like
MsgBox (The reason the event has been cancelled)
Can that be done? I tried it and it didn't work for me.
Thank you
Capt OhNo
 
T

tina

we're all still learning - in this case, i'm learning to read. <g> i
misread your original post. glad fred gave you the help you need. :)


Captain OhNo said:
Tina,
Thanks for the reply. Fred provided the code but I will give you the
interpretation of what I wanted. The code was on a Chk Box. When the Chk
Box is clicked, the date is automatically filled into the date field. If
someone tries to un-check the check box, I do not want them to be able to and
I do not want the date to change in the date field. It may not be very
sophisticated logic, but I'm learning.
Thanks for your reply.
Capt OhNo

tina said:
If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
DoCmd.CancelEvent
End If

your code is saying
"if txtCompleteDate is null, then set its' value to Date.
but if txtCompleteDate is *not* null, then cancel the control's Update
event."
that means that if the user enters a date in the control, he/she cannot get
out of the control. the only way to exit the control is to press the Esc key
to undo his/her changes. is that really what you want to happen?

or are you simply trying to ensure that if the user deletes the date from
this control, the code will replace it with today's date? can you clarify
what you're trying to accomplish? (once we help you get that done, adding
the message box will be easy.)

hth


Captain OhNo said:
Hi,
I have successfully created code that will cancel an event based on the
value of a control. However, after the Event is Cancelled, I would like a
MsgBox to display. I do not know the right code.

The following code is on the BeforeUpdate Event of a control chkBoxControl,
referencing the [txtCompleteDate] control.

If IsNull([txtCompleteDate]) Then
Me.[txtCompleteDate] = Date
Else
DoCmd.CancelEvent
End If

after DoCmd.CancelEvent, I would like
MsgBox (The reason the event has been cancelled)
Can that be done? I tried it and it didn't work for me.
Thank you
Capt OhNo
 

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