473,509 Members | 11,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to compare two date fields in an Access Form?

12 New Member
I am having difficulty comparing two different date fields on my access form. Ex. If date two is less than date one... I want the system to display a msg... saying date two must be greater than date one.
Oct 31 '14 #1
16 15635
twinnyfo
3,653 Recognized Expert Moderator Specialist
Hi stress999,

Assuming you have some controls named similarly as below, you would have VBA behind each of your Date fields in the AfterUpdate Event. Your code would look similar to this:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub txtDate1_AfterUpdate()
  5.     VerifyDates
  6. End Sub
  7. Private Sub txtDate2_AfterUpdate()
  8.     VerifyDates
  9. End Sub
  10. Private Sub VerifyDates()
  11.     If Not (IsNull(Me.txtDate1) Or IsNull(Me.txtDate2)) Then
  12.         If Me.txtDate2 < Me.txtDate1 Then
  13.             Me.txtWarning = "Date 2 must be after Date 1"
  14.         Else
  15.             Me.txtWarning = ""
  16.         End If
  17.     End If
  18. End Sub
Normally I don't write the whole thing out, but I had a few minutes and this should get you started.

Hope this hepps!
Oct 31 '14 #2
stress999
12 New Member
Thanks Twinnyfo - this is syntax I was looking for. Great stuff!
Nov 2 '14 #3
stress999
12 New Member
Hi Twinnyfo,
I tried to insert the code above... for some reason it errors out. To test the exact code, I created 2 text boxes with the field names you used in the code.
The code was inserted from the "General" option.
Nov 2 '14 #4
twinnyfo
3,653 Recognized Expert Moderator Specialist
stress999,

First, I don't know what "General" option you are referring to.

Second, at what point of the code is it producing an error? This will help us to trouble shoot.
Nov 3 '14 #5
stress999
12 New Member
Twinnyfo,
I placed the code in the "General" option from the the drop down option where are the named fields are stored. It errors out at "Private Sub verifyDates() and "txtWarning.

Where should I placed the code? I have identical named field on the form as you created in the code: "txtDate1 and "txtDate2. I am not sure what is going on...
Nov 3 '14 #6
twinnyfo
3,653 Recognized Expert Moderator Specialist
This code should be in the VBA module for the form that has the two text controls on it.
Nov 3 '14 #7
stress999
12 New Member
Twinnyfo,

The error occurs at "Private Sub VerifyDate()" in the code below

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Private Sub txtDate1_AfterUpdate()
  4.     VerifyDates
  5. End Sub
  6.  
  7. Private Sub txtDate2_AfterUpdate()
  8.     VerifyDates
  9. End Sub
  10.  
  11. Private Sub VerifyDates()
  12.     If Not (IsNull(Me.txtDate1) Or IsNull(Me.txtDate2)) Then
  13.         If Me.txtDate2 < Me.txtDate1 Then
  14.             Me.txtWarning = "Date 2 must be after Date 1"
  15.  
  16.         Else
  17.             Me.txtWarning = ""
  18.         End If
  19.  
  20.     End If
  21. End Sub
Nov 3 '14 #8
twinnyfo
3,653 Recognized Expert Moderator Specialist
First, please use Code tags when posting Code.

What is the error that you are receiving? This will tell us how to troubleshoot.
Nov 3 '14 #9
stress999
12 New Member
The error occurred at line 10 and 15 in the original code you posted.
Line 15 kicks out compile error: "Method or data member not found" when click text box that represents "textDate2
Nov 3 '14 #10
twinnyfo
3,653 Recognized Expert Moderator Specialist
What are the names of the controls on your form. You must either 1) replace the control names in the code with the names of your controls or 2) rename your controls to match what is in the code.
Nov 3 '14 #11
stress999
12 New Member
Twinnyfo, the controls on the form are the same as the code above...txtDate1 and txtDate2.
I switched the control names. Now the compile error on LINES 14 states: Method or data member not found
Nov 4 '14 #12
twinnyfo
3,653 Recognized Expert Moderator Specialist
I can only imagine that you don't have a text box named txtWarning.....
Nov 4 '14 #13
stress999
12 New Member
Thx twinnyfo got it...
Nov 4 '14 #14
twinnyfo
3,653 Recognized Expert Moderator Specialist
Great to hear it! Let us know if you need anything else.
Nov 4 '14 #15
stress999
12 New Member
Thx a lot Twinnyfo
I would like to force the user to enter the correct dates. Not just a warning message when the dates are incorrect.
Nov 4 '14 #16
twinnyfo
3,653 Recognized Expert Moderator Specialist
There are lots of methods, preferences and options for doing that. It may be more appropriate to start a new thread for that discussion.

One option to begin with is to force the user to use the Date Picker when they select a date. This will work, but can be cumbersome if you have to enter a date from 12 years ago.

I always prefer to use an input mask. This will force the user to enter a Date in Date Format i.e., if you have the input mask set properly, and you want the user to enter the date in the format of "dd-mmm-yyyy" those are the only characters they can enter.

Once they enter the date (AfterUpdate Event), you compare that date to your required parameters--whatever they may be--and if the date meets those criteria, move along. If not, then force the user to try again.

Lots of ways to do this.....
Nov 4 '14 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5112
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
9
3186
by: Gleep | last post by:
sorry i didn't explain it correctly before my table is like this example fields: ID name username outcome date1 date2 date3 (etc..) - date15 price1 price2 price3 (etc..) I know that...
1
2625
by: Lauren Quantrell | last post by:
I have a whole bunch of forms that have an unbound StartDate and an EndDate field that I have used in MSAccess MDB databases as parameters in queries (ie tblEvent.StartDate >...
5
2366
by: Astra | last post by:
Hi All I have a <SELECT> for the month (1 .. 12) and a <SELECT> for the year (2004 .... 2020), do you know of any js validation check I can use to check whether these values are older than...
4
2479
by: Stewart Allen | last post by:
I'm trying to filter a table that has 2 date fields, the first date will always have a value but the second will only occasionally has a value. Each date field also has a corresponding text field...
1
1708
by: William Ortenberg | last post by:
With an Access 97 front-end, and a SQL Server 2000 back-end, I have a field on a form representing a SQL datetime column that I only want to see in a Short Date format (xx/xx/xxxx). I set the format...
1
10467
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
6
8125
by: Ledmark | last post by:
Hello - I am in a class for Access 2007 Database apllication design and we are covering types of Validation rules. We have a problem that I'm trying to solve but have no idea how to go about writing...
7
2020
by: buddyr | last post by:
Hello, txtbox one has a date. txtbox two has a number. number is number of days) txtbox three is empty. I have alot more going on but this is my question: can I add txtbox1 + txtbox2 and show...
2
5970
by: Chellie | last post by:
I am using Access 2007. I have two date fields (date receievd and date complete). I need to be able to run a query and find out who is not completing their work within two days of receipt. I tried...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7073
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.