Variable Table Name in From Statement

  • Thread starter Thread starter Susan Pedersen
  • Start date Start date
S

Susan Pedersen

Guest
How can I dynamically change the table name in an SQL From Statement based on a user's input into a text box on a form?

In other words, the table name is supplied via a variable.

Thanks!
 
One way to do this is through code (of course). In the Form you have the textbox (we'll call it txtbox1). The code should probably be something like this:


dim strTableName as string
dim strSQL as string

'This will get the string name of the table
strTableName = me.txtbox1.value

'This is the SQL statement that will change via the user's input

strSQL = "select * from " & strTableName

Now you can do whatever you want with the query. Something like the following

[form]![FormNameThatyouwantchanged].recordsource = strSQL

This will change the record source of the form to the query that the use specified.
 

Users who are viewing this thread

Back
Top Bottom