473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to compare two date fields in an Access Form?

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 15627
twinnyfo
3,653 Expert Mod 2GB
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
Thanks Twinnyfo - this is syntax I was looking for. Great stuff!
Nov 2 '14 #3
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 Expert Mod 2GB
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
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 Expert Mod 2GB
This code should be in the VBA module for the form that has the two text controls on it.
Nov 3 '14 #7
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 Expert Mod 2GB
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
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 Expert Mod 2GB
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
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 Expert Mod 2GB
I can only imagine that you don't have a text box named txtWarning.....
Nov 4 '14 #13
Thx twinnyfo got it...
Nov 4 '14 #14
twinnyfo
3,653 Expert Mod 2GB
Great to hear it! Let us know if you need anything else.
Nov 4 '14 #15
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 Expert Mod 2GB
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
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
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.