Dlookup format for table lookup with criteria

I am trying to look up a selected record in a table.
The table holds Company names and a field to identify Report Paths.
This table called tblServerReportTable has three fields ServerID, Company and ReportPath.
I want to look up the 'report path' based on selecting a specific company.
In my code, I have a variable called MyCompany which identifies the company.
However  my Dlookup code,  to look up this report path, fails.
The code I am trying to use is;
MyLookup = DLookup("[ReportPath]", "tblServerReportTable", "[company] =" & [MyCompany])
The code is failing with the error
"Syntax error (missing operator) in query expression '[company]=MyCompanyName'
(MyCompanyName is the return of the variable MyCompany.)
I have tried many different quotes formats, etc without success.
Can anyone help with what is wrong with my code, or is there a better way, for example a SQL select code ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this:
MyLookup = DLookup("[ReportPath]", "tblServerReportTable", "[company] =" & CHR(34) & [MyCompany] & Chr(34))

Open in new window

Text criteria needs to be delimited with quotation marks.

Also, if no records are found, DLookup will retur a null, so you need handle that possibility.

Appending an empty string is one way to do this:

MyLookup = "" & DLookup("ReportPath", "tblServerReportTable", "company =" & CHR(34) & [MyCompany] & Chr(34))

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

Jim Dettman has an article here that covers this topic very thoroughly:


jwjjwjAuthor Commented:
Thank you, the solution worked perfectly, first time. Also thank you for the reference to Jim Dettman, very helpful
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.