|
-
May 12th, 2004, 09:15 PM
#1
Thread Starter
Fanatic Member
Database Query Problem [SOLVED]
I'm using the following code:
VB Code:
rs.Open "SELECT * FROM " & lvwGroups.SelectedItem & " ORDER BY Name", dtaVolumes.ConnectionString, adOpenKeyset, adLockReadOnly
The error is as follows:
Syntax error in FROM clause
I looked it up on MSDN, and it says this error is caused by a table having the name of a reserved keyword in SQL. I changed the table name to something like "Test" just to test it, and STILL got the error! Now I know that the word Test is definitely not used in SQL. What's wrong?
Last edited by hothead; May 13th, 2004 at 04:26 PM.
-
May 13th, 2004, 12:54 AM
#2
Re: Database Query Problem
Try this:
VB Code:
rs.Open "SELECT * FROM `" & lvwGroups.SelectedItem & "` ORDER BY `Name`", dtaVolumes.ConnectionString, adOpenKeyset, adLockReadOnly, adCmdText
N.B. This ` is not an apostrophe '.
-
May 13th, 2004, 06:28 AM
#3
Fanatic Member
You could also try surrounding the table name with square brackets - []:
VB Code:
rs.Open "SELECT * FROM [b][[/b]" & lvwGroups.SelectedItem & "[b]][/b] ORDER BY [b][[/b]Name[b]][/b]", dtaVolumes.ConnectionString, adOpenKeyset, adLockReadOnly, adCmdText
Also, Name is a reserved word, so I surrounded it with the brackets as well.
If neither visualAd's nor my suggestion works, build a SQL string and output it to verify the sql statement you are trying to run:
VB Code:
Dim sSql As String
sSql = "SELECT * FROM [" & lvwGroups.SelectedItem & "] Order By [Name]"
Debug.Print sSql
rs.Open sSql, dtaVolumes.ConnectionString, adOpenKeyset, adLockReadOnly, adCmdText
Chris
Master Of My Domain
Got A Question? Look Here First
-
May 13th, 2004, 04:19 PM
#4
Thread Starter
Fanatic Member
Neither has worked. Still getting error in FROM clause. SQL statement is being passed as expected.
-
May 13th, 2004, 04:25 PM
#5
Thread Starter
Fanatic Member
I fixed it.
Strange.... seems the adCmdTable was causing my problem.
-
May 13th, 2004, 04:29 PM
#6
adCmdTable - siginifies the text is the name of a table in the database
adCmdText - should be used if it is an SQL statement.
-
May 13th, 2004, 07:35 PM
#7
Thread Starter
Fanatic Member
Yeah, now that you've explained this to me, it makes a lot more sense.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|