473,395 Members | 1,680 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,395 software developers and data experts.

using Dlookup with dates and > sign

reginaldmerritt
201 100+
I'm using Dlookup to search a table for the first record it comes across that has a date >= the date i specifiy.

e.g.

Expand|Select|Wrap|Line Numbers
  1. formateddate = format(Me.SelectedDate,"mmddyy")
  2.  
  3. VarX = DLookup("[PKDateField]", "tbChargeRate", "[PKDateField] >=  #" & formateddate & "#")
The problem i have is that there maybe several records in tbChargeRate that have PKDateField > Me.SelectedDate. But i only want the first one it comes across, which is what i thought DLookup did.It does not act as i think it should.


I have four test records in tbChargeRate with PKDateField

Record1 : 01/08/06
Record2 : 01/09/06
Record3 : 01/04/07
Record4 : 07/06/08


These are some test results
If i use Me.SelectedDate as 01/05/07 DLookup returns Record1(01/06/07) correct
If i use Me.SelectedDate as 01/01/05 DLookup returns Record1(01/04/07) wrong
if i use Me.SelectedDate as 01/05/07 DLookup returns Record1(07/06/08) correct

As you can see when using 01/01/05 should return Record1, but instead it returns Record3.

I'm very confused on whats going on here, any suggestions and help welcome.
Nov 25 '07 #1
3 2405
ADezii
8,834 Expert 8TB
I'm using Dlookup to search a table for the first record it comes across that has a date >= the date i specifiy.

e.g.

Expand|Select|Wrap|Line Numbers
  1. formateddate = format(Me.SelectedDate,"mmddyy")
  2.  
  3. VarX = DLookup("[PKDateField]", "tbChargeRate", "[PKDateField] >=  #" & formateddate & "#")
The problem i have is that there maybe several records in tbChargeRate that have PKDateField > Me.SelectedDate. But i only want the first one it comes across, which is what i thought DLookup did.It does not act as i think it should.


I have four test records in tbChargeRate with PKDateField

Record1 : 01/08/06
Record2 : 01/09/06
Record3 : 01/04/07
Record4 : 07/06/08


These are some test results
If i use Me.SelectedDate as 01/05/07 DLookup returns Record1(01/06/07) correct
If i use Me.SelectedDate as 01/01/05 DLookup returns Record1(01/04/07) wrong
if i use Me.SelectedDate as 01/05/07 DLookup returns Record1(07/06/08) correct

As you can see when using 01/01/05 should return Record1, but instead it returns Record3.

I'm very confused on whats going on here, any suggestions and help welcome.
The Format() Function returns a Variant of Type String which means you may be comparing a String Value with leading and trailing # signs. Explicitly Declare formateddate as a Date Variable, then it should be able to be coerced to a Date for comparisons:

Expand|Select|Wrap|Line Numbers
  1. Dim formateddate As Date
  2. formateddate = format(Me.SelectedDate,"mmddyy")
  3.  
  4. VarX = DLookup("[PKDateField]", "tbChargeRate", "[PKDateField] >=  #" & formateddate & "#")
Nov 26 '07 #2
reginaldmerritt
201 100+
The Format() Function returns a Variant of Type String which means you may be comparing a String Value with leading and trailing # signs. Explicitly Declare formateddate as a Date Variable, then it should be able to be coerced to a Date for comparisons:

Expand|Select|Wrap|Line Numbers
  1. Dim formateddate As Date
  2. formateddate = format(Me.SelectedDate,"mmddyy")
  3.  
  4. VarX = DLookup("[PKDateField]", "tbChargeRate", "[PKDateField] >=  #" & formateddate & "#")
If i declare formateddate as a Date type, it gives a run time error - "type mismatch"
Nov 28 '07 #3
ADezii
8,834 Expert 8TB
If i declare formateddate as a Date type, it gives a run time error - "type mismatch"
  1. Are you sure [PKDateField] is defined as a Date Data Type in the Table?
  2. If it is, and you're still getting the Error, try the conversion within DLookup() as in:
    Expand|Select|Wrap|Line Numbers
    1. VarX = DLookup("[PKDateField]", "tbChargeRate", "[PKDateField] >=  #" & CDate(formateddate) & "#")
  3. This is strange since I had no trouble obtaining the proper results.
  4. Let me know how you make out.
Nov 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sue | last post by:
I sent an e-mail below and got a responese but i still have the #error message in the subject below: Can someone help me: questions and replies are separted by ----- -------My Questions: I have...
4
by: ShyGuy | last post by:
I have a table with 4 fields. Three are used for criteria. I can get the DLookup to work with 1 criteria with the following but can't get it to work with 2 or three. NumofAppts = DLookup("",...
4
by: thx606 | last post by:
Hello, I'm having problems figuring this one out. I have 2 combo boxes. The first one the user selects a professor then the second one they select a course and that works fine. Here is my...
1
by: Colin Mardell | last post by:
Can anyone tell me why this VBA returns a null value when the source table doesn't have one? I have a table called 'zTerms' with fields 'TermID', 'Term', 'TermStartDate' and 'TermEndDate' I...
8
by: Christine Henderson | last post by:
I have a problem using the above function in the following simplified circumstance: In the lookup table called "Klms Travelled" I have 3 fields, eg: Receiver Name Receiver Suburb ...
6
by: bjaj | last post by:
Hi How do I use a boolean criterian with the funktion DLookup ? I know the syntax for strings, numeric and date as follows For numerical values: DLookup("FieldName" , "TableName" ,...
1
by: BobM | last post by:
i read the thread entitled "Convert Field Value String to Actual Field in Expression" It appeared that the final solutions got moore complex not less complex MY PROBLEM IS: 1. get a value from...
2
by: boyleyc | last post by:
Hi all the following code works perfectly well. Basically it populates a series of check boxes on my form, depending on whether dlookup finds an associated record. The problem i have is that...
4
by: JHNielson | last post by:
I have a query that I'm trying to update with a dlookup with multiple criteria This is the string: EVNT_DT: DLookUp("","","( .EVNT_QTR=.) & (.=.)") When i run it it says it can't find the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.