DLookup

K

KBDB

In my VBA code I have a DLookup statement which is:

Private Sub custid_AfterUpdate()

Me.CustomerName = DLookup("[Customer Name]", "Customer File", _
"[Cust ID] = " & Me.CustID)

End Sub


I run this and I get this error:
Run Time Error 2471

The expression you entered as a query parameter produced this error:
'DT'

DT is the custID selected from the combo box.

I just don't understand what this error is trying to tell me.
Could you help?
Have you got any ideals?

Thanks,
Kathleen
 
F

fredg

In my VBA code I have a DLookup statement which is:

Private Sub custid_AfterUpdate()

Me.CustomerName = DLookup("[Customer Name]", "Customer File", _
"[Cust ID] = " & Me.CustID)

End Sub

I run this and I get this error:
Run Time Error 2471

The expression you entered as a query parameter produced this error:
'DT'

DT is the custID selected from the combo box.

I just don't understand what this error is trying to tell me.
Could you help?
Have you got any ideals?

Thanks,
Kathleen

The syntax needed is determined by the datatype of the criteria field.
If the value stored in the [Cust ID] field is DT, then it has to be
text. Your syntax was for a Number datatype.

Me.CustomerName = DLookup("[Customer Name]", "Customer File", _
"[Cust ID] ='" & Me.CustID & "'")

See VBA help on DLookup as well as
'Restrict data to a subset of records'
for more information on the proper syntax needed for the various
datatypes.
 
D

Dirk Goldgar

KBDB said:
In my VBA code I have a DLookup statement which is:

Private Sub custid_AfterUpdate()

Me.CustomerName = DLookup("[Customer Name]", "Customer File", _
"[Cust ID] = " & Me.CustID)

End Sub


I run this and I get this error:
Run Time Error 2471

The expression you entered as a query parameter produced this error:
'DT'

DT is the custID selected from the combo box.

I just don't understand what this error is trying to tell me.


It's trying to tell you that your [Cust ID] field is text, not a number, and
so any value you want to match it against must be enclosed in quotes. Try
this:


Me.CustomerName = _
DLookup("[Customer Name]", "Customer File", _
"[Cust ID] = '" & Me.CustID & "'")

Note that I've put single-quotes (') around the embedded CustID. That
should work so long as the CustID will never contain the single-quote
character.

By the way, your programming would be a lot easier if you didn't have spaces
in your field and table names. You wouldn't have to put square brackets
around all those names.
 
K

KBDB

Hi,

Something so simple can be so hard sometimes.
Thanks, I won't forget this one again.

-Kathleen

fredg said:
In my VBA code I have a DLookup statement which is:

Private Sub custid_AfterUpdate()

Me.CustomerName = DLookup("[Customer Name]", "Customer File", _
"[Cust ID] = " & Me.CustID)

End Sub

I run this and I get this error:
Run Time Error 2471

The expression you entered as a query parameter produced this error:
'DT'

DT is the custID selected from the combo box.

I just don't understand what this error is trying to tell me.
Could you help?
Have you got any ideals?

Thanks,
Kathleen

The syntax needed is determined by the datatype of the criteria field.
If the value stored in the [Cust ID] field is DT, then it has to be
text. Your syntax was for a Number datatype.

Me.CustomerName = DLookup("[Customer Name]", "Customer File", _
"[Cust ID] ='" & Me.CustID & "'")

See VBA help on DLookup as well as
'Restrict data to a subset of records'
for more information on the proper syntax needed for the various
datatypes.
 
K

KBDB

Hi,

Thank you. I knew it would be something so simple.

I have to put those spaces in so I can tell whats file stuff. I am just
starting to code so I get real confused alot. I'm sure after this project I
will do things different next time.

-Kathleen

Dirk Goldgar said:
KBDB said:
In my VBA code I have a DLookup statement which is:

Private Sub custid_AfterUpdate()

Me.CustomerName = DLookup("[Customer Name]", "Customer File", _
"[Cust ID] = " & Me.CustID)

End Sub


I run this and I get this error:
Run Time Error 2471

The expression you entered as a query parameter produced this error:
'DT'

DT is the custID selected from the combo box.

I just don't understand what this error is trying to tell me.


It's trying to tell you that your [Cust ID] field is text, not a number, and
so any value you want to match it against must be enclosed in quotes. Try
this:


Me.CustomerName = _
DLookup("[Customer Name]", "Customer File", _
"[Cust ID] = '" & Me.CustID & "'")

Note that I've put single-quotes (') around the embedded CustID. That
should work so long as the CustID will never contain the single-quote
character.

By the way, your programming would be a lot easier if you didn't have spaces
in your field and table names. You wouldn't have to put square brackets
around all those names.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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