Locked lesson.
About this lesson
Use criteria from a Form Control when opening a report with VBA.
Exercise files
Download this lesson’s related exercise files.
Services_37_Start Open Report with Criteria , VBA.accdb1.8 MB Services_37_Open Report with Criteria , VBA.accdb
1.8 MB
Quick reference
Open Report with Criteria, VBA
VBA
Use Criteria from a Form Control when Opening a Report
This code opens a report and filters it for criteria, if any is specified. It also passes a friendly string with criteria written in a way that someone looking at the report can understand.
This code is executed on the Click event of specified control and is in the code behind the form object.
The control name that launches this code is called cmd_RptCustomerServicesByYear.
Two variables, vWhere and vFriendly, are dimensioned as variants so that the code can use them and know what kind of data to expect. Variants are allowed to 'be' Null, which means no value.
vWhere is a variable containing the criteria the user wants to limit the report for. It will be used in the OpenReport statement.
vFriendly is a variable containing a friendly version of the report criteria and will be passed to the report object in the OpenReport statement.
The two variables are both initialized to have no value, which is referenced as Null.
This code checks to see if a control called CustID is filled out on the form. If it is filled out, vWhere and vFriendly are assigned values.
IsNull(expression) is a function that returns True or False by evaluating the expression.
Not IsNull(expression) means to change True to False and False to true.
If If Not IsNull(Me.CustID) is True then something is filled out in the CustID control.
For instance: If vWhere is equal to 1, then vWhere will be --> CustID = 1
vWhere is a variable containing the Where Condition parameter for the OpenReport action. Refer to the Quick Reference Guide in Lesson 36 for OpenReport parameters.
Me.CustID is a reference to the combo box on the form that this code is behind.
Me.CustID.Column(1) means the value in the second column of the combo box since column indexing starts with 0.
CustID is also the name of a field in the report RecordSource.
If Me.CustID is 1 and the customer corresponding to this value is Alpha Associates, then vFriendly will be --> Customer = Alpha Associates
Once vWhere and vFriendly are determined, the report is opened.
OpenReport is a macro action, which is why it is prefaced with DoCmd.
The Report Name that will be opened is "r_CustomerServicesByYear". Quotes are used as delimiters to mark where the text begins and ends.
acViewPreview is a constant for the View parameter and means that the report will open in Print Preview.
There are 2 commas between acViewPreview and vWhere because the Filter Name argument is being skipped.
The filter for the report is set to vWhere. Data will automatically be filtered without having to do anything else.
vFriendly is being passed to the report in the OpenArgs parameter. OpenArgs stands for Open Arguments and can be anything you want. It is up to the report to use this information.
There are also 2 commas between vWhere and vFriendly because the Window Mode argument is also being skipped.
The Dim and OpenReport statements use line continuation. When a statement is continued on the next line, it ends with
Extra space in the code is okay and helps with readability.
A statement beginning with single quote ( ' ) is a comment and will be ignored by the compiler.
The terms Argument and Parameter can be used interchangeably.
Process Information in OpenArgs when a Report is Opened
When the report is opened, the OpenArgs property is tested to see if it contains anything. If OpenArgs has a value, the caption of a label is set to whatever is in the OpenArgs parameter. Otherwise, the caption of the label is set to a space.
This code is executed on the Open event of the report and is in the code behind the report object.
A statement beginning with single quote ( ' ) is a comment and will be ignored by the compiler.
There is a label control on the report that this code is behind called Label_Criteria
The Caption property of the label will be changed to what is in the OpenArgs parameter. If nothing is there, the label caption will be set to a space.
SQL Statement
SQL stands for Structured Query Language. An SQL Statement can select data from one or more tables, sort and filter. The basic syntax for an SQL statement is:
SELECT fieldlist
FROM tablename
IN anotherdatabase.mdb
WHERE conditions
GROUP BY fieldlist
HAVING conditions for fields that are grouped
ORDER BY fieldlist;
The only necessary phrases to specify are SELECT (what fields to show and calculate) and FROM (what tables or queries the data will come from).
Login to download
Lesson notes are only available for subscribers.