Solved

MS-Access find closest date

Posted on 2012-03-14
11
532 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
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copied Report Does not run on a different Query Source ? 7 33
set focus to tabbed sub form 10 39
Access Schema 6 26
Question about formatting a report 11 21
This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

828 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