dlookup

  • Thread starter Thread starter bobbeed
  • Start date Start date
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'.
 
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)
 
Back
Top