FROM (tablename) question?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can you setup a query to use a tablename passed as a parameterinstead of
hardcoded. I have multiple queries that are exactly the same except for table
name and I want just have one query and pass the table name depending on the
logic.

Is that possible???
 
You can do something like this using VBA code, where you build the SQL
string into a variable, and then execute the query via VBA code.

But if you want to have the query ask you for the tablename as a parameter
when you run the query, as noted elsethread, the answer is no.
 
Thanks. I got it to run using a string in VBA code but could you show me an
example of how to view the results or save it to use later?

Dim conDatabase As ADODB.Connection
Dim strSQL As String
Dim TableName1 As String

Set conDatabase = CurrentProject.Connection
'To be populated on the fly
TableName1 = "tblMFSFund"

strSQL = "SELECT * FROM " & TableName1
conDatabase.Execute strSQL

conDatabase.Close
Set conDatabase = Nothing
 
You can do this by creating a temporary query and saving it, and then
displaying it.

Dim qdf As DAO.QueryDef
Dim dbs As DAO.Database
Dim intLoop As Integer
Dim strSQL As String, strQ As String
strQ = "QueryName"
Set dbs = CurrentDb
strSQL = "SELECT * FROM " & TableName1
Set qdf = dbs.CreateQueryDef(strQ, strSQL)
qdf.Close
DoCmd.OpenQuery strQ
Set qdf = Nothing
dbs.Close
Set dbs = Nothing

--

Ken Snell
<MS ACCESS MVP>
 
Back
Top