Examples of Expressions ACCESS
Examples of Expressions ACCESS
Applies To: Access 2016 , Access 2013 , Access 2010 , Access 2007
Note: Although this article provides basic steps for creating expressions, it is not a
comprehensive guide for using the tools that Access provides for creating expressions. For
more information about creating expressions, see the article Create an expression.
In this article
Understand expressions
Understand expressions
In Access, the term expression is synonymous with formula. An expression consists of a
number of possible elements that you can use, alone or in combination, to produce a result.
Those elements include:
You can use expressions in a number of ways — to perform a calculation, retrieve the value
of a control, or supply criteria to a query, just to name a few.
For more information about how and where to use expressions, see the article Create an
expression.
Top of Page
Examples of expressions used in forms and reports
The tables in this section provide examples of expressions that calculate a value in a control
located on a form or report. To create a calculated control, you enter an expression in the
ControlSource property of the control, instead of in a table field or query.
The following steps explain how to enter an expression in a text box control on an existing
form or report.
1. In the Navigation Pane, right-click the form or report that you want to change and
click Design View on the shortcut menu.
2. On the form or report, right-click the text box control that you want to change (not the
label associated with the text box), and then click Properties on the shortcut menu.
3. If necessary, click either the All tab or the Data tab. Both tabs provide the Control
Source property.
4. Click the box next to the Control Source property and type your expression. For
example, you can copy and paste an expression from the Expression column in the
table in the following section.
5. Close the property sheet.
Expression Result
="N/A" Displays N/A.
Displays the values that reside in table fields called
FirstName and LastName. In this example, the &
=[FirstName] & " " & [LastName] operator is used to combine the FirstName field, a space
character (enclosed in quotation marks), and the
LastName field.
Uses the Left function to display the first character of
=Left([ProductName], 1)
the value of a field or control called ProductName.
Uses the Right function to display the last 2 characters
=Right([AssetCode], 2)
of the value in a field or control called AssetCode.
Uses the Trim function to display the value of the
=Trim([Address]) Address control, removing any leading or trailing
spaces.
Uses the IIf function to display the values of the City
=IIf(IsNull([Region]), [City] & " " and PostalCode controls if the value in the Region
& [PostalCode], [City] & " " & control is null; otherwise, it displays the values of the
[Region] & " " & [PostalCode]) City, Region, and PostalCode controls, separated by
spaces.
Expression Result
Uses the + operator and null propagation to display the
values of the City and PostalCode controls if the value
in the Region field or control is null; otherwise, it
displays the values of the City, Region, and PostalCode
=[City] & (" " + [Region]) & " " & fields or controls, separated by spaces.
[PostalCode]
Null propagation means that if any component of an
expression is null, the entire expression is also null. The
+ operator supports null propagation; the & operator
does not.
For more information about using headers and footers in forms and reports, see the article
Insert page numbers into a form or report.
Expression Result
The sum of the values of the Subtotal and Freight
=[Subtotal]+[Freight]
fields or controls.
The interval between the date values of the
=[RequiredDate]-[ShippedDate]
RequiredDate and ShippedDate fields or controls.
Expression Result
The product of the value of the Price field or
=[Price]*1.06 control and 1.06 (adds 6 percent to the Price
value).
The product of the values of the Quantity and
=[Quantity]*[Price]
Price fields or controls.
The quotient of the values of the EmployeeTotal
=[EmployeeTotal]/[CountryRegionTotal]
and CountryRegionTotal fields or controls.
Note: When you use an arithmetic operator (+, -, *, and /) in an expression, and the value of
one of the controls in the expression is null, the result of the entire expression will be null —
this is known as Null propagation. If any records in one of the controls that you use in the
expression might have a null value, you can avoid Null propagation by converting the null
value to zero by using the Nz function — for example, =Nz([Subtotal])+Nz([Freight]).
For more information about the function, see the article Nz Function.
The following table lists examples of expressions that you can use in calculated controls on
forms.
Expression Result
The value of the OrderID control on the Orders
=Forms![Orders]![OrderID]
form.
=Forms![Orders]![Orders The value of the OrderSubtotal control on the
Subform].Form![OrderSubtotal] subform named Orders Subform on the Orders form.
The value of the third column in ProductID, a
multiple-column list box on the subform named
=Forms![Orders]![Orders
Orders Subform on the Orders form. (Note that 0
Subform]![ProductID].Column(2)
refers to the first column, 1 refers to the second
column, and so on.)
The product of the value of the Price control on the
=Forms![Orders]![Orders subform named Orders Subform on the Orders form
Subform]![Price] * 1.06 and 1.06 (adds 6 percent to the value of the Price
control).
The value of the OrderID control on the main or
=Parent![OrderID]
parent form of the current subform.
The expressions in the following table show some ways to use calculated controls on reports.
The expressions reference the Report property.
For more information about that property, see the article Report Property.
Expression Result
The value of a control called "OrderID" in a
=Report![Invoice]![OrderID]
report called "Invoice."
The value of the SalesTotal control on the
=Report![Summary]![Summary
subreport named Summary Subreport on the
Subreport]![SalesTotal]
Summary report.
The value of the OrderID control on the main or
=Parent![OrderID]
parent report of the current subreport.
The expressions in the following table show some of the ways to use functions such as Avg,
Count, and Sum.
Expression Description
Uses the Avg function to display the average of the values of a
=Avg([Freight])
table field or control named "Freight."
Uses the Count function to display the number of records in the
=Count([OrderID])
OrderID control.
Uses the Sum function to display the sum of the values of the
=Sum([Sales])
Sales control.
Uses the Sum function to display the sum of the product of the
=Sum([Quantity]*[Price])
values of the Quantity and the Price controls.
Displays the percentage of sales, determined by dividing the
value of the Sales control by the sum of all the values of the
Sales control.
=[Sales]/Sum([Sales])*100
Note: If you set the Format property of the control to Percent,
do not include *100 in the expression.
For more information about using aggregate functions and totaling the values in field and
columns, see the articles Sum data by using a query, Count data by using a query, Count the
rows in a datasheet, and Display column totals in a datasheet.
Expression Description
Uses the Date function to display the current date in the form of
mm-dd-yy, where mm is the month (1 through 12), dd is the day
=Date()
(1 through 31), and yy is the last two digits of the year (1980
through 2099).
Uses the Format function to display the week number of the year
=Format(Now(), "ww")
for the current date, where ww represents weeks 1 through 53.
=DatePart("yyyy", Uses the DatePart function to display the four-digit year of the
[OrderDate]) value of the OrderDate control.
=DateAdd("y", -10, Uses the DateAdd function to display a date that is 10 days before
[PromisedDate]) the value of the PromisedDate control.
=DateDiff("d", Uses the DateDiff function to display the number of days'
[OrderDate], difference between the values of the OrderDate and ShippedDate
[ShippedDate]) controls.
Uses arithmetic operations on dates to calculate the date 30 days
=[InvoiceDate] + 30
after the date in the InvoiceDate field or control.
Conditional expressions that return one of two possible
values
The example expressions in the following table use the IIf function to return one of two
possible values. You pass the IIf function three arguments: The first argument is an
expression that must return a True or False value. The second argument is the value to return
if the expression is true, and the third argument is the value to return if the expression is false.
Expression Description
Uses the IIf (Immediate If) function to display
=IIf([Confirmed] = "Yes", "Order the message "Order Confirmed" if the value of
Confirmed", "Order Not Confirmed") the Confirmed control is Yes; otherwise, it
displays the message "Order Not Confirmed."
Uses the IIf and IsNull functions to display an
=IIf(IsNull([Country/region]), " ", empty string if the value of the Country/region
[Country]) control is null; otherwise, it displays the value of
the Country/region control.
Uses the IIf and IsNull functions to display the
=IIf(IsNull([Region]), [City] & " " & values of the City and PostalCode controls if the
[PostalCode], [City] & " " & [Region] & " value in the Region control is null; otherwise, it
" & [PostalCode]) displays the values of the City, Region, and
PostalCode fields or controls.
Uses the IIf and IsNull functions to display the
=IIf(IsNull([RequiredDate]) Or message "Check for a missing date" if the result
IsNull([ShippedDate]), "Check for a of subtracting ShippedDate from RequiredDate is
missing date", [RequiredDate] - null; otherwise, it displays the interval between
[ShippedDate]) the date values of the RequiredDate and
ShippedDate controls.
Top of Page
You use criteria in a query to limit the records that you work with. For example, you can use
the Between operator to supply a starting and ending date and limit the results of your query
to orders that were shipped between those dates.
The following sections explain how to add a calculated field to a query and provide examples
of expressions for use in queries.
-or-
On the Design tab, in the Query Setup group, click Builder to start the Expression
Builder.
For more information about using the Expression Builder, see the article Create an
expression.
Expression Description
Creates a field called FullName that displays the
FullName: [FirstName] & " " &
values in the FirstName and LastName fields,
[LastName]
separated by a space.
Creates a field called Address2 that displays the
Address2: [City] & " " & [Region] & " "
values in the City, Region, and PostalCode fields,
& [PostalCode]
separated by spaces.
Creates a field called ProductInitial, and then uses
the Left function to display, in the ProductInitial
ProductInitial:Left([ProductName], 1)
field, the first character of the value in the
ProductName field.
Creates a field called TypeCode, and then uses the
TypeCode: Right([AssetCode], 2) Right function to display the last two characters of
the values in the AssetCode field.
Creates a field called AreaCode, and then uses the
Mid function to display the three characters
AreaCode: Mid([Phone],2,3)
starting with the second character of the value in
the Phone field.
For more information about using aggregate functions and totaling the values in field and
columns, see the articles Sum data by using a query, Count data by using a query, Count the
rows in a datasheet, and Display column totals in a datasheet.
Expression Description
Creates a field called LagTime, and then uses the
LagTime: DateDiff("d",
DateDiff function to display the number of days
[OrderDate], [ShippedDate])
between the order date and ship date.
Expression Description
Creates a field called YearHired, and then uses the
YearHired:
DatePart function to display the year each employee
DatePart("yyyy",[HireDate])
was hired.
Creates a field called MinusThirty, and then uses the
MinusThirty: Date( )- 30 Date function to display the date 30 days prior to the
current date.
In addition to aggregate functions, Access also provides "domain" aggregate functions that
you use to sum or count values selectively. For example, you can count only the values
within a certain range or look up a value from another table. The set of domain aggregate
functions includes the DSum Function, the DCount Function, and the DAvg Function.
To calculate totals, you will often need to create a totals query. For example, to summarize by
group, you need to use a Totals query. To enable a Totals query from the query design grid,
click Totals on the View menu.
Expression Description
Creates a field called RowCount, and then uses the
RowCount:Count(*) Count function to count the number of records in the
query, including records with null (blank) fields.
Creates a field called FreightPercentage, and then
calculates the percentage of freight charges in each
subtotal by dividing the sum of the values in the Freight
field by the sum of the values in the Subtotal field.
(This example uses the Sum function.)
FreightPercentage:
Sum([Freight])/Sum([Subtotal])
Note: You must use this expression with a Totals
*100
query. If you set the Format property of the field to
Percent, do not include *100.
Expression Description
Creates a field called
CurrentCountryRegion, and then
uses the IIf and IsNull functions to
CurrentCountryRegion:IIf(IsNull([CountryRegion]), " display an empty string in that field
", [CountryRegion]) when the CountryRegion field
contains a null value; otherwise, it
displays the contents of the
CountryRegion field.
Creates a field called LeadTime,
and then uses the IIf and IsNull
functions to display the message
LeadTime: IIf(IsNull([RequiredDate] - [ShippedDate]),
"Check for a missing date" if the
"Check for a missing date", [RequiredDate] -
value in either the RequiredDate
[ShippedDate])
field or the ShippedDate field is
null; otherwise, it displays the date
difference.
Creates a field called
SixMonthSales, and then displays
the total of the values in the
SixMonthSales: Nz([Qtr1Sales]) + Nz([Qtr2Sales])
Qtr1Sales and Qtr2Sales fields by
first using the Nz function to
convert any null values to zero.
Expression Description
Creates a field called Cat, and
then displays the
Cat: (SELECT [CategoryName] FROM [Categories] CategoryName, if the
WHERE CategoryID from the
[Products].[CategoryID]=[Categories].[CategoryID]) Categories table is the same as
the CategoryID from the
Products table.
Expressions that define criteria and limit the records in
the result set
You can use an expression to define criteria for a query. Access then returns only those rows
that match the criteria. The steps in this section provide basic information about adding
criteria to a query, and the tables in this section provide examples of criteria for matching text
and date values.
1. In the Navigation Pane, right-click the query that you want to change and click
Design View on the shortcut menu.
2. In the Criteria row of the design grid, click the cell in the column that you want to
use, and then type your criteria.
If you want a larger area in which to type an expression, press SHIFT+F2 to display
the Zoom box.
-or-
On the Design tab, in the Query Setup group, click Builder to start the
Expression Builder and create your expression.
Note: When you create expressions that define criteria, do not precede the expressions with
the = operator.
For more information about using the Expression Builder, see the article Create an
expression.
For more information about entering and using date values, see the article Enter a date or
time value. For information about using the functions in these sample expressions, click the
links to the various function topics.
For more information about creating update queries, see the article Create an update query.
Top of Page
1. In the Navigation Pane, right-click the table that you want to change and click Design
View on the shortcut menu.
2. Click the field that you want to change, and on the General tab, click the Default
Value property box.
3. Type the expression, or click the Build button to the right of the property box to
create an expression by using the Expression Builder.
If a control is bound to a field in a table, and the field has a default value, the default value of
the control takes precedence.
Top of Page
1. In the Navigation Pane, right-click the table that you want to change and click Design
View on the shortcut menu.
2. Click the field that you want to change.
3. Click the Validation Rule property box, located in the lower section of the table
designer.
4. Type the expression, or click the Build button to the right of the property box to
create an expression by using the Expression Builder.
Note: Do not precede the expression with the = operator when you create a validation
rule.
The examples in the following table demonstrate the validation rule expressions for the
ValidationRule property and the associated text for the ValidationText property.
For more information about validating data, see the article Create a validation rule to validate
data in a field.
Top of Page
1. In the Navigation Pane, right-click the macro that you want to change and click
Design View on the shortcut menu.
2. If you don't see the Condition column in the macro designer, on the Design tab, in
the Show/Hide group, click Conditions.
3. Click the Condition cell for the macro action that you want to change, and then type
your conditional expression.
4. Save your changes and close the macro.