By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,646 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,646 IT Pros & Developers. It's quick & easy.

table's name in a variable - in sql stmt

P: 3
hi
can any one help me how to construct an sql select statement in which the table's name , i'm givin it in a variable

Tbl="eng_class6" or sometimes Tbl="maths_class6" , so I want data from diiferent tables.

Cmd.CommandText = "select * from " & Tbl & " where questno =" & arrRndNos(i) & ""
Cmd.CommandType = adCmdTable
Set RS = Cmd.Execute

Is it possible?
this gives me the error in FROM clause.
thanks
Nov 26 '06 #1
Share this Question
Share on Google+
3 Replies


Expert 5K+
P: 8,434
can any one help me how to construct an sql select statement in which the table's name , i'm givin it in a variable
Tbl="eng_class6" or sometimes Tbl="maths_class6" , so I want data from diiferent tables.
Cmd.CommandText = "select * from " & Tbl & " where questno =" & arrRndNos(i) & ""
Cmd.CommandType = adCmdTable
Set RS = Cmd.Execute
Is it possible?
this gives me the error in FROM clause.
thanks
The general idea seem alright, so the problem is likely to be in the specific details of this case. Can you tell us the exact error message? And can you set a break point or something, and display the value of Tbl to check for certain what's in it? (You could also use Debug.Print, or whatever).

It might help (or might not) help if you put square brackets around the table name. For example, "[maths_class6]" instead of "maths_class6".

It might help to know what environment you're working in, too. For example, VB6, VB.Net, VBA in MS Access, etc.

Um... You might also try adding a semicolon (;) on the end of the string - don't know whether it will help or not. Is that what was supposed to be in that empty string you're appending?
Nov 26 '06 #2

P: 3
hi, it works with this.

Cmd.CommandText = "select * from " & Tbl & " where questno =" & arrRndNos(1) & ";"
Cmd.CommandType = adCmdText
Set RS = Cmd.Execute

thank you so much
Nov 27 '06 #3

Expert 5K+
P: 8,434
hi, it works with this.
Cmd.CommandText = "select * from " & Tbl & " where questno =" & arrRndNos(1) & ";"
Cmd.CommandType = adCmdText
Set RS = Cmd.Execute
thank you so much
So the problem must have been either the semicolon, or the value of i.
Nov 27 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.