VBA with SQL Statement (Dynamic field names)

I have the below variables but the SQL statement does not work.  Getting Runtime 438 - object does not support this property or method.

Currentfield ="quote_id"

CurrentfieldValue = db.execute_("SELECT LkupQuoteData. & Currentfield &  FROM LkupQuoteData;")

When hovering over the SQL  statement, it is recognising the quote_id as the field name to use and the underlying query is of the same field name.

Any help would be appreciated

Susan StevensonAsked:
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.

David Christal CISSPCommented:
Try adding a couple of quotes:  "SELECT LkupQuoteData." & Currentfield  &"  FROM LkupQuoteData;"
Kelvin SparksCommented:
I'd declare a string variable and resolve the SELECT LkupQuoteData." & Currentfield  &"  FROM LkupQuoteData into the variable the use that variable in the db.Execute

Susan StevensonAuthor Commented:
Thank you for responding so quickly..

 test = "SELECT LkupQuoteData." & Currentfield & " FROM LkupQuoteData;"
                        MsgBox test - this works as I would expect it to returning the string of the sql statement
                        CurrentfieldValue = db.execute_(test) - This returns the same error as previous :-(

When I run the SQL statement via query wizard, it returns the value I want
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Kelvin SparksCommented:
Can you advise what LkupQuoteData is - a table, view, function ......

Gustav BrockCIOCommented:
Remove the underscore:

CurrentfieldValue = db.execute("SELECT LkupQuoteData." & Currentfield & " FROM LkupQuoteData;")

Dale FyeOwner, Dev-Soln LLCCommented:
I've never seen this syntax.

you could do:

CurrentFieldValue = DLOOKUP(CurrentField, "LkupQuoteData")


set rs = db.openrecordset("SELECT ....")
CurrentFieldValue = iif(not rs.eof, rs(0), NULL)
set rs = nothing

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
Gustav BrockCIOCommented:
Ah, true. Execute is for action queries.

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.