Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How come this SQL statement won't work? 1

Status
Not open for further replies.

BotCow

Programmer
Jul 10, 2003
58
US
I have it coded here:

strSQL = "SELECT " & tblName & ".Page, " & tblName & ".Date, " & tblName & ".Subject, " & tblName & ".Company, " & tblName & ".People " & _
"FROM " & tblName

The thing is, I want the tblName to be dynamic. Above that code is this:

tblName = "tbl" & Me!cmbSource

Where 'cmbSource' is the table selected from the combo box.

ie: If I selected "Pears" from the combo box. The SQL statement should read:

SELECT tblPear.Page, tblPear.Date, tblPear.Subject, tblPear.Company, tblPear.People
FROM tblPear

However, I get a syntax error. Did I miss something obvious?
 
You don't need to put a table qualifier in front of each field, especially if you are only selecting from a single table - makes it far more simple to code. It may be an idea to put square brackets round any fieldname you suspect may be a reserved word (e.g. I have my suspicions about 'Page' and 'Date').

tblName = "tbl" & Me!cmbSource
strSQL = "SELECT [Page], [Date], Subject, Company, People FROM " & tblName
 
Thanks, I get the same error, but I don't think it's that segment of code anymore. I'm thinking it has a problem when it sends it to the query. But that code above saves me the trouble of looking at that jumbled crap I coded earlier. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top