Results 1 to 12 of 12

Thread: Syntax run-time error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    113

    Syntax run-time error

    I get error Syntax error (missing operator) in query expression 'Name LIKE '%V%@'' on this code

    Code:
            Select Case cmbOperation(0).ListIndex
    
            Case 0
    
                strFilter = strFilter & " LIKE '%" & txtFilter(0).Text & "%'"
    
            Case 1
    
                strFilter = strFilter & " = '" & txtFilter(0).Text & "'"
    
            Case 2
    
                strFilter = strFilter & " <> '" & txtFilter(0).Text & "'"
    
            Case 3
    
                strFilter = strFilter & " > '" & txtFilter(0).Text & "'"
    
            Case 4
    
                strFilter = strFilter & " >= '" & txtFilter(0).Text & "'"
    
            Case 5
    
                strFilter = strFilter & " < '" & txtFilter(0).Text & "'"
    
            Case 6
    
                strFilter = strFilter & " <= '" & txtFilter(0).Text & "'"
    
            End Select
    
     
    
        ConnectToDB
    
        
    
        Set mobjRst = New ADODB.Recordset
    
        
    
        'mobjRst.Open "SELECT * from Customer where " & Me.cmbFields.Text & strFilter
    
        strSQL = "SELECT * from Customer where " & Me.cmbFields.Text & strFilter
    
        
    
        mobjCmd.CommandText = strSQL
    
        Set mobjRst = mobjCmd.Execute

    Debug line
    Code:
    Set mobjRst = mobjCmd.Execute
    can anyone help?

  2. #2
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009
    Posts
    353

    Re: Syntax run-time error

    Instead of this:

    Code:
        mobjCmd.CommandText = strSQL
    
        Set mobjRst = mobjCmd.Execute
    Try this:

    Code:
        Set mobjRst = mobjCmd.Execute(strSQL)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    113

    Re: Syntax run-time error

    Error Command text was not set for the command on that line

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Syntax run-time error

    before you use the strSQL ... print it to the debug window or to a message box and make sure that the resulting SQL is in fact valid... I have a sneaky suspicion that you aren't getting the SQL you think you are. Also, what kind of database are you running against?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Syntax run-time error

    And you don't Execute a SELECT query with the command object, you use the recordset object to OPEN it.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    113

    Re: Syntax run-time error

    Msgbox strFilter results in

    LIKE '&#37;V%'

    when I remove the command object and use

    Code:
    mobjRst.Open "SELECT * from Customer where " & Me.cmbFields.Text & strFilter
    I get error "The connection cannot be used to perform this operation. It's either closed or invalid in this context."

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    113

    Re: Syntax run-time error

    sorry forgot to mention i'm running it on an MS Access database

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    113

    Re: Syntax run-time error

    corrected the open line by adding the open connection to the end.

    Now i'm getting the syntax error again.

    Syntax error (missing operator) in query expression 'Name LIKE '&#37;V%' on this code

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Syntax run-time error

    Doesn't Access use * vs &#37; for wild cards? I forget.

    Anyway, Name is one of those fields that are kinda reserved, correct? Try using [Name] in your query instead. If that still generates an error, please post the entire sql statement so that we may have a look at it.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    113

    Re: Syntax run-time error

    testing on another field looks like your correct LaVolpe it doesn't like Name, i'll do some more tests to make sure it's working ok now

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    113

    Re: Syntax run-time error

    does anyone know how I can use the word Name in a search without getting the syntax error?

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Syntax run-time error

    The best thing to do is rename the field to something else - because even tho there is a fix in this situation, there are situations where you cannot fix it at all.

    I would recommend changing it to something like CustomerName

    For information on other unsafe field/table names, see the article What names should I NOT use for tables/fields/views/stored procedures/...? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)

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