Académique Documents
Professionnel Documents
Culture Documents
Page 1 of 10
Examples of expressions
Page 2 of 10
Hide All
Examples of expressions
You use expressions (expression: Any combination of mathematical or logical operators, constants, functions,
and names of fields, controls, and properties that evaluates to a single value. Expressions can perform
calculations, manipulate characters, or test data.)in many operations in Microsoft Access, including creating
calculated controls, query and filter criteria, default values, validation rules, and macro conditions. Often, the
easiest way to create an expression is to find an example of an expression that is similar to what you want to
create, and then modify it to do what you want.
Examples of expressions used in forms, reports, and data access pages
Examples of manipulating and calculating dates
The following table lists examples of expressions that you can use in calculated controls (calculated control: A
control used on a form, report, or data access page to display the result of an expression. The result is
recalculated each time there is a change in any of the values that the expression is based on.) on forms,
reports, and data access pages (data access page: A Web page, published from Access, that has a connection
to a database. In a data access page, you can view, add to, edit, and manipulate the data stored in the
database. A page can also include data from other sources, such as Excel.).
Expression
Description
=Date()
Uses the Date function to display the current date in the form of mm-dd-yyyy,
where mm is the month (1 through 12), dd is the day (1 through 31), and yyyy
is the year (1980 through 2099).
=Format(Now(), "ww")
Uses the Format function to display the number of the week of the year the
current date represents, where ww is 1 through 53.
=DatePart("yyyy",
[OrderDate])
Uses the DatePart function to display the four-digit year of the value of the
OrderDate field.
=DateAdd("y", -10,
[PromisedDate])
Uses the DateAdd function to display a date that is 10 days before the value of
the PromisedDate field.
=DateDiff("d",
[OrderDate],
[ShippedDate])
Uses the DateDiff function to display the variance in days between the values
of the OrderDate and ShippedDate fields.
Notes
In the ControlSource property of a calculated control, precede the expression with the = operator. On a
data access page, you can omit the = operator, and type an alias instead; for example, type WeekNumber:
Format(Now(), "ww").
When you set the Name property of a calculated control in a form or report, or set the ID property of a
calculated control in a data access page, make sure that you use a unique name. Don't use the name or ID of
one of the controls you used in the expression.
In an expression on a form or report, you can use the name of a control or the name of a field in the
underlying records source. In an expression on a data access page, you can use only the name of a field
that's in the data definition of the page.
Examples of manipulating text values
The following table lists examples of expressions that you can use in calculated controls on forms, reports, and
data access pages.
Expression
Description
="N/A"
Displays N/A.
=[FirstName]&" "&[LastName]
=Left([ProductName], 1)
Uses the Left function to display the first character of the value of
the ProductName field.
=Right([AssetCode], 2)
Uses the Right function to display the last 2 characters of the value
of the AssetCode field.
=Trim([Address])
Uses the Trim function to display the value of the Address field,
removing any leading or trailing spaces.
=IIf(IsNull([Region]),[City]&" "&
[PostalCode],[City]&" "&[Region]&"
"& [PostalCode])
Uses the IIf function to display the values of the City and
PostalCode fields if Region is Null; otherwise, it displays the values
of the City, Region, and PostalCode fields, separated by spaces.
Notes
In the ControlSource property of a calculated control, precede the expression with the = operator. On a
data access page, you can omit the = operator, and type an alias instead; for example, type FullName:
[FirstName]&" "& [LastName] .
When you set the Name property of a calculated control in a form or report, or set the ID property of a
calculated control in a data access page, make sure to use a unique name. Don't use the name or ID of one
of the controls that you used in the expression.
In an expression on a form or report, you can use the name of a control or field in the underlying record
source. In an expression on a data access page, you can use only the name of a field that's in the data
definition of that page.
Examples of manipulating arithmetic operations
The following table lists examples of expressions that you can use in calculated controls on forms, reports, and
data access pages.
Expression
Description
=[Subtotal] + [Freight]
=[RequiredDate] [ShippedDate]
=[Price] * 1.06
The product of the value of the Price field and 1.06 (adds 6 percent to the
Price value).
=[Quantity] * [Price]
=[EmployeeTotal] /
[CountryTotal]
Notes
In the ControlSource property of a calculated control, precede the expression with the = operator. On a
data access page, you can omit the = operator, and type an alias instead; for example, type
ExtendedPrice: [Quantity] * [Price] .
When you set the Name property of a calculated control in a form or report, or set the ID property of a
calculated control on a data access page, make sure to use a unique name. Don't use the name or ID of one
of the controls that you used in the expression.
In an expression on a form or report, you can use the name of a control or the name of a field in the
underlying record source. In an expression on a data access page, you can use only the name of a field that's
in the data definition of the page.
When you use an arithmetic operator (+, -, *, /) 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. On a form or report, if some records in
one of the controls that you used in the expression might have a Null value, you can convert the Null value
to zero by using the Nz function; for example:
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExamplesS.htm
... 1/30/03
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExam
... 1/30/03
Examples of expressions
Page 3 of 10
Examples of expressions
Page 4 of 10
=Msodsc.Nz([Subtotal]) + Msodsc.Nz([Freight])
Expression
Description
=DLookup("[ContactName]", "[Suppliers]",
"[SupplierID] = Forms![SupplierID]")
=DLookup("[ContactName]", "[Suppliers]",
"[SupplierID] = Forms![New Suppliers]![SupplierID]")
The following table lists examples of expressions that you can use in calculated controls on forms, reports, and
data access pages.
Expression
Description
=IIf(IsNull([Region]),[City]&" "&
[PostalCode], [City]&" "&[Region]&" "
&[PostalCode])
Notes
In a calculated control, precede the expression with the = operator.
When you set the Name property of a calculated control, make sure you use a unique name. Don't use the
name of one of the controls you used in the expression.
Examples of expressions used in queries and filters
Notes
In the ControlSource property of a calculated control, precede the expression with the = operator. On a
data access page, you can omit the = operator, and type an alias instead; for example, type
DisplayCountry: IIf(IsNull([Country])," ",[Country]) .
When you set the Name property of a calculated control in a form or report, or set the ID property of a
calculated control in a data access page, make sure to use a unique name. Don't use the name or ID of one
of the controls that you used in the expression.
In an expression on a form or report, you can use the name of a control or the name of a field in the
underlying record source. In an expression on a data access page, you can use only the name of a field that's
in the data definition of the page.
Examples of aggregate and domain aggregate functions
Note You can't use aggregate and domain aggregate functions in a data access page. Additionally, you can't
use domain aggregate functions in a Microsoft Access project (.adp) (Microsoft Access project: An Access file
that connects to a Microsoft SQL Server database and is used to create client/server applications. A project file
doesn't contain any data or data -definition-based objects such as tables and views.).
Examples of expressions that use aggregate functions
Expression
Description
=Avg([Freight])
Uses the Avg function to display the average of the values of the Freight control.
=Count([OrderID])
Uses the Count function to display the number of records in the OrderID control.
=Sum([Sales])
Uses the Sum function to display the sum of the values of the Sales control.
=Sum([Quantity] *
[Price])
Uses the Sum function to display the sum of the product of the values of the
Quantity and Price controls.
=[Sales] / Sum
([Sales]) * 100
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.
Note
If the control's Format property is set to Percent, don't include the *100.
Result
> 234
< 1200.45
>= "Callahan"
For a LastName field, all names from Callahan through the end of the
alphabet
For a ShipCountry field, orders shipped to countries other than the USA
Not 2
Not T*
For a LastName field, employees whose names don't start with the letter "T" (ANSI -89)
Not T%
For a LastName field, employees whose names don't start with the letter "T" (ANSI -92)
Result
In("Canada", "UK")
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExamplesS.htm
... 1/30/03
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExam
... 1/30/03
Examples of expressions
Page 5 of 10
In(France, Germany, Japan) For a CountryName field, employees living in France or Germany or Japan
Text, partial, and matching values
Expression
Result
"London"
>="N"
Like "S*"
For a ShipName field, orders shipped to customers whose name starts with the
letter S (ANSI-89)
Examples of expressions
Page 6 of 10
Expression
Result
#2/2/2000#
'2/2/2000'
Date()
< Date( ) - 30
Year([OrderDate]) = 1999
Like "S%"
For a ShipName field, orders shipped to customers whose name starts with the
letter S (ANSI-92)
DatePart("q", [OrderDate]) = 4
Right([OrderID], 2) = "99"
For a CompanyName field, orders for companies whose name is more than 30
characters long
Result
Like "S*"
For a ShipName field, orders shipped to customers whose names start with the letter S
(ANSI-89)
Like "S%"
For a ShipName field, orders shipped to customers whose names start with the letter S
(ANSI-92)
Like "*Imports"
For a ShipName field, orders shipped to customers whose names end with the word
"Imports" (ANSI-89)
Like "%
Imports"
For a ShipName field, orders shipped to customers whose names end with the word
"Imports" (ANSI-92)
Like "[A-D]*"
For a ShipName field, orders shipped to customers whose names start with A through D
(ANSI-89)
Like "[A-D]%"
For a ShipName field, orders shipped to customers whose names start with A through D
(ANSI-92)
Like "*ar*"
For a ShipName field, orders shipped to customers whose names include the letter
sequence "ar" (ANSI-89)
Like "%ar%"
For a ShipName field, orders shipped to customers whose names include the letter
sequence "ar" (ANSI-92)
Like "Maison
Dewe?"
For a ShipName field, orders shipped to the customer with "Maison" as the first part of its
name and a 5-letter second name in which the first 4 letters are "Dewe" and the last letter
is unknown (ANSI -89)
Like "Maison
Dewe_"
For a ShipName field, orders shipped to the customer with "Maison" as the first part of its
name and a 5-letter second name in which the first 4 letters are "Dewe" and the last letter
is unknown (ANSI -92)
Dates
Expression Result
Is Null
For a ShipRegion field, orders for customers whose ShipRegion field is Null (blank)
Is Not Null
For a ShipRegion field, orders for customers whose ShipRegion field contains a value
""
For a Fax field, orders for customers who don't have a fax machine, indicated by a zero-length
string value in the Fax field instead of a Null (blank) value
Result
For a Freight field, orders for which the freight cost rose above the
mean plus the standard deviation for freight cost
Result
For a UnitPrice field, products whose price is the same as the price of
Aniseed Syrup
For a UnitPrice field, products that have a unit price above the average
For a Salary field, the salary of every sales representative whose salary
is higher than that of all employees with "Manager" or "Vice President"
in their titles
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExamplesS.htm
... 1/30/03
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExam
... 1/30/03
Examples of expressions
Page 7 of 10
Examples of expressions
Page 8 of 10
Displays in the YearHired field the year each employee was hired
PriorDate: Date( ) - 30
Expression
Result
Displays in the Address2 field the value of the City, Region, and
PostalCode fields, separated by spaces
ProductInitial: Left([ProductName],
1)
TypeCode: Right([AssetCode], 2)
Displays in the TypeCode field the last two characters of the value in
the AssetCode field
AreaCode: Mid([Phone], 2, 3)
Displays in the AreaCode field the three characters starting with the
second character of the value in the Phone field
Result
Count(*)
Uses the Count function to count the number of records in the query,
including records with Null (blank) fields
FreightPercentage: Sum
([Freight]) / Sum([Subtotal]) *
100
Result
PrimeFreight: [Freight] *
1.1
OrderAmount: [Quantity] *
[UnitPrice]
Displays in the OrderAmount field the product of the values in the Quantity and
UnitPrice fields
Expression
Result
LeadTime: [RequiredDate]
- [ShippedDate]
Displays in the LeadTime field the difference between the values in the
RequiredDate and ShippedDate fields
TotalStock: [UnitsInStock]
+ [UnitsOnOrder]
Displays in the TotalStock field the sum of the values in the UnitsInStock and
UnitsOnOrder fields
FreightPercentage: Sum
([Freight]) / Sum
([Subtotal]) * 100
SixMonthSales: Nz([Qtr1Sales], 0) + Nz
([Qtr2Sales], 0)
AverageFreight: DAvg
("[Freight]", "[Orders]")
The Total row in the design grid (design grid: The grid that you use to design
a query or filter in query Design view or in the Advanced Filter/Sort window. For
queries, this grid was formerly known as the QBE grid.) must be displayed, and
the Total cell for this field must be set to Expression.
Use a subquery
If the Format property of the field is set to Percent, don't include the *100.
SumofUnits: Nz
([UnitsInStock], 0) + Nz
([UnitsOnOrder], 0)
Displays in the SumofUnits field the sum of all units in stock and on order.
When you use an arithmetic operator (+, -, *, /) in an expression and the
value of one of the fields in the expression is Null, the result of the entire
expression will be Null. If some records in one of the fields you used in the
expression might have a Null value, you can convert the Null value to zero
using the Nz function.
Expression
Result
Result
Expression
Result
"Salesperson"
#8/10/99#
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExamplesS.htm
... 1/30/03
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExam
... 1/30/03
Examples of expressions
Page 9 of 10
Examples of expressions
Page 10 of 10
incomplete.). The macro will be executed if the condition evaluates to True (or Yes).
"PN" & [PartNumber]
[UnitPrice] * [Quantity]
[Freight] * 1.5
DSum("[Quantity] *
[UnitPrice]",
"Order Details", "[ProductID]
=" & [ProductID])
Where the Product IDs in the current table match the Product IDs in the
Order Details table, updates sales totals based on the product of Quantity
and UnitPrice
Right([ShipPostalCode], 5)
IIf(IsNull([UnitPrice]), 0,
[UnitPrice])
Tip
To cause Microsoft Access to temporarily ignore an action (action: The basic building block of a macro; a self contained instruction that can be combined with other actions to automate tasks. This is sometimes called a
command in other macro languages.), enter False as a condition. Temporarily ignoring an action can be helpful
when you are trying to find problems in a macro.
Use this expression
[City]="Paris"
Paris is the City value in the field on the form from which the macro
was run.
DCount("[OrderID]", "Orders")>35
There are more than 35 entries in the OrderID field of the Orders
table.
There are more than three entries in the Order Details table for which
the OrderID field of the table matches the OrderID field on the Orders
form.
The value of the ShippedDate field on the form from which the macro
is run is no earlier than 2-Feb-2001 and no later than 2 -Mar-2001.
Forms![Products]![UnitsInStock]<5
The value of the UnitsInStock field on the Products form is less than
five.
Expression
Result
IsNull([FirstName])
The FirstName value on the form from which the macro is run is Null
(has no value). This expression is equivalent to [FirstName] Is Null.
The value in the Country field on the form from which the macro is
run is UK, and the value of the TotalOrds field on the SalesTotals form
is greater than 100.
The value in the Country field on the form from which the macro is
run is France, Italy, or Spain, and the postal code isn't five characters
long.
SELECT [CategoryID],Count([ProductID])
AS [CountOfProductID]
FROM [Products] GROUP BY [CategoryID]
HAVING Count([ProductID]) > 10;
MsgBox("Confirm changes?",1)=1
"MT"
MT
New York, N.Y. (note that you must enclose the value in quotes if it includes punctuation)
""
Zero-length string
Date( )
Today's date
=Yes
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExamplesS.htm
... 1/30/03
mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office10\1033\acmain10.chm::/html/acdecExpressionExam
... 1/30/03