Solved

MS-Access find closest date

Posted on 2012-03-14
11
529 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

777 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