DLookup problems

B

Brad

Thanks for taking the time to read my question

Dim CellID as Variant 'When the DLookup works I'll change this to Integer
Dim ThePhoneNumber as String

CellID = DLookup("CellNumber", "tblCellInfo", "CellNumber = 555-555-5555")

returns Null

The value is there.

This works:
SELECT tblCellInfo.CellID, tblCellInfo.CellNumber
FROM tblCellInfo
WHERE (((tblCellInfo.CellNumber)="555-555-5555"));

I'd like to get this to work:

ThePhoneNumber = "555-555-5555"

CellID = DLookup("CellNumber", "tblCellInfo", "CellNumber = " & """" &
ThePhoneNumber & """")

What is wrong with my DLookup??? Both make CellID = Null

Thanks,

Brad
 
B

Brad

I figured it out.

Single Quotes

CellID = DLookup("CellID", "tblCellInfo", "CellNumber = '" & ThePhoneNumber
& "'")
 
F

fredg

Thanks for taking the time to read my question

Dim CellID as Variant 'When the DLookup works I'll change this to Integer
Dim ThePhoneNumber as String

CellID = DLookup("CellNumber", "tblCellInfo", "CellNumber = 555-555-5555")

returns Null

The value is there.

This works:
SELECT tblCellInfo.CellID, tblCellInfo.CellNumber
FROM tblCellInfo
WHERE (((tblCellInfo.CellNumber)="555-555-5555"));

I'd like to get this to work:

ThePhoneNumber = "555-555-5555"

CellID = DLookup("CellNumber", "tblCellInfo", "CellNumber = " & """" &
ThePhoneNumber & """")

What is wrong with my DLookup??? Both make CellID = Null

Thanks,

Brad

Well, if you already know the CellNumber is 555-555-5555 why do you
need to look it up? [CellID] = "555-555-5555" :-(

Anyway, it seems that the field [CellNumber] is a text datatype field,
in which case the number to be looked up must be enclosed within
quotes (you did this in your Select Where clause, ...
WHERE (((tblCellInfo.CellNumber)="555-555-5555")); ... why not here?).

Since the entire where clause in the DLookUp is within quotes, you use
single quotes around the string.

CellID = DLookup("CellNumber", "tblCellInfo", "CellNumber =
'555-555-5555'")

If the actual number is unknown, but is showing on a control on the
current record on the form, then you would use:
CellID = DLookup("CellNumber", "tblCellInfo", "CellNumber = '" &
Forms!NameOfForm!![ControlOnFormName] & "'")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top