Cancel = True not working

lmcc007

Registered User.
Local time
Today, 17:10
Joined
Nov 10, 2007
Messages
635
The Cancel is not working--when it set focus it does not undo the delete. I thought entering Cancel = True will undo the delete. Below is the code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If (Me.txtEventDate & "" = "") Then
If MsgBox("You deleted the date. Save anyway?", _
vbYesNo + vbQuestion + vbDefaultButton2, gstrAppTitle) = vbNo Then
Me.txtEventDate.SetFocus
Cancel = True​
Else
DoCmd.Close acForm, "fdlgEventDetail", acSavePrompt​
End If
End If
End Sub


The below code is not catching the wrong dates:

Private Sub txtEventDate_BeforeUpdate(Cancel As Integer)
If (txtEventDate) >= #1/1/1900# Then
MsgBox ("Event Date must be greater than 1/1/1900."), vbCritical, gstrAppTitle
Me.txtEventDate.SetFocus
Cancel = True
End If
End Sub

Thanks for your help!
 
The Cancel is not working--when it set focus it does not undo the delete. I thought entering Cancel = True will undo the delete.
If you want changes undone you have to use

Me.Undo

after the Cancel = True




The below code is not catching the wrong dates:

Private Sub txtEventDate_BeforeUpdate(Cancel As Integer)
If (txtEventDate) >= #1/1/1900# Then
MsgBox ("Event Date must be greater than 1/1/1900."), vbCritical, gstrAppTitle
Me.txtEventDate.SetFocus
Cancel = True
End If
End Sub
Well you are saying is not right. You have a test that is testing the value of the date and it generates the text message and all if the date is greater than 1/1/1900 and your message says it must be greater than that date. You would need to change it toi

If txtEventDate <= #1/1/1900# Then
 
Thanks Boblarson,

What does Cancel = True do then if you still have to put me.Undo?
 
Okay Boblarson,

I put this code on my field before entering a new record:

If txtEventDate <= #1/1/1969# Then
MsgBox ("Event Date must be greater than 1/1/1969."), vbCritical, gstrAppTitle
Me.txtEventDate.SetFocus ' Go back to Date field
Cancel = True​
End If

After I hit OK, I get the following error:

Error Number: 2108
Description: You must save the field before you execute the GotoControl action, the GoToControl method, or the SetFocus method
.

when I hit OK it goes to the next field instead of staying at the date field.
 
The two issues are 1) writing the update to disk and 2) undoing user keystrokes.
Cancel = true addresses only the first issue.
 

Users who are viewing this thread

Back
Top Bottom