Consulting

Results 1 to 8 of 8

Thread: Solved: SQL Strings

  1. #1
    VBAX Regular
    Joined
    Apr 2007
    Posts
    36
    Location

    Solved: SQL Strings

    Hello All

    If I have two SQL statements like the following

    [vba]
    strSQL = "SELECT * FROM tblSchool_Clean " & _
    "WHERE tblSchool_Clean.PoE_Desc IN(" & strCriteria1 & ");"
    [/vba]

    [vba]
    strSQL = "SELECT * FROM tblSchool_Clean " & _
    "WHERE tblSchool_Clean.ToE_Desc IN(" & strCriteria2 & ");"
    [/vba]

    is it possible to merge them into one by using an AND somewhere, I dont know how to do this so if it is possible can someone show me with the above example please.

    Many Thanks

    Mav

  2. #2
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Depends on what you mean by "merging" them. If you mean, 'give me a
    result set that matches both criteria':

    [VBA]
    strSQL = SELECT * FROM tblSchool_Clean " & _
    "WHERE PoE_Desc IN (" & strCriteria1 & ") " & _
    "AND WHERE ToE_Desc IN (" & strCriteria2 & ")"
    [/VBA]

    If you mean, 'give me a result set that matches at least one criterion':

    [VBA]
    strSQL = SELECT * FROM tblSchool_Clean " & _
    "WHERE PoE_Desc IN (" & strCriteria1 & ") " & _
    "OR WHERE ToE_Desc IN (" & strCriteria2 & ")"
    [/VBA]
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  3. #3
    VBAX Regular
    Joined
    Apr 2007
    Posts
    36
    Location
    many many thanks, and if I had Pivot Table to share I would give it you.

  4. #4
    VBAX Regular
    Joined
    Apr 2007
    Posts
    36
    Location
    Alas it did not work, I placed the following string in the code and an error message box pops up with "Characters found after end of SQL statement

    [vba]
    strSQL1 = "SELECT * FROM tblSchool_Clean " & _
    "WHERE tblSchool_Clean.PoE_Desc IN(" & strCriteria1 & ");" & _
    "AND WHERE tblSchool_Clean.ToE_Desc IN(" & strCriteria2 & );"
    [/vba]

    The code which follows this bit is

    [vba]
    ' Apply the new SQL statement to the query
    qdf.SQL = strSQL1

    ' Empty the memory
    Set db = Nothing
    Set qdf = Nothing
    [/vba]

    Can anyone shed some light on this please, thanks ever so much in advance

    Mav

  5. #5
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Mav,

    You did not quite follow directions

    [VBA]
    strSQL1 = "SELECT * FROM tblSchool_Clean " & _
    "WHERE tblSchool_Clean.PoE_Desc IN(" & strCriteria1 & ") " & _
    "AND WHERE tblSchool_Clean.ToE_Desc IN(" & strCriteria2 & ");"
    [/VBA]
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  6. #6
    VBAX Regular
    Joined
    Apr 2007
    Posts
    36
    Location
    Another error message pops up this time (I cut and pasted this time)

    Syntax error (missing operator) in query expression 'tblSchool_Clean.PoE_Desc IN ('School Phase One','School Phase Two') AND WHERE tblSchool_Clean.ToE_Desc IN ('School Type One','School Type Two')'.

    ....School Phase One/Two and School Type One/Two are the selection types.....

    ta

    Mav

  7. #7
    VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    [vba]
    strSQL1 = "SELECT * FROM tblSchool_Clean " & _
    "WHERE tblSchool_Clean.PoE_Desc IN(" & strCriteria1 & ") " & _
    "AND tblSchool_Clean.ToE_Desc IN(" & strCriteria2 & ");"
    [/vba]

  8. #8
    VBAX Regular
    Joined
    Apr 2007
    Posts
    36
    Location
    Geekgirl to the rescue

    many thanks

    Mav

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •