Avatar of 4charity
4charity

asked on 

Undo Control Not working

I am trying to put some code on a date field, so that a date in the future cannot be accidentally typed in. If the date is in the future, I want a message box to appear, and the control to be cleared for a new (correct) date entered. The message box pops up, but the control doesn't clear. Any suggestions?


Private Sub InvoiceDate_BeforeUpdate(Cancel As Integer)

If Me.InvoiceDate > Now() Then
MsgBox "Invoice Date Cannot be in the future. Please adjust."
Me!InvoiceDate.Undo

End If

End Sub
Microsoft Access

Avatar of undefined
Last Comment
Emil_Gray
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Is this a Bound control?  If not, UnDo only works on Bound controls.

mx
Avatar of 4charity
4charity

ASKER

Yes, it is bound to a field in the underlying table.
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Bound ... ok:


Private Sub InvoiceDate_BeforeUpdate(Cancel As Integer)

If Me.InvoiceDate > Date() Then    ' *** compare against Date()
MsgBox "Invoice Date Cannot be in the future. Please adjust."
    Cancel = True ' Add this ***********
    Me!InvoiceDate.Undo

End If

End Sub
Avatar of 4charity
4charity

ASKER

OK - I added the Cancel line and switched to Date instead of Now.
Still doesn't undo.
SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of 4charity
4charity

ASKER

If Me.InvoiceDate > Date Then
MsgBox "Invoice Date Cannot be in the future. Please adjust."
Cancel = True
Me!InvoiceDate.Undo

End If
Does Me.InvoiceDate include Time?
Is InvoiceDate bound to a real Date/Time data type ?

Are you getting the Message box prompt ?

mx
Avatar of 4charity
4charity

ASKER

Yes.

This works:

If Me.InvoiceDate > Date Then
MsgBox "Invoice Date Cannot be in the future. Please adjust."
Cancel = True
Me!InvoiceDate.Undo
Undo
End If
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Charity,

You should not need the Undo line, and it may cause an undo for the entire record, not just for the current control.

Dale
But  Undo will undo the entire form ... ?

I see this form time to time ... Me.someControl.Undo just flat ass does not work ... not sure why.

mx
Sometimes reversing makes it work:

If Me.InvoiceDate > Date Then
MsgBox "Invoice Date Cannot be in the future. Please adjust."
Me!InvoiceDate.Undo
Cancel = True
End If
Avatar of 4charity
4charity

ASKER

In checking for it to work, you have to make sure you really change the date alot, so that you realize it went back to the previous date, not a null field. this does work:

If Me.InvoiceDate > Date Then
MsgBox "Invoice Date Cannot be in the future. Please adjust."
Cancel = True
Me!InvoiceDate.Undo
End If
That almost always works ... unless something really weird is going on.

mx
Avatar of Emil_Gray
Emil_Gray
Flag of United States of America image

This should be done as an AfterUpdate action.

Private Sub InvoiceDate__AfterUpdate()

If Me.InvoiceDate > Date Then
MsgBox "Invoice date cannot be in the future. Please adjust.
Me.InvoiceDate = Null
Me.Refresh
End If

End Sub
Well, AfterUpdate is After the Fact ... control has already be updated.  That's why it's called "After' Update.  The correct event for any validation is the Before Update event, either at the Control level or Form level.

mx
Avatar of Emil_Gray
Emil_Gray
Flag of United States of America image

DatabaseMX, you're right. That is the best way but it appeared that he was having problems with the BeforeUpdate action so I just was suggesting an alternative solution. Another suggestion might be to use the Validation Rule as an input limiter.
I think she got it to work >> ". this does work"

mx
SOLUTION
Avatar of Emil_Gray
Emil_Gray
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo