PC Review


Reply
Thread Tools Rate Thread

DLookup not recognizing variable

 
 
Don Barton
Guest
Posts: n/a
 
      29th Dec 2010
I am using MS Access 2007 with Vista home premium
The user selects a value (string) from a combobox. Hitting tab to the
next control fires an on Got Focus event.

Dim TestMnem As String, TestName As Variant

TestMnem = Me.[LOTestMnemonic]

TestName = DLookup("[LabTestName]", "tblLabTests", "[LabTestMnemonic]
= " & TestMnem)

Me.LOTestName = TestName

TestMnem returns the desired string value as selected on my form. Let
say it is "CHEMSC"
Next I use this the variable TestMnem in the DLookup criteria
expression above.
I get the following error each time: "The expression you entered as a
query parameter produced this error: *value of TestMnem*

From everything I've looked at this should work. If I change
"[LabTestMnemonic] = " & TestMnem) to "[LabTestMnemonic] =
'CHEMSC'"), the event works correctly.


Don
LabDBDesigns
 
Reply With Quote
 
 
 
 
Don Barton
Guest
Posts: n/a
 
      30th Dec 2010
On Dec 29, 5:55*pm, Marshall Barton <marshbar...@wowway.com> wrote:
> Don Barton wrote:
> >I am using MS Access 2007 with Vista home premium
> >The user selects a value (string) from a combobox. *Hitting tab to the
> >next control fires an on Got Focus event.

>
> >Dim TestMnem As String, TestName As Variant

>
> >TestMnem = Me.[LOTestMnemonic]

>
> >TestName = DLookup("[LabTestName]", "tblLabTests", "[LabTestMnemonic]
> >= " & TestMnem)

>
> >Me.LOTestName = TestName

>
> >TestMnem returns the desired string value as selected on my form. Let
> >say it is "CHEMSC"
> >Next I use this the variable TestMnem in the DLookup criteria
> >expression above.
> >I get the following error each time: "The expression you entered as a
> >query parameter produced this error: *value of TestMnem*

>
> >From everything I've looked at this should work. *If I change
> >"[LabTestMnemonic] = " & TestMnem) to "[LabTestMnemonic] =
> >'CHEMSC'"), the event works correctly.

>
> Because LabTestMnemonic is a Text field in its table, you
> need to end up with quotes around the value in the
> comparison:
>
> TestName = DLookup("LabTestName", "tblLabTests",
> "LabTestMnemonic = """ & TestMnem & """")
>
> I removed the unecessary *[ ] just to make it easier to
> read.
>
> The reason for so many quotes is because a " inside "s has
> to be typed as "". *If the values in LabTestMnemonic can
> never contain an apostrophe, you could use this instead:
>
> TestName = DLookup("LabTestName", "tblLabTests",
> "LabTestMnemonic = '" & TestMnem & "' ")
>
> --
> Marsh
> MVP [MS Access]


Thanks Marsh,

I've haven't been doing any development in MS Access for about 2 years
now, and the above explaination makes perfect sense.
It feels good to get back into it.
I had forgotten how "interesting" DLookup's usage of "'/"" to set off
text string variable criteria inside a text string (criteria
expression) was.
It would have been most helpful if the MS Access Help (F1) for DLookup
included a string criteria example. Perhaps it is not considered good
practice perhaps?
It works great none the less,

Thanks again.

Don
LabDBDesigns
 
Reply With Quote
 
Jason
Guest
Posts: n/a
 
      30th Dec 2010
then add the quotes as below assuming TestMnem is string not numeric
On 30/12/2010 11:46, Don Barton wrote:
> TestName = DLookup("[LabTestName]", "tblLabTests", "[LabTestMnemonic] = """ & TestMnem & """)

TOO MANY QUOTED LINES
 
Reply With Quote
 
Jason
Guest
Posts: n/a
 
      30th Dec 2010
then add the quotes as below assuming TestMnem is string not numeric
On 30/12/2010 11:46, Don Barton wrote:
TestName = DLookup("[LabTestName]", "tblLabTests", "[LabTestMnemonic]
= """ & TestMnem & """")
> TOO MANY QUOTED LINES


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use Variable for DLookup Form name Ryan Tisserand Microsoft Access Form Coding 3 12th Mar 2008 08:35 PM
DLOOKUP(); expression as a variable roelofv Windows XP 0 18th Feb 2008 03:13 PM
Help! DLookup w/Variable swngdncr@garlic.com Microsoft Access Form Coding 3 23rd Mar 2006 11:10 PM
Dlookup in variable Terry Microsoft Access Form Coding 2 10th Feb 2004 02:51 PM
two variable dlookup string Brenton Anderson Microsoft Access VBA Modules 2 3rd Nov 2003 12:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:43 PM.