Avatar of Susan Stevenson
Susan Stevenson
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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

Thanks,
Susan
Microsoft AccessVisual Basic ClassicVB Script

Avatar of undefined
Last Comment
Gustav Brock

8/22/2022 - Mon
David Christal CISSP

Try adding a couple of quotes:  "SELECT LkupQuoteData." & Currentfield  &"  FROM LkupQuoteData;"
SOLUTION
Kelvin Sparks

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Susan Stevenson

ASKER
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
Kelvin Sparks

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

Kelvin
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Gustav Brock

Remove the underscore:

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

/gustav
ASKER CERTIFIED SOLUTION
Dale Fye

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Gustav Brock

Ah, true. Execute is for action queries.

/gustav