Shown below is a typical example of an Access Query which combines the use of IIF statements and form controls to deliver a dataset controlled by what a user has selected in a form.
The first example shows a very simple query which refers to a screen field on a form
The next example is more complicated, the additional complexity using IIF statements allows the form parameters to be optionally supplied by the user.
SELECT IIf(Not IsNull([Forms]![frmPrompt]![txtTheDate]),IIf([Forms]![frmPrompt]![txtTheDate]<[Date],1,0),1) AS Expr1,This article explains a method for converting queries like this to use either Pass-Through Queries or Views on SQL Server.
The first step is to create a table in SQL Server which will hold a users parameters, this table will be shared by all users, but a filter will be used to ensure that each user only works with there selections.
-- Create a table into which parameters will be insertedNext we create the user specific view, into which a user will update their parameters.
-- Create a View through which a user can see their parametersIn this case we could use either a Pass Through Query, or as shown below create a view on SQL Server, and then link the view to Access.
The first example shows how we do this for the simpler query, notice how the parameters view does not have any joining fields to the main table (called a cross-join or cartesian product), this projects the parameter against each row in the target table acting to filter the rows.
CREATE VIEW vw_ResultsSimpleFor the more sophisticated query allowing optional paramters we use a COALESCE functions to ensure that the view works when any combination of parameters are completed by a user. IF for example a user does not complete the country parameter then the COALESCE function (which takes the first not null value) becomes ' ' = ' ' which will always be true and thus ignores the filter, but if the country parameter has a value 'France', then the function becomes equivalent to 'France' = [Country].
CREATE VIEW vw_ResultsFor a numerical field we could use COALESCE ([ANumber], 0) >= COALESCE ([SelectedNumber], [ANumber], 0)
The next step is to link Access to the views.
If you don’t want to write any program code, and the form is not already bound to another table or query, then the prompting form needs simply to be bound to the vw_tblParameters, and you need to make sure that if a parameter is not supplied then the field contains a value of null.
If you have a more complex form, then you need to use some programming code to open the vw_tblParameters, clear all unused parameters to NULL, and save all the screen parameters.
One final comment is that the view of the parameters table must remain updateable, some program code is provided in our article on Relinking Tables And Views to help with this if you need to relink the view.
The sample code can be downloaded from here Access 2000 Database.
by Andrew Couch Office Access MVP, Director ASC Associates
andy@ascassociates.biz