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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using string variable for Dlookup criteria 1

Status
Not open for further replies.

inso18

Technical User
Dec 30, 2006
147
IL
Hi All.

What's the correct syntax for
Code:
Dim strS as String
strS = "Mike"
Msgbox Dlookup (field.name, "tblTable", "field1=" & strS)
This doesn't work, I can't figure out the correct one maybe because understand the rules behind all the " & ' signs when using these stuff, and haven't found any reading source for learning this issue.
 
Strings need single quotes:

[tt]Msgbox Dlookup (field.name, "tblTable", "field1='" & strS & "'")[/tt]

This field.name looks wrong to me. You need the name of a field or function as a string.
 
A safer way:
Code:
MsgBox DLookup("[field name]", "tblTable", "field1='" & Replace(strS, "'", "''") & "'")


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks a lot.

(the field.name is actually a recordset item.)

Can you please explain the logic behind this syntax? Why there have to be ' ' brackets and & & and " "?
 
is the VBA literal constant delimiter
& is the VBA string concatenation operator
' is the SQL literal constant delimiter

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Ok, and why do they have to be in this specific order in this case?
 
In this case ?
[tt]"field1='" & strS & "'"[/tt]

The goal is to send to the function the following WHERE clause as a literal constant:
[tt]field1='Mike'[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Ahh, I think I understand now.

This is something further on topic, how the correct result should be achieved in a query creteria field, if I it is, for ex:

[ClientID] is a number
IsNull(DCount("CustomerID","Customers","CustomerID="&[CustomerID]))

the criteria here works, but not correctly (When I manualy write a number "CustomerID=1", it does work correctly.

I'll appritiate if you can explain this too.
 
Sorry, I don't see what to explain as you don't say what but not correctly means.
What is [ClientID] in that context ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Oops, it is supposed to be [Customer_Id].
 
Anyway, I don't think the DCount function may return a Null value ...
 
My fault, it does work.
After this post I understand more about how to correctly combine delimiters. Thanks PHV and Remou.
 
Yeah, afterwards I used a ="0" instead of IsNull.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top