Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DLookup and Date Comparison

Status
Not open for further replies.

MightyRoo

Programmer
Jan 5, 2005
22
US
Hello all,

I'm having an issue with obtaining some data from a table that falls between a date range. The code is as follows:

CustDiscount = DLookup("[CustDisc]", "TblDiscounts", "[CustID]='" & CustomerID & "AND [EffectiveDate]< " & OrderDate & " AND [ExpirationDate]> '" & OrderDate & "'")

Essentially I would like to be able to obtain a number by looking up from a table by comparing a date range and an identifier and use that number as a variable.

Any feedback or thoughts on how to obtain the data out of the table would be greatly appreciated.

Thanks
 
You could try this version which has not been tested.
Access wants # to enclose Date datatype fields. You are missing a ' after CustomerId before the AND

Code:
CustDiscount = DLookup("[CustDisc]", "TblDiscounts", "[CustID]='" & CustomerID & "' AND [EffectiveDate]< #" & OrderDate & "# AND [ExpirationDate]> #" & OrderDate & "#")
 
Whenever I need to use dates in a SQL statement or a DLookup, I use a simple function:

Code:
Function SQLDate(datDate As Date) As String
    SQLDate = "#" & Format(datDate, "mm/dd/yyyy") & "#"
End Function

Then you can change your statement to this:

Code:
CustDiscount = DLookup("[CustDisc]", "TblDiscounts", "[CustID]='" & CustomerID & "AND [EffectiveDate]< " & SQLDate(OrderDate) & " AND [ExpirationDate]> " & SQLDate(OrderDate))

 
Thank you both for your reply. Axworthy, your code did what I wanted. Thank you for that little trick.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top