2015年4月30日 星期四

Access create search Form

This Access tutorial explains how to create a search Form with multiple criteria.


You may also want to read:


Access DoCmd.OpenForm Method to open Access Form


Access Combo Box value depends on another Combo Box


Return Access Form input to Access Report or Form or Query


Access create search Form with multiple criteria


Sometimes you need to create a search Form where users can select specific criteria. The simplest solution is to directly filter data for each field like using AutoFilter in Excel, but this is not elegant at all. A better way to do this is for user to choose from dropdown box of different levels, value of one dropdown box depends on the value of another dropdown box.


For example, in the first dropdown box, users choose department A, and then in the second dropdown box,  users can view a list of person of department A.


This tutorial will show you how to do this.


Create a result Form


Assume that we have a Query as below. Our goal is to allow users to select department to show employee information, or select department + employee ID.


We are going to make a “result Form” that shows the search result.






































Query1
DepartmentEmployee IDEmployment DateSalaryAllowance
Account11/1/200010000100
Admin21/1/200120000200
Admin31/1/200230000300
Admin41/1/200340000400
Payroll51/1/200450000500
HR61/1/200560000600

Click on the Query and the under Create tab, select More Forms > DataSheet


access_search_form


Now save the Form as result_frm


access_search_form_04


In this example, I want to return all fields of Query1 in Result Form. I choose DataSheet because each record can be displayed in one row with horizontal scroll bar. I don’t prefer to use Continuous Form or Single Form because each record is displayed in several rows.


Create a Search Form


Create a blank Form and then add two Combo Boxes (Combo_Dept, Combo_EmpID) and one button (search_btn) as shown below.


access_search_form_05


1) Click on Combo_Dept, in the Row Source property, type


SELECT DISTINCT Department FROM Query1;

Row Source is what you can select in the ComboBox.


2) Click on Combo_EmpID, in the Row Source property, type


SELECT [Employee ID] FROM Query1 WHERE [Department]=[forms]![search_frm]![Combo_Dept];

3) Select Combo_Dept, select After Update Event and build the below Procedure


Private Sub Combo_Dept_AfterUpdate()
    Me.Combo_EmpID = Null
    Me.Combo_EmpID.Requery
End Sub

As you select a Department, Combo_EmpID is reset to Null and then rerun the query


4) Right Click on search_btn and then choose Build Event


Private Sub search_btn_Click()
    If IsNull(Me.Combo_EmpID) Then
        searchCriteria = "[Department]='" & Me.Combo_Dept & "'"
        searchSQL = "Select * FROM Query1 where " & searchCriteria
    Else
        searchCriteria = "[Department]='" & Me.Combo_Dept & "' AND [Employee ID] =" & Me.Combo_EmpID
        searchSQL = "Select * FROM Query1 where " & searchCriteria
    End If
    DoCmd.OpenForm "result_frm", 3, searchSQL
End Sub

Now you have successfully created a simple search Form. After you have selected criteria in Search Form, once you click the search button, the Result Form will pop up and displays the result.


Outbound References


https://msdn.microsoft.com/en-us/library/office/ff820845.aspx?f=255&MSPPError=-2147217396



Access create search Form

沒有留言:

張貼留言