Results 1 to 10 of 10

Thread: Error[ReSolved]

  1. #1

    Thread Starter
    Addicted Member Paradox's Avatar
    Join Date
    Sep 2004
    Location
    Nairobi
    Posts
    189

    Resolved Error[ReSolved]

    I am trying to query a records from a SQL Server Dbase and i get a syntax error. Where am i goin wrong
    Thanks


    VB Code:
    1. Private Sub Command1_Click()
    2.     rsrequests.Open "SELECT * FROM Requests WHERE((DateReq) Between '" & Format(DTPicker1.Value, "dd mmm yyyy") & "' AND '" & Format(DTPicker2.Value, "dd mmm yyyy") & "'AND '" & (Combo1.Text) & "')", Cn, adOpenStatic, adLockReadOnly
    Last edited by Paradox; Nov 12th, 2004 at 01:13 AM.
    Peny wise pound Foolish

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    That's pretty tough to look at. Why don't you turn on the profiler and check out the sql that's getting sent. Profiler is a great debugging tool.

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The last criteria needs to be part of an equation. You currently have

    & "'AND '" & (Combo1.Text) & "')", cn

    Add the database field you are checking against the combobox.

    Also, insert a space between the single quote and the last And

  4. #4

    Thread Starter
    Addicted Member Paradox's Avatar
    Join Date
    Sep 2004
    Location
    Nairobi
    Posts
    189
    I'm Kinda newbie to SQL ope u don mind writin da code.

    The err msg still remains.

    Incorrect syntax near ')'

    Thanks
    Peny wise pound Foolish

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    brucevde did a lot more than me, but still I suggest you check out the sql that is actually getting sent to the DB. Best way is to use the profiler, if you know how. If not, well, you'll want to learn how to do it (it'll save you tons of time), but you could also set a string to the sql you're going to send, then display the string that will be sent.

  6. #6

    Thread Starter
    Addicted Member Paradox's Avatar
    Join Date
    Sep 2004
    Location
    Nairobi
    Posts
    189
    Thanks for your help. let me try using the profiler and see how it goes
    Peny wise pound Foolish

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Does Combo1.Text contain a single quote?

    Post your modified query.

  8. #8

    Thread Starter
    Addicted Member Paradox's Avatar
    Join Date
    Sep 2004
    Location
    Nairobi
    Posts
    189
    Yeah it has a single quote


    Code:
    Private Sub Command1_Click()
        rsrequests.Open "SELECT * FROM Requests WHERE((DateReq) Between '" & Format(DTPicker1.Value, "dd mmm yyyy") & "' AND '" & Format(DTPicker2.Value, "dd mmm yyyy") & "'AND ' " & (Combo1.Text) & " ' )", Cn, adOpenStatic, adLockReadOnly
    Peny wise pound Foolish

  9. #9
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    You need to replace the single quote in Combo1.Text with two single quotes. I prefer to use a variable to hold the sql statement but thats up to you

    VB Code:
    1. Dim strSQL as String
    2.  
    3. strSQL = "SELECT * FROM Requests WHERE " _
    4. & "((DateReq) Between '" & Format(DTPicker1.Value, "dd mmm yyyy")  _
    5. & "' AND '" & Format(DTPicker2.Value, "dd mmm yyyy")  _
    6. & "' AND SomeDatabaseField = '" & Replace(Combo1.Text,"'","''") & "')",
    7.  
    8. rsrequests.Open strSQL, Cn, adOpenStatic, adLockReadOnly

    The statement sent to SQL Server should look something like

    SELECT * FROM Requests WHERE ((DateReq) Between '11 Nov 2004' AND '11 Nov 2004' AND SomeDatabaseField = 'O''Brien')

  10. #10

    Thread Starter
    Addicted Member Paradox's Avatar
    Join Date
    Sep 2004
    Location
    Nairobi
    Posts
    189
    brucevde and Mike Hildner Thanks alot.
    Dont know how much you guys helped me out. You are appreceiated
    Peny wise pound Foolish

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