Problem with DLookup cutting off the last character of the criteria

Thirith

Board Regular
Joined
Jun 9, 2009
Messages
120
Office Version
  1. 365
Platform
  1. Windows
On my main form, I've got a date field that should update another field based on a range of dates. The field is called txtDateSubmissionPhase1 and it should look up information in the table tblStageLookup, which has these relevant columns: StageDescription and StageEndDate.

This is the DLookup code I'm using:
Code:
varTest = DLookup("[StageDescription]", "tblStageLookup", "[StageEndDate] <= # " & [txtDateSubmissionPhase1] & "#")

However, weirdly I keep getting an error - and in this error it looks like the final character (in this case the second #) is omitted from the criteria.

Can anyone here tell me why this is happening and how I can avoid it? It seems a pretty strange thing for the code to do, but I have to admit that I'm always a bit at a loss with respect to code that needs to be in string format.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I can't see why that would happen.

Does this make a difference?
Code:
strCrit =  "[StageEndDate] <= # " & [txtDateSubmissionPhase1] & "#"

Debug.Print strCrit

varTest = DLookup("[StageDescription]", "tblStageLookup",strCrit)

P.S. You can see the value of strCrit in the Immediate Window (CTRL+G).
 
Upvote 0
How utterly weird... I tried the code you posted (taking out the space after the first #), with the following results: 1) The value of strCrit is [StageEndDate] <= #01.01.2019#, but I then get the following error: "Syntax error in date in query expression '[StageEndDate] <= #01.01.2019". It's like DLookup is waiting at the end of the expression with a big knife in order to cut off that final character...
 
Upvote 0
Perhaps the problem is with the way your date is formatted?
 
Upvote 0
It's possible, but I wouldn't know where the problem lies. All the relevant dates are formatted in the same format, which is the default here (dd.mm.yyyy).
 
Upvote 0
Okay, I'm not quite sure how I got down the garden path of strings, hashes and the like - but I now went back to what I thought I'd tried to begin with, and it works:

Code:
Stage = DLookup("[StageDescription", "tblStageLookup", "[StageEndDate] >= [Forms]![frmSPIRITapplications]![txtDateSubmissionPhase1]")

And it works. I'm not entirely certain what I did wrong the first time, but it was probably something minor and I mistakenly thought it was some big underlying issue, which sent me on a wild goose chase. In any case, it works now and does exactly what it needs to do. Yay! Nonetheless, thanks for your help, which kept me looking for possible solutions.
 
Upvote 0
You're right, that might be it. Though I'm not sure it's the only thing, since that in itself shouldn't result in a syntax error, should it?
 
Upvote 0
No, I am not saying that.
I am saying you have perhaps inadvertently changed your criteria trying to get to the bottom of this.?
I *thought* it might be the space after the initial #, as that does not work, but you say you removed that. I tried something similar without the space and it worked fine.?
 
Upvote 0
The >=/<= issue most likely was down to me making mistakes when retyping the code for my post. (Since it wasn't line after line of VBA code, I didn't just copy and paste.)

I'm still weirded out by the underlying problem; it's puzzling to me why a string would display correctly using Debug.Print, but that same string used in conjunction with DLookup giving me an error that indicates that the last character was omitted? That's plain weird.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top