MS-ACCESS VBA: HOW TO FILTER A SUBFORM IN DATASHEET VIEW
Suppose that a text box is placed in the main form and user inputs data into it to get the filtered records from the subform. To achieve this filter, we use the following codes in VBE environment.
The first sub routine in on the Clear Filter Button to remove filter from the subform.
The second sub routine is on the text box used for search and the AfterUpdate event is executed when user submits any value in the text box.
Option Compare Database
Option Explicit
Private Sub cmdClearFilter_Click()
Me.Text1.Value = ""
Me.Mobiles_subform.Form.FilterOn = False
End Sub
Private Sub Text1_AfterUpdate()
Me.Mobiles_subform.Form.Filter = ""
Me.Mobiles_subform.Form.FilterOn = False
Me.Mobiles_subform.Form.Filter = "[Brand] Like'*" & Me.Text1.Value & "*'"
Me.Mobiles_subform.Form.FilterOn = True
End Sub
Remark: Filter is provided a double quoted criteria. In case of string values, we quote the string by single or double quotes. Similarly, date values are placed between # sign. The criteria are basically WHERE clause of SQL.
AFTER FILTER:
Suppose that a text box is placed in the main form and user inputs data into it to get the filtered records from the subform. To achieve this filter, we use the following codes in VBE environment.
The first sub routine in on the Clear Filter Button to remove filter from the subform.
The second sub routine is on the text box used for search and the AfterUpdate event is executed when user submits any value in the text box.
Option Compare Database
Option Explicit
Private Sub cmdClearFilter_Click()
Me.Text1.Value = ""
Me.Mobiles_subform.Form.FilterOn = False
End Sub
Private Sub Text1_AfterUpdate()
Me.Mobiles_subform.Form.Filter = ""
Me.Mobiles_subform.Form.FilterOn = False
Me.Mobiles_subform.Form.Filter = "[Brand] Like'*" & Me.Text1.Value & "*'"
Me.Mobiles_subform.Form.FilterOn = True
End Sub
Remark: Filter is provided a double quoted criteria. In case of string values, we quote the string by single or double quotes. Similarly, date values are placed between # sign. The criteria are basically WHERE clause of SQL.
AFTER FILTER:
No comments:
Post a Comment