ACCESS subform filter (fuzzy query)

ACCESS subform filter (fuzzy query)


Make a blank form, set controls and subforms according to the fields to be filtered, as shown in the figure:

image.png


The figure above is the filter form of the Water Margin Seating Table, with two filter fields set up, namely "seat" and "name".


The design idea is to use VBA to set the data source attribute (recordsource) of the subform, and filter out the data display that meets these two conditions according to the "seat" and "name", and the "name" field should be queried fuzzy.


Add a button to the form, and write the following code on the click event of the button:


Private Sub btnCX_Click()


Dim str As String

str = ""



If Not IsNull(Me.座次) And IsNull(Me.姓名) Then


    str = "where [108将].[seats]=" & Me.seats

    

ElseIf Not IsNull(Me.姓名) And IsNull(Me.座次) Then


    str = "where [108将].[name] like'*" & Me. name& "*'"

    

ElseIf IsNull(Me.座次) And IsNull(Me.姓名) Then

    str = ""

    

Else

    str = "where [108将].[seats]=" & Me.seats& "or [108will].[name] like'*" & Me. name& "*' "

 

End If


    Me.Ctl108 will_child form.Form.RecordSource = "select * from [108will]" & str

    Me.Ctl108 will _ child form.Form.Requery

End Sub


The design ideas are as follows:

1. When "seat order" has content, and "name" is empty, the code

 str = "where [108将].[seats]=" & Me.seats

Because "seat order" is digital, just use & to connect.

2. When the "name" has content, and the "seat order" is empty, the code

str = "where [108将].[name] like'*" & Me. name& "*'"

Here, because "name" is a text type, it is necessary to use the'"& text field variable & "'used for variable reference, which is single quotation mark, double quotation mark, &, text variable, &, double quotation mark, single quotation mark.

In order to use wildcards to implement fuzzy queries, add * between single quotation marks and double quotation marks. ** One thing to note is that there can be no spaces between single quotation marks, *, and double quotation marks. **


Guess you like

Origin blog.51cto.com/14212895/2666917