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

    How to make automatic Search Form?

    I've created a search form which works quite well. A user selects a value from a combo box and then hits the search button (cmdSearch) and the form is filtered using the Click event.



    Click image for larger version. 

Name:	search.png 
Views:	16 
Size:	31.3 KB 
ID:	22469

    I would like to remove the search button and make it so that the form is automatically filtered as soon as a user selects a value from one of the combo boxes. I believe this would be in the AfterUpdate event of each combo box. If that is the case, I am not sure how to break up the code into several different combo boxes. I appreciate any help I can get.

    Code:
       Dim startStr As StringDim strFilter As String
    If Not IsNullOrEmpty(Me.cboSearchLastName) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "(tblBuilding.BuildingPK IN (" _
        & " SELECT tblFacilityMgr.BuildingFK " _
        & " FROM tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK " _
        & " WHERE tblCustomer.LastName ='" & Me.cboSearchLastName & "') OR "
        strFilter = strFilter & "tblRooms.RoomsPK IN (" _
        & " SELECT tblRoomsPOC.RoomsFK " _
        & " FROM tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK " _
        & " WHERE tblCustomer.LastName ='" & Me.cboSearchLastName & "'))"
    End If
    If Not IsNullOrEmpty(Me.cboSearchFirstName) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "(tblBuilding.BuildingPK IN (" _
        & " SELECT tblFacilityMgr.BuildingFK " _
        & " FROM tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK " _
        & " WHERE tblCustomer.FirstName ='" & Me.cboSearchFirstName & "') OR "
        strFilter = strFilter & "tblRooms.RoomsPK IN (" _
        & " SELECT tblRoomsPOC.RoomsFK " _
        & " FROM tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK " _
        & " WHERE tblCustomer.FirstName ='" & Me.cboSearchFirstName & "'))"
    End If
    If Not IsNullOrEmpty(Me.cboSearchOrganization) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "(tblBuilding.BuildingPK IN (" _
        & " SELECT tblFacilityMgr.BuildingFK " _
        & " FROM tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK " _
        & " WHERE tblCustomer.OrganizationFK =" & Me.cboSearchOrganization & ") OR "
        strFilter = strFilter & "tblRooms.RoomsPK IN (" _
        & " SELECT tblRoomsPOC.RoomsFK " _
        & " FROM tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK " _
        & " WHERE tblCustomer.OrganizationFK =" & Me.cboSearchOrganization & "))"
    End If
    If Not IsNullOrEmpty(Me.cboSearchShopName) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "(tblBuilding.BuildingPK IN (" _
        & " SELECT tblFacilityMgr.BuildingFK " _
        & " FROM tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK " _
        & " WHERE tblCustomer.ShopNameFK =" & Me.cboSearchShopName & ") OR "
        strFilter = strFilter & "tblRooms.RoomsPK IN (" _
        & " SELECT tblRoomsPOC.RoomsFK " _
        & " FROM tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK " _
        & " WHERE tblCustomer.ShopNameFK =" & Me.cboSearchShopName & "))"
    End If
    If Not IsNullOrEmpty(Me.cboSearchOfficeSym) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "(tblBuilding.BuildingPK IN (" _
        & " SELECT tblFacilityMgr.BuildingFK " _
        & " FROM tblCustomer INNER JOIN tblFacilityMgr ON tblCustomer.CustomerPK = tblFacilityMgr.CustomerFK " _
        & " WHERE tblCustomer.OfficeSymFK =" & Me.cboSearchOfficeSym & ") OR "
        strFilter = strFilter & "tblRooms.RoomsPK IN (" _
        & " SELECT tblRoomsPOC.RoomsFK " _
        & " FROM tblCustomer INNER JOIN tblRoomsPOC ON tblCustomer.CustomerPK = tblRoomsPOC.CustomerFK " _
        & " WHERE tblCustomer.OfficeSymFK =" & Me.cboSearchOfficeSym & "))"
    End If
    If Not IsNullOrEmpty(Me.cboSearchBuildingName) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "[BuildingPK] =" & Me.cboSearchBuildingName
    End If
    If Not IsNullOrEmpty(Me.cboSearchRoomName) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "[RoomsPK] =" & Me.cboSearchRoomName
    End If
    If Not IsNullOrEmpty(Me.cboSearchEquipmentName) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "tblRooms.RoomsPK IN (" _
        & " SELECT tblCabinet.RoomsFK " _
        & " FROM tblCabinet INNER JOIN tblEquipment ON tblCabinet.CabinetPK = tblEquipment.CabinetFK " _
        & " WHERE tblEquipment.EquipmentName ='" & Me.cboSearchEquipmentName & "') "
    End If
    If Not IsNullOrEmpty(Me.cboSearchSerialNum) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "tblRooms.RoomsPK IN (" _
        & " SELECT tblCabinet.RoomsFK " _
        & " FROM tblCabinet INNER JOIN tblEquipment ON tblCabinet.CabinetPK = tblEquipment.CabinetFK " _
        & " WHERE tblEquipment.SerialNum ='" & Me.cboSearchSerialNum & "') "
    End If
    If Not IsNullOrEmpty(Me.cboSearchEquipmentIP) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & "tblRooms.RoomsPK IN (" _
        & " SELECT tblCabinet.RoomsFK " _
        & " FROM tblCabinet INNER JOIN tblEquipment ON tblCabinet.CabinetPK = tblEquipment.CabinetFK " _
        & " WHERE tblEquipment.EquipmentIP ='" & Me.cboSearchEquipmentIP & "') "
    End If
    Call MsgBox(strFilter, vbOKOnly, "Debug")
    If DCount("*", "qryFrmMainRooms", strFilter) = 0 Then
                    MsgBox "No corresponding records to your search criteria." & vbCrLf & vbCrLf
    Call cmdReset_Click
    End If
    Me.Filter = strFilter
    Me.FilterOn = True

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I would like to remove the search button and make it so that the form is automatically filtered as soon as a user selects a value from one of the combo boxes.
    I think this is a bad idea unless 99.9% of the time only one search term is selected. If a user has 5 terms to select/search on, you will have to wait through 5 searches to get the final results.




    Having said that, lets say the button is named "btnSearch" and the event is "btnSearch_Click".

    In the after update event of each of the combo boxes add the line
    Code:
    Call btnSearch_Click
    And set the button's visible property to FALSE. You will have everything set up to return to the button functionality - just comment out/delete the after update events for the combo boxes.

  3. #3
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218
    Thanks for your help ssanfu! I had no idea it was just that easy.

    You do make a good point though. I don't imagine it will be an issue because usually no more than 2 combo boxes are used at the same time. But I've set it up as you said and if I don't like it after a day or two of testing, I'll comment it out.

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

Similar Threads

  1. How do I make a search like this?
    By JohnBlue in forum Access
    Replies: 8
    Last Post: 06-19-2015, 10:15 AM
  2. How do I make a search like this?
    By JohnBlue in forum Access
    Replies: 6
    Last Post: 06-18-2015, 01:11 PM
  3. Replies: 10
    Last Post: 08-02-2012, 08:32 AM
  4. Replies: 6
    Last Post: 11-22-2011, 09:24 AM
  5. How to make Search forms
    By Spark in forum Forms
    Replies: 7
    Last Post: 09-12-2011, 05:37 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