dlookup

B

bobbeed

Can anyone please explain to me, in simple terms(I am on the limit of
what I understand with this question) why the first will work, but the
second will not.


WORKS
Private Sub Command1_Click()
Dim varX As Variant

varX = DLookup("[CompanyName]", "Shippers", "[ShipperID] = 2")
MsgBox varX
End Sub


DOESN'T WORK
Private Sub Command1_Click()
Dim varX As Variant
Dim intNumber As Integer

intNumber = 2
varX = DLookup("[CompanyName]", "Shippers", "[ShipperID] = intNumber ")
MsgBox varX
End Sub

The second stops with the following message:

Runtime error 2471:
The expression you entered as a query parameter produced this error:
'The object doesn't contain the Automation object 'intNumber'.
 
R

Rick Brandt

bobbeed said:
Can anyone please explain to me, in simple terms(I am on the limit of
what I understand with this question) why the first will work, but the
second will not.


WORKS
Private Sub Command1_Click()
Dim varX As Variant

varX = DLookup("[CompanyName]", "Shippers", "[ShipperID] = 2")
MsgBox varX
End Sub


DOESN'T WORK
Private Sub Command1_Click()
Dim varX As Variant
Dim intNumber As Integer

intNumber = 2
varX = DLookup("[CompanyName]", "Shippers", "[ShipperID] = intNumber ")
MsgBox varX
End Sub

The variable name needs to be outside the quotes. Otherwise you are looking for
a ShipperID equal to some other field in the Shippers table named "intNumber".

varX = DLookup("[CompanyName]", "Shippers", "[ShipperID] = " & intNumber)
 

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