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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Table Name in SQL From Statement

Status
Not open for further replies.

lizzbizz

MIS
Dec 17, 2001
6
US
How can I write an SQL statement where the From table name changes based on user's input into a form's textbox?
 
In it simplest form you merely use the value from the textbox. The problem with using this method is that the user must input the information exactly or the SQL statement will error when run. We have used the MSysObjects table to return all the tables in the database to a combobox. The combobox then will always have a valid table name. Just remember to write the ControlSource property of the combobox to exclude those table you don't want to see, 'Not Like MSys*" and 'Not Like "USys*".

"SELECT * FROM " & Me.Textbox1 ";"

Steve King Growth follows a healthy professional curiosity
 
I'm new to VB programming so I want to stick with the simplest method for now. I am the user so I'll trust that I can enter the table name correctly in the text box.

What code would precede the SQL statement "SELECT * FROM " & Me.Textbox1 ";" ?

Thanks.

 
The following code gives me a Query Definition already exists error. How can I make it temporary (Name = "") and still run the OpenQuery command?

Dim dbs As Database, qdf As QueryDef, strSQL As String
Set dbs = CurrentDb
strSQL = "SELECT * FROM " & Forms!Form1!MonthYear & ";"
Set qdf = dbs.CreateQueryDef("Exception Greater or Equal to $10", strSQL)
DoCmd.OpenQuery "Exceptions Greater or Equal to $10"
 
try Deleting the Object before you "Create" it again

On Error Resume Next 'In case it doesn't exist
DoCmd.DeleteObject acQuery "Exceptions Greater or Equal to $10"


If you have Error Handling in place in your code, then add a line to send any additional errors to the Error Handling code.

PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top