ACC: How to Dynamically Create a FilterID: Q109334
|
Moderate: Requires basic macro, coding, and interoperability skills.
This article describes a method of dynamically creating a filter by
applying expressions on a form using a macro. The same technique can
also be adapted to a Query by Form (QBF) method.
NOTE: Although this article will work with Microsoft Access 7.0 and
97, a new feature has been introduced called Filter by Form. For
more information on this new feature, query on the words "Filter by Form"
using the Microsoft Access 97 Help Index.
The "+" and "&" operators are used to concatenate (join) two string
values. However, each operator behaves differently when used on Null-
valued strings. For example, if you use a Null variable "Name" in the
expression "Dear "+[Name], then the expression evaluates to Null,
whereas if you have the expression "Dear "&[Name], then the expression
evaluates to "Dear ."
You can use these different behaviors to build a string for use in the
Where condition of an ApplyFilter or OpenForm macro action. You combine
the strings using the + operator to concatenate values to dynamically
build each separate part of the filter (which becomes Null if no value
is entered), and then use the & operator to concatenate the separate
strings.
To dynamically create a filter by applying expressions on a form by using a
macro, follow these steps:
Label
------------------
Caption: Search ID
Text Box
-------------------------------
Name: SID
ControlSource: <leave blank>
AfterUpdate: Filter.Make null 2
NOTE: In versions 1.x, the Name property is called the ControlName
property.
Label
--------------------------
Caption: Search First Name
Text Box
-----------------------------------------------------
Name: SName
ControlSource: <leave blank>
Validation Rule: Not Like "*'*" Or Is Null
Validation Text: Can't use apostrophe in filter text.
AfterUpdate: Filter.Make null
NOTE: In the following example, an underscore (_) is used as a
line-continuation character. Remove the underscore when re-creating
this example.
In Microsoft Access 7.0:
Text Box
-------------------------------------------------------------------
Name: Where Text
ControlSource: =Mid((" And [Employee ID]="+[SID]) & _
(" And [First Name] Like '"+[SName]+"'"),2)
In Microsoft Access 1.x, 2.0 and 97:
Text Box
-------------------------------------------------------------------
Name: Where Text
ControlSource: =Mid((" And [Employee ID]="+[SID]) & _
(" And [First Name] Like '"+[SName]+"'"),6)
Command Button
---------------------------
Caption: Apply Filter
OnClick: Filter.Apply Filter
NOTE: In versions 1.x, the OnClick property is called the OnPush
property.
Macro Group: Filter
--------------------------------------------------------------
Macro Name: Make null
Condition: IsNull(Screen.ActiveControl)
Action: StopMacro
Description: This macro is for text fields.
Condition: Len(Screen.ActiveControl)=0
Action: SetValue
Item: Screen.ActiveControl
Expression: Null
Description: Make an empty text field null.
Macro Name: Make null 2
Condition: IsNull(Screen.ActiveControl)
Action: StopMacro
Description: This macro is for numeric fields.
Condition: Not IsNumeric(Screen.ActiveControl)
Action: SetValue
Item: Screen.ActiveControl
Expression: Null
Description: Turn non-numeric values into nulls.
Macro Name: Apply Filter
Condition: IsNull([Where Text])
Action: ApplyFilter
Where Condition: True
Description: No Where Text, so select all records and stop.
Condition: ...
Action: StopMacro
Condition: Len([Where Text]) > 255
Action: MsgBox
Message: You have selected too many criteria in the filter.
Title: Apply Filter
Description: If filter too long, display warning and stop.
Condition: ...
Action: StopMacro
Action: ApplyFilter
Where Condition: =[Where Text]
Description: Apply the filter.
Additional query words: dynamic QBE query by example
Keywords : kbusage McrActn
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: March 30, 1999