Can I use a variable for a table name in an SQL statement using VB

M

Marshall Barton

ambushsinger said:
Can I use a variable for a table name in an SQL statement using VBA


No.

You can use VBA to construct an SQL statement with the table
name in your variable. Eg.

strSQL = "SELECT whatever FROM [" & strtablename & "] ... "

Then use the SQL statement to do whatever.
 
J

John Spencer

The answer is Yes. Since you posted no details I can only give you a simple
example.

Dim strSQL as String
Dim tblName as String

strSQL = "SELECT A.Field1, A.Field2" & _
" FROM [" & tblName & "] as A INNER JOIN SomeTable"
" ON A.SomeField = SomeTable.SomeMatchingField"



John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
A

ambushsinger

Excellent...it works, Thanks

John Spencer said:
The answer is Yes. Since you posted no details I can only give you a simple
example.

Dim strSQL as String
Dim tblName as String

strSQL = "SELECT A.Field1, A.Field2" & _
" FROM [" & tblName & "] as A INNER JOIN SomeTable"
" ON A.SomeField = SomeTable.SomeMatchingField"



John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
Can I use a variable for a table name in an SQL statement using VBA
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top