Solved

Enhancement to my Code to Check if a Date is Greater Than the Current Date

Posted on 2013-06-19
9
530 Views
Last Modified: 2013-06-24
I have code that checks a date in a field to see if it is less than or equal to the current date.  I have it on the BeforeUpdate Event of the Field, so the focus automatically goes back to this field, but the old date that the user entered is still sitting there.

After the cursor's focus is back in that field, I would like to have the field wipe out the old date (clear the field) and be blank and ready for the user to enter the correct date.

When I tried putting in the following line of code: me.Quality_Review_Date.value = ""

after the line of code "Cancel=True", I get run-time error '2115'.

Below is the code I'm using to check the Date field:
If Me.Quality_Review_Date.Value > (Date) Then
    MsgBox ("You must enter a date less than or equal to today's date!")
    Cancel = True
End If

Open in new window

What do I need to do to accomplish this?

Thanks,
gdunn59
0
Comment
Question by:gdunn59
9 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39261637
Hi,

And if you put the line before Cancel = True

Regards
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 300 total points
ID: 39261706
This will do:
If Me!Quality_Review_Date.Value > Date Then
    MsgBox ("Please enter a date less than or equal to today's date.")
    Me!Quality_Review_Date.Undo
    Cancel = True
End If

Open in new window

/gustav
0
 

Author Comment

by:gdunn59
ID: 39262897
cactus_data:

I tried it both before and after, same issue.

Thanks,
gdunn59
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 39263124
Is the code running from the BeforeUpdate event of the textbox with the date?  That is where it should be.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:gdunn59
ID: 39263511
Helen_Feddema:

Yes, it is.

Thanks,
gdunn59
0
 

Author Comment

by:gdunn59
ID: 39271992
cactus_data:

Sorry, I didn't see in your posting that you had put in the "Undo" command.  I pasted in your code from ID: 39261706, and it worked.  It places the cursor back in the Date field, with the date highlighted, the only thing is it doesn't wipe out the date that is already there.

Is there a way around this to have the field get wiped out all together?  If not, that's fine.  At least the field/date is highlighted and the user can just type and it will wipe out the existing date with the new date the user types in.

Thanks,
gdunn59
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39272070
OK, that explains.

And no, Undo will not wipe out anything, just revert a change.

If that isn't enough, you will have to use the AfterUpdate event but this will, of course, only trig if data has been changed, and that is what you wish to prevent.

/gustav
0
 

Author Comment

by:gdunn59
ID: 39272078
cactus_data:

Ok.  I guess the way it is will work.

Thanks for your assistance.

gdunn59
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39272098
You are welcome!

/gustav
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now