Results 1 to 12 of 12

Thread: Whats wrong with this SQL query?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Question Whats wrong with this SQL query?

    This query doesen't work. Please, help me...

    VB Code:
    1. Dim db As Database
    2. Dim rs As Recordset
    3. Dim SQL, SQLWhere, Number1, Number2
    4.  
    5. If Len(Me![Text0]) > 0 Then
    6. Number1 = "=""" & Me![Text0] & """"
    7. Else
    8. Number1 = "Is Null"
    9. End If
    10.  
    11. If Len(Me![Text1]) > 0 Then
    12. Number2 = "="""  & Me![Text1] & """"
    13. Else
    14. Number2 = "Is Null"
    15. End If
    16.  
    17. SQL = "SELECT * FROM tbltable1 "
    18.  
    19. SQLWhere = "WHERE tbltable1.[Field1] " & "" & Number1 & " " & _
    20. "AND tbltable1.[Field2] " & "" & Number2 & ";"
    21.  
    22. Set db = CurrentDb
    23. Set rs = db.OpenRecordset(SQL & SQLWhere)
    Last edited by Pirre001; Jul 31st, 2002 at 06:19 AM.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Try this :

    Debug.Print SQL & SQLWhere


    And post the result here
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    To make it easier for us just post the finished query once it has been built up..

    Also - you can use ' instead of " inside queries.. eg;

    SQL = "SELECT * FROM tblTable1 WHERE Field1=' "+this+" ' "

    this will stop you going mad with the multiple " required.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    think you are missing =


    SQLWhere = "WHERE tbltable1.[Field1] " & "" & Number1 & " " & _
    "AND tbltable1.[Field2] " & "" & Number2 & ";"

    should be

    SQLWhere = "WHERE tbltable1.[Field1] =" & "" & Number1 & " " & _
    "AND tbltable1.[Field2] =" & "" & Number2 & ";"

    assuming field1 and 2 are text fields and not numeric...

    if numeric you also have to loose the ""
    -= a peet post =-

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Peet,

    I "fix" = in this part of the code...
    VB Code:
    1. If Len(Me![Text0]) > 0 Then
    2. Number1 = "=""" & Me![Text0] & """"
    3. Else
    4. Number1 = "Is Null"
    5. End If

    Or...

  6. #6
    Addicted Member E-Link's Avatar
    Join Date
    Nov 2001
    Location
    INA
    Posts
    242
    if your field is text field try this:
    VB Code:
    1. If Len(Me![Text0]) > 0 Then
    2. Number1 = "='" & Me![Text0] & "'"
    3. Else
    4. Number1 = "Is Null"
    5. End If
    6.  
    7. If Len(Me![Text1]) > 0 Then
    8. Number2 = "='"  & Me![Text1] & "'"
    9. Else
    10. Number2 = "Is Null"
    11. End If
    12.  
    13. SQL = "SELECT * FROM tbltable1 "
    14.  
    15. SQLWhere = "WHERE tbltable1.[Field1] " & "" & Number1 & " " & _
    16. "AND tbltable1.[Field2] " & "" & Number2 & ";"
    17.  
    18. Set db = CurrentDb
    19. Set rs = db.OpenRecordset(SQL & SQLWhere)

    but if your fileds are numeric then u must loose the "'"

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    oh.. sorry Pirre

    u sure did

    now do as plenderj said
    -= a peet post =-

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Ok, peet. I do that..

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    E-Link,

    I shuld try your example also...

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    This is exactly what I get with Debug.print.
    VB Code:
    1. SELECT * FROM tbltable1 WHERE tbltable1.[Field1] Is Null AND tbltable1.[Field2] ="D=8
    2. L=30
    3. U=37VAC
    4. R=160W";

    Why can´t I use BOF and EOF whith the code? Now I always get True on both EOF and BOF.
    VB Code:
    1. If Len(Me![Text0]) > 0 Then
    2. Number1 = "='" & Me![Text0] & "'"
    3. Else
    4. Number1 = "Is Null"
    5. End If
    6.  
    7. If Len(Me![Text1]) > 0 Then
    8. Number2 = "='" & Me![Text1] & "'"
    9. Else
    10. Number2 = "Is Null"
    11. End If
    12.  
    13. SQL = "SELECT * FROM tbltable1 "
    14.  
    15. SQLWhere = "WHERE tbltable1.[Field1] " & "" & Number1 & " " & _
    16. "AND tbltable1.[Field2] " & "" & Number2 & ";"
    17. Set db = CurrentDb
    18. Set rs = db.OpenRecordset(SQL & SQLWhere)
    19.  
    20. If rs.BOF = False And rs.EOF = False Then
    21. Msgbox "Test"
    22. End if
    Last edited by Pirre001; Aug 1st, 2002 at 02:57 AM.

  11. #11
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524
    If If rs.BOF = False And rs.EOF = False Then
    Msgbox "Test"
    End if
    Did u use 2 If sentences ???

    try to omit one.

    Btw, what kind of cursor type do u use to open ur recordset ? if u're using adOpenKeyset, u can use

    Code:
         If rs.RecordCount > 0 Then
               MsgBox "Test"
         End If

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    The only I wanna know...if I get any result of my query or not.

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