Log in

View Full Version : create new table from existing table where existing table name is variable



Knife1
12-17-2010, 08:53 AM
Hi

I would like to create a new table from an existing table but the name of the table to create the new table from should be defined by the user via an inputbox.

So for example when the user names the table as “table1” , table1 is passed to a string variable and table1 in the database is used to extract table 2 from. I though to do this using a SQL select into procedure as below but I think that a variable name is not accepted in the FROM clause of a SQL statement.

How could I do this?



Dim strTableName As String
strTableName = InputBox("Please enter the Table Name", "Table Name", "Table1")
DoCmd.RunSQL "SELECT Column1, Column2 INTO table2 FROM strTableName "



Cheers

HiTechCoach
12-17-2010, 08:45 PM
Try:




DoCmd.RunSQL "SELECT Column1, Column2 INTO table2 FROM " & strTableName

knifelong
12-21-2010, 06:20 AM
Yes, that worked fine thanks!

HiTechCoach
12-21-2010, 06:28 AM
You're welcome.

Glad we could assist.