Results 1 to 7 of 7

Thread: Database Query Problem [SOLVED]

  1. #1

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692

    Database Query Problem [SOLVED]

    I'm using the following code:

    VB Code:
    1. 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.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Database Query Problem

    Try this:
    VB Code:
    1. rs.Open "SELECT * FROM `" & lvwGroups.SelectedItem & "` ORDER BY `Name`", dtaVolumes.ConnectionString, adOpenKeyset, adLockReadOnly, adCmdText

    N.B. This ` is not an apostrophe '.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    You could also try surrounding the table name with square brackets - []:
    VB Code:
    1. 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:
    1. Dim sSql As String
    2.  
    3. sSql = "SELECT * FROM [" & lvwGroups.SelectedItem & "] Order By [Name]"
    4.  
    5. Debug.Print sSql
    6. rs.Open sSql, dtaVolumes.ConnectionString, adOpenKeyset, adLockReadOnly, adCmdText
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  4. #4

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    Neither has worked. Still getting error in FROM clause. SQL statement is being passed as expected.

  5. #5

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    I fixed it.

    Strange.... seems the adCmdTable was causing my problem.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    adCmdTable - siginifies the text is the name of a table in the database

    adCmdText - should be used if it is an SQL statement.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    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
  •  



Click Here to Expand Forum to Full Width