Dlookup Question

J

John W

I'm sure this is an easy one but I can't seem to figure out my problem. I
have a form("Main") with a listbox ("Payer") and a table("Data") with the
columns "PayerName" & "Comment". I'm attempting to use Dlookup to display a
message box that displays the "Comment" field for the payer chosen from the
list box. Here's what I'm trying:

dim myComment as string

myComment=DLookup("[Comment]","Data","[PayerName] = " & Forms![Main]!Payer)
msgbox mycomment

I keep getting an error saying I have cancelled the last operation? Not
sure what I'm missing here.

Thanks!
 
D

Douglas J. Steele

That very misleading error message can indicate that your DLookup statement
has a mistyped field (or table) name in it.

As well, what data type is PayerName? From the name of the field, I'd expect
it to be text, which means that the value needs to be put inside quotes:

myComment=DLookup("[Comment]","Data", _
"[PayerName] = """ & Forms![Main]!Payer & """")

(That's three double quotes in front, and four double quotes after.
Ordinarily, I'd use single quotes as a delimiter, but that'll fail on names
with apostrophes in them, such as O'Riley)
 
J

John W

Okay - it works now. PayerName is a text field. I added the extra quotes
and I did have a spelling error. I swear I looked at it at least 5 times
checking for spelling errors. Must have something wrong with my eyes today!

Thanks for the help!
Douglas J. Steele said:
That very misleading error message can indicate that your DLookup
statement has a mistyped field (or table) name in it.

As well, what data type is PayerName? From the name of the field, I'd
expect it to be text, which means that the value needs to be put inside
quotes:

myComment=DLookup("[Comment]","Data", _
"[PayerName] = """ & Forms![Main]!Payer & """")

(That's three double quotes in front, and four double quotes after.
Ordinarily, I'd use single quotes as a delimiter, but that'll fail on
names with apostrophes in them, such as O'Riley)


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


John W said:
I'm sure this is an easy one but I can't seem to figure out my problem.
I have a form("Main") with a listbox ("Payer") and a table("Data") with
the columns "PayerName" & "Comment". I'm attempting to use Dlookup to
display a message box that displays the "Comment" field for the payer
chosen from the list box. Here's what I'm trying:

dim myComment as string

myComment=DLookup("[Comment]","Data","[PayerName] = " &
Forms![Main]!Payer)
msgbox mycomment

I keep getting an error saying I have cancelled the last operation? Not
sure what I'm missing here.

Thanks!
 
W

windowslive messenger

John W said:
I'm sure this is an easy one but I can't seem to figure out my problem. I
have a form("Main") with a listbox ("Payer") and a table("Data") with the
columns "PayerName" & "Comment". I'm attempting to use Dlookup to display
a message box that displays the "Comment" field for the payer chosen from
the list box. Here's what I'm trying:

dim myComment as string

myComment=DLookup("[Comment]","Data","[PayerName] = " &
Forms![Main]!Payer)
msgbox mycomment

I keep getting an error saying I have cancelled the last operation? Not
sure what I'm missing here.

Thanks!
 

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