Solved

MS-Access find closest date

Posted on 2012-03-14
11
541 Views
Last Modified: 2012-03-14
Hello experts, I have a tricky problem with trying to find the closest date that I need some help with.

When a user enters a date in a text box, I would like to find the nearest date in a table, the closest future date if available otherwise the closest past date

e.g if the text box value is 29-Feb-2012 then I would like to return the value 02-Mar-2012 from the table [TBL_Dates].[Upload_Date] below.


[TBL_Dates].[Upload_Date]
17-Feb-2012
24-Feb-2012
02-Mar-2012
09-Mar-2012


Thanks for any help
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 37721063
You can use a VBA function -- this assumes that your table is populated (not empty):

Dim varNextHigher as Variant
Dim varNextLower as Variant

varNextHigher = DMin("Upload_Date", "tbl_Dates", "Upload_Date > #" & txtDate & "#")
varNextLower = DMin("Upload_Date", "tbl_Dates", "Upload_Date < #" & txtDate & "#")

msgbox "Closest Date = " & Nz(varNextHigher,NextLower)
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37721105
for the nextlower use dmax instead of dmin


varNextLower = DMax("Upload_Date", "tbl_Dates", "Upload_Date < #" & txtDate & "#")
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37721184
Yup - that was a copy/paste mistake in my post.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:lancegallagher_expertsexchange
ID: 37721211
Hi, having trouble getting it to work for me

I find that I have to enter the date in American date format

e.g if I enter 08/03/2012 and run the procedure I get closest date =

however if I enter 03/08/2012 and run the procedure I get closest date = 09/03/2012
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37721219
Try this:

Dim varNextHigher as Variant
Dim varNextLower as Variant

varNextHigher = DMin("Upload_Date", "tbl_Dates", "Upload_Date > #" & cDate(txtDate) & "#")
varNextLower = DMax("Upload_Date", "tbl_Dates", "Upload_Date < #" & CDate(txtDate) & "#")

msgbox "Closest Date = " & Nz(varNextHigher,NextLower)
0
 

Author Comment

by:lancegallagher_expertsexchange
ID: 37721274
Hi, still the same problem when using

varNextHigher = DMin("Upload_Date", "tbl_Dates", "Upload_Date > #" & cDate(txtDate) & "#")
varNextLower = DMax("Upload_Date", "tbl_Dates", "Upload_Date < #" & CDate(txtDate) & "#")

what do you think I should try next?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37721285
Is this a true Date field, or is it text?

And what format do the values appear in the table itself?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37721310
You can also try formatting the dates identically:

varNextHigher = DMin("Upload_Date", "tbl_Dates", "Format('yyyy-mm-dd', Upload_Date) > '" & Format("yyyy-mm-dd", (txtDate) & "'")
varNextLower = DMax("Upload_Date", "tbl_Dates", "Format('yyyy-mm-dd', Upload_Date) < '" & Format("yyyy-mm-dd", (txtDate) & "'")

Open in new window

0
 

Author Comment

by:lancegallagher_expertsexchange
ID: 37721325
Hi,

The text box txtDate is format as short date . The format of Upload_Date is short date too.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 37721333
Sorry- I've got the dates and format strings backwards;

varNextHigher = DMin("Upload_Date", "tbl_Dates", "Format(Upload_Date, 'yyyy-mm-dd') > '" & Format((txtDate) ,"yyyy-mm-dd") & "'")
varNextLower = DMax("Upload_Date", "tbl_Dates", "Format(Upload_Date, 'yyyy-mm-dd') < '" & Format((txtDate) ,"yyyy-mm-dd") & "'")

Open in new window

0
 

Author Closing Comment

by:lancegallagher_expertsexchange
ID: 37721375
Great, that works a treat. Thanks very much for your help and quick response.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Looking for advice on how to develop a project database 2 47
Menus 6 55
Why does the filterbox look different in different fields? 5 41
Cant delete records in query 8 48
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…

732 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