Results 1 to 6 of 6
  1. #1
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218

    Multiple search Criteria in vba

    I have a problem here I was hoping someone could help me on.



    I'm building a search form which contains several search boxes. Some of them are text, and some are numbers. I am successful in getting it to work with text (cboSearchLastName and cboSearchFirstName). However when I add numbers I remove the single quote and then it no longer works with "AND" at the end of it. It gives me run-time error 3075 and says invalid syntax "WHERE OrganizationFK = 1 AND". If I remove the AND it works without a problem, but then I cannot expand my search criteria.

    Code:
    Private Sub cmdSearch_Click()Dim sqlSearch As String
         If Not IsNull(Me.cboSearchLastName) Then
              sqlSearch = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _
    & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _
    & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _
    & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _
    & " WHERE LastName ='" & Me.cboSearchLastName & "'" _
    & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _
    & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _
    & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK" _
    & " WHERE LastName ='" & Me.cboSearchLastName & "'AND"
       End If
       If Not IsNull(Me.cboSearchFirstName) Then
              sqlSearch = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _
    & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _
    & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _
    & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _
    & " WHERE FirstName ='" & Me.cboSearchFirstName & "'" _
    & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _
    & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _
    & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK" _
    & " WHERE FirstName ='" & Me.cboSearchFirstName & "'AND"
       End If
       If Not IsNull(Me.cboSearchOrganization) Then
              sqlSearch = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _
    & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _
    & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _
    & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _
    & " WHERE OrganizationFK =" & Me.cboSearchOrganization & "" _
    & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _
    & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _
    & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK" _
    & " WHERE OrganizationFK =" & Me.cboSearchOrganization & " AND "
       End If
       If Not IsNull(Me.cboSearchShopName) Then
       sqlSearch = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _
    & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _
    & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _
    & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _
    & " WHERE ShopNameFK =" & Me.cboSearchShopName & "" _
    & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _
    & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _
    & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK" _
    & " WHERE ShopNameFK =" & Me.cboSearchShopName & ""
    End If
            Call MsgBox(sqlSearch, vbOKOnly, "Debug")
    Me.RecordSource = sqlSearch
    End Sub
    What am I doing wrong?

  2. #2
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218
    One other question.

    In many code examples (but not all) "sqlSearch" is included after the equals sign. Like this: sqlSearch = sqlSearch &. Usually it's called strWhere but name doesn't matter.

    Why is that? What does that do? Why not leave it out and just have it like I did it above? Is it wrong to do it like that?

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    When you see it done like that they are building up the string step by step. When an optional search field has been filled in, you add it to the string. You add And between each, typically trimming off the trailing And at the end. Which is probably your solution.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    See Martin Green's site for vba and SQL info. There are 6 lessons, starting here.

  5. #5
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218
    I figured it out!

    You were right pbaldy.

    Also I found out I can't have each search string be its own SQL statement and then use that as the RecordSource. Because, there can only be one query as the recordsource for the form. So, I had to set the recordsource to my sql statement and then filter from there. Also I needed to write code to automatically remove the trailing " AND ".

    Code:
    Private Sub cmdSearch_Click()Me.RecordSource = "SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblFacilityMgr.CustomerFK, tblFacilityMgr.BuildingFK, tblRooms.RoomsPK" _
    & " FROM (tblBuilding INNER JOIN tblRooms ON tblBuilding.BuildingPK = tblRooms.BuildingFK) INNER JOIN" _
    & " (tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK) ON" _
    & " tblBuilding.BuildingPK = tblFacilityMgr.BuildingFK" _
    & " UNION SELECT tblCustomer.LastName, tblCustomer.FirstName, tblCustomer.OrganizationFK," _
    & " tblCustomer.ShopNameFK, tblCustomer.OfficeSymFK, tblRoomsPOC.CustomerFK, tblRooms.BuildingFK, tblRoomsPOC.RoomsFK" _
    & " FROM tblRooms INNER JOIN (tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK)" _
    & " ON tblRooms.RoomsPK = tblRoomsPOC.RoomsFK"
    Dim strWhere As String
    Dim lngLen As Long
         If Not IsNull(Me.cboSearchLastName) Then
              strWhere = strWhere & "[LastName] ='" & Me.cboSearchLastName & "' AND "
                 End If
                 If Not IsNull(Me.cboSearchFirstName) Then
              strWhere = strWhere & "[FirstName] ='" & Me.cboSearchFirstName & "' AND "
                 End If
                 If Not IsNull(Me.cboSearchOrganization) Then
              strWhere = strWhere & "[OrganizationFK] =" & Me.cboSearchOrganization & " AND "
                 End If
                 If Not IsNull(Me.cboSearchShopName) Then
              strWhere = strWhere & "[ShopNameFK] =" & Me.cboSearchShopName & " AND "
                 End If
                 If Not IsNull(Me.cboSearchOfficeSym) Then
              strWhere = strWhere & "[OfficeSymFK] =" & Me.cboSearchOfficeSym & " AND "
                 End If
                 If Not IsNull(Me.cboSearchBuildingName) Then
              strWhere = strWhere & "[BuildingFK] =" & Me.cboSearchBuildingName & " AND "
                 End If
            Call MsgBox(strWhere, vbOKOnly, "Debug")
        lngLen = Len(strWhere) - 5
        If lngLen <= 0 Then
            MsgBox "No criteria", vbInformation, "Nothing to do."
        Else
            strWhere = Left$(strWhere, lngLen)
            Call MsgBox(strWhere, vbOKOnly, "Debug")
          '  MsgBox "No Records Found."
    Me.Filter = strWhere
    Me.FilterOn = True
    End If
    End Sub

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by ittechguy View Post

    You were right pbaldy.
    I'm always right...except when I'm wrong.

    Glad you got it working.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-26-2015, 11:08 AM
  2. Multiple Search Criteria Form
    By J77TDC in forum Forms
    Replies: 3
    Last Post: 08-09-2011, 08:50 AM
  3. Replies: 9
    Last Post: 05-05-2011, 02:05 PM
  4. Search with multiple criteria
    By injanib in forum Forms
    Replies: 1
    Last Post: 01-16-2011, 11:51 PM
  5. Search Button with Multiple Criteria
    By injanib in forum Forms
    Replies: 2
    Last Post: 01-12-2011, 02:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums