Ekternal Prosedur Aacces
Ekternal Prosedur Aacces
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.
Note You can also use expressions in a form or report when you Highlight data
with conditional formatting.
Text operations
The expressions in the following table use the & (ampersand) and the + (plus)
operators to combine text strings, built-in functions to manipulate a text string, or
otherwise operate on text to create a calculated control.
Expression Result
="N/A" Displays N/A.
=[FirstName] & " " & Displays the values that
[LastName] reside in table fields called
FirstName and LastName.
In this example,
the & operator is used to
combine the FirstName
field, a space character
(enclosed in quotation
marks), and the LastName
field.
=Left([ProductName], 1) Uses the Left function to
display the first character
of the value of a field or
control called
Expression Result
ProductName.
=Right([AssetCode], 2) Uses the Right function to
display the last 2 characters
of the value in a field or
control called AssetCode.
=Trim([Address]) Uses the Trim function to
display the value of the
Address control, removing
any leading or trailing
spaces.
=IIf(IsNull([Region]), Uses the IIf function to
[City] & " " & display the values of the
[PostalCode], [City] & " "
& [Region] & " " & City and PostalCode
[PostalCode]) controls if the value in the
Region control is null;
Expression Result
otherwise, it displays the
values of the City, Region,
and PostalCode controls,
separated by spaces.
=[City] & (" " + [Region]) Uses the + operator and
& " " & [PostalCode] 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 fields or
controls, separated by
spaces.
Expression Result
Top of Page
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
=[Page] 1
="Page " & [Page] Page 1
="Page " & [Page] & " of " & Page 1 of 3
[Pages]
=[Page] & " of " & [Pages] & " 1 of 3 Pages
Pages"
=[Page] & "/" & [Pages] & " Pages" 1/3 Pages
=[Country/region] & " - " & [Page] UK - 1
=Format([Page], "000") 001
Expression Result
="Printed on: " & Date() Printed on:
12/31/17
Top of Page
Arithmetic operations
You can use expressions to add, subtract, multiply, and divide the values in two or
more fields or controls. You can also use expressions to perform arithmetic
operations on dates. For example, suppose you have a Date/Time table field
named RequiredDate. In the field, or in a control bound to the field, the
expression =[RequiredDate] - 2 returns a date/time value equal to two days
before the current values in the RequiredDate field.
Expression Result
=[Subtotal]+[Freight] The sum of the
Expression Result
values of the
Subtotal and
Freight fields or
controls.
=[RequiredDate]-[ShippedDate] The interval
between the date
values of the
RequiredDate and
ShippedDate
fields or controls.
=[Price]*1.06 The product of
the value of the
Price field or
control and 1.06
(adds 6 percent to
Expression Result
the Price value).
=[Quantity]*[Price] The product of
the values of the
Quantity and
Price fields or
controls.
=[EmployeeTotal]/ The quotient of
[CountryRegionTotal] the values of the
EmployeeTotal
and
CountryRegionTot
al 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]).
Top of Page
The following table lists examples of expressions that you can use in calculated
controls on forms.
Expression Result
=Forms![Orders]![OrderID] The value of the
Expression Result
OrderID control on
the Orders form.
=Forms![Orders]![Orders The value of the
Subform].Form![OrderSubtotal] OrderSubtotal control
on the subform
named Orders
Subform on the
Orders form.
=Forms![Orders]![Orders The value of the third
Subform]![ProductID].Column(2) column in ProductID,
a multiple-column list
box on the subform
named Orders
Subform on the
Orders form. (Note
Expression Result
that 0 refers to the
first column, 1 refers
to the second column,
and so on.)
=Forms![Orders]![Orders The product of the
Subform]![Price] * 1.06 value of the Price
control on the
subform named
Orders Subform on
the Orders form and
1.06 (adds 6 percent
to the value of the
Price control).
=Parent![OrderID] The value of the
OrderID control on
Expression Result
the main or 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.
Expression Result
=Report![Invoice]![OrderID] The value of a control
called "OrderID" in a
report called "Invoice."
=Report![Summary]![Summary The value of the
Subreport]![SalesTotal] SalesTotal control on
the subreport named
Summary Subreport on
the Summary report.
Expression Result
=Parent![OrderID] The value of the
OrderID control on the
main or parent report
of the current
subreport.
Top of Page
Expression Description
=Avg([Freight]) Uses the Avg function to
display the average of the
values of a table field or
control named "Freight."
=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
Expression Description
and the 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.
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, Display column totals in a datasheet using a Totals row, and Display
column totals in a datasheet.
Top of Page
Expression Description
=DLookup("[ContactName]", Uses the
"[Suppliers]", "[SupplierID] = " & DLookup
Forms("Suppliers")("[SupplierID]"))
function to
return the
value of
the
Expression Description
ContactNa
me field in
the
Suppliers
table
where the
value of
the
SupplierID
field in the
table
matches
the value
of the
SupplierID
control on
Expression Description
the
Suppliers
form.
=DLookup("[ContactName]", Uses the
"[Suppliers]", "[SupplierID] = " & DLookup
Forms![New Suppliers]![SupplierID])
function to
return the
value of
the
ContactNa
me field in
the
Suppliers
table
where the
value of
Expression Description
the
SupplierID
field in the
table
matches
the value
of the
SupplierID
control on
the New
Suppliers
form.
=DSum("[OrderAmount]", "[Orders]", Uses the
"[CustomerID] = 'RATTC'") DSum
function to
return the
Expression Description
sum total
of the
values in
the
OrderAmo
unt field in
the Orders
table
where the
CustomerI
D is RATTC.
=DCount("[Retired]","[Assets]","[Retire Uses the
d]=Yes") DCount
function to
return the
number of
Expression Description
Yes values
in the
Retired
field (a
Yes/No
field) in the
Assets
table.
Top of Page
Date operations
Tracking dates and times is a fundamental database activity. For example, you can
calculate how many days have elapsed since the invoice date to age your accounts
receivable. You can format dates and times in numerous ways, as shown in the
following table.
Expression Description
=Date() 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 (1 through 31),
and yy is the last two digits of the
year (1980 through 2099).
=Format(Now(), Uses the Format function to display
"ww") the week number of the year for the
current date, where ww represents
weeks 1 through 53.
=DatePart("yyyy", Uses the DatePart function to display
[OrderDate]) the four-digit year of the value of the
OrderDate control.
Expression Description
=DateAdd("y", -10, Uses the DateAdd function to display
[PromisedDate]) a date that is 10 days before the value
of the PromisedDate control.
=DateDiff("d", Uses the DateDiff function to display
[OrderDate], the number of days' difference
[ShippedDate])
between the values of the OrderDate
and ShippedDate controls.
=[InvoiceDate] + 30 Uses arithmetic operations on dates
to calculate the date 30 days after the
date in the InvoiceDate field or
control.
Top of Page
Expression Description
=IIf([Confirmed] = "Yes", "Order Uses the IIf
Confirmed", "Order Not (Immediate If)
Confirmed")
function to display
the message "Order
Confirmed" if the
value of the
Confirmed control
is Yes; otherwise, it
displays the
message "Order
Expression Description
Not Confirmed."
=IIf(IsNull([Country/region]), " Uses the IIf and
", [Country]) IsNull functions to
display an empty
string if the value of
the Country/region
control is null;
otherwise, it displays
the value of the
Country/region
control.
=IIf(IsNull([Region]), [City] & Uses the IIf and
" " & [PostalCode], [City] & " " IsNull functions to
& [Region] & " " & [PostalCode])
display the values of
the City and
PostalCode controls
Expression Description
if the value in the
Region control is
null; otherwise, it
displays the values of
the City, Region, and
PostalCode fields or
controls.
=IIf(IsNull([RequiredDate]) Or Uses the IIf and
IsNull([ShippedDate]), "Check IsNull functions to
for a missing date",
display the message
[RequiredDate] - [ShippedDate])
"Check for a missing
date" if the result of
subtracting
ShippedDate from
RequiredDate is null;
otherwise, it displays
Expression Description
the interval between
the date values of
the RequiredDate
and ShippedDate
controls.
Top of Page
Text operations
The expressions in the following table use the & and + operators to combine text
strings, use built-in functions to operate on a text string, or otherwise operate on
text to create a calculated field.
Expression Description
FullName: [FirstName] & " Creates a field called
" & [LastName] FullName that displays the
values in the FirstName and
LastName fields, separated
Expression Description
by a space.
Address2: [City] & " " & Creates a field called
[Region] & " " & Address2 that displays the
[PostalCode]
values in the City, Region,
and PostalCode fields,
separated by spaces.
ProductInitial: Creates a field called
Left([ProductName], 1) ProductInitial, and then uses
the Left function to display,
in the ProductInitial field,
the first character of the
value in the ProductName
field.
TypeCode: Creates a field called
Right([AssetCode], 2) TypeCode, and then uses
Expression Description
the Right function to display
the last two characters of
the values in the AssetCode
field.
AreaCode: Mid([Phone],2,3) Creates a field called
AreaCode, and then uses
the Mid function to display
the three characters starting
with the second character of
the value in the Phone field.
ExtendedPrice: CCur([Order Names the calculated field
Details].[Unit ExtendedPrice, and uses the
Price]*[Quantity]*(1-
[Discount])/100)*100 CCur function to calculate
the line item totals with an
applied discount.
Top of Page
Arithmetic operations
You can use expressions to add, subtract, multiply, and divide the values in two or
more fields or controls. You can also perform arithmetic operations on dates. For
example, suppose you have a Date/Time field called RequiredDate. The
expression =[RequiredDate] - 2 returns a Date/Time value equal to two days
before the value in the RequiredDate field.
Expression Description
PrimeFreight: [Freight] * 1.1 Creates a field called
PrimeFreight, and then
displays freight charges
plus 10 percent in the
field.
OrderAmount: [Quantity] * Creates a field called
Expression Description
[UnitPrice] OrderAmount, and
then displays the
product of the values in
the Quantity and
UnitPrice fields.
LeadTime: [RequiredDate] - Creates a field called
[ShippedDate] LeadTime, and then
displays the difference
between the values in
the RequiredDate and
ShippedDate fields.
TotalStock: [UnitsInStock]+ Creates a field called
[UnitsOnOrder] TotalStock, and then
displays the sum of the
values in the
Expression Description
UnitsInStock and
UnitsOnOrder fields.
FreightPercentage: Creates a field called
Sum([Freight])/Sum([Subtotal]) FreightPercentage, and
*100
then displays the
percentage of freight
charges in each
subtotal. This
expression uses the
Sum function to total
the values in the
Freight field, and then
divides those totals by
the sum of the values
in the Subtotal field.
Expression Description
using a query.
If you set
the Format property of
the field to Percent, do
not include *100.
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, Display column totals in a datasheet using a Totals row, and Display
column totals in a datasheet.
Top of Page
Date operations
Nearly all databases store and track dates and times. You work with dates and
times in Access by setting the date and time fields in your tables to the Date/Time
data type. Access can perform arithmetic calculations on dates; for example, you
can calculate how many days have elapsed since the invoice date to age your
accounts receivable.
Expression Description
LagTime: DateDiff("d", Creates a field called
[OrderDate], [ShippedDate]) LagTime, and then uses
the DateDiff function to
display the number of
days between the order
date and ship date.
YearHired: DatePart("yyyy", Creates a field called
[HireDate]) YearHired, and then uses
the DatePart function to
display the year each
Expression Description
employee was hired.
MinusThirty: Date( )- 30 Creates a field called
MinusThirty, and then
uses the Date function
to display the date 30
days prior to the current
date.
Top of Page
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
RowCount: Count(*) Creates a field called
RowCount, and then
uses the Count
function to count the
number of records in
the query, including
records with null
Expression Description
(blank) fields.
FreightPercentage: Creates a field called
Sum([Freight])/Sum([Subtotal]) FreightPercentage, and
*100
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.)
Top of Page
Expression Description
CurrentCountryRegion: Creates a field called
IIf(IsNull([CountryRegion]), CurrentCountryRegion,
Expression Description
" ", [CountryRegion]) and then uses the IIf and
IsNull functions to
display an empty string
in that field when the
CountryRegion field
contains a null value;
otherwise, it displays the
contents of the
CountryRegion field.
LeadTime: Creates a field called
IIf(IsNull([RequiredDate] - LeadTime, and then uses
[ShippedDate]), "Check for a
the IIf and IsNull
missing date", [RequiredDate]
- [ShippedDate]) functions to display the
message "Check for a
missing date" if the value
in either the
Expression Description
RequiredDate field or the
ShippedDate field is null;
otherwise, it displays the
date difference.
SixMonthSales: Creates a field called
Nz([Qtr1Sales]) + SixMonthSales, and then
Nz([Qtr2Sales])
displays the total of the
values in the Qtr1Sales
and Qtr2Sales fields by
first using the Nz
function to convert any
null values to zero.
Top of Page
Calculated fields with subqueries
You can use a nested query, also called a subquery, to create a calculated field. The
expression in the following table is one example of a calculated field that results
from a subquery.
Expression Description
Cat: (SELECT [CategoryName] FROM Creates a
[Categories] WHERE [Products]. field called
[CategoryID]=[Categories].[CategoryID])
Cat, and
then
displays the
CategoryN
ame, if the
CategoryID
from the
Categories
Expression Description
table is the
same as the
CategoryID
from the
Products
table.
Top of Page
Top of Page
Top of Page
Top of Page
Top of Page
Top of Page
Update queries
You use an update query to modify the data in one or more existing fields in a
database. For example, you can replace values or delete them entirely. This table
demonstrates some ways to use expressions in update queries. You use these
expressions in the Update To row in the query design grid for the field that you
want to update.
For more information about creating update queries, see the article Create and run
an update query.
Field Expression Result
Title "Salesperson" Changes a text
value to
Salesperson.
ProjectStart #8/10/17# Changes a
date value to
10-Aug-17.
Retired Yes Changes a No
value in a
Yes/No field to
Yes.
PartNumber "PN" & [PartNumber] Adds PN to
the beginning
of each
specified part
number.
Field Expression Result
LineItemTotal [UnitPrice] * Calculates the
[Quantity] product of
UnitPrice and
Quantity.
Freight [Freight] * 1.5 Increases
freight charges
by 50 percent.
Sales DSum("[Quantity] * Where the
[UnitPrice]", "Order ProductID
Details",
values in the
"[ProductID]=" &
[ProductID]) current table
match the
ProductID
values in the
Order Details
Field Expression Result
table, updates
sales totals
based on the
product of
Quantity and
UnitPrice.
ShipPostalCod Right([ShipPostalCode], Truncates the
e 5) leftmost
characters,
leaving the
five rightmost
characters.
UnitPrice Nz([UnitPrice]) Changes a null
(undefined or
unknown)
Field Expression Result
value to a zero
(0) in the
UnitPrice field.
Top of Page
SQL statements
Structured Query Language, or SQL, is the query language that Access uses. Every
query that you create in query Design view can also be expressed by using SQL. To
see the SQL statement for any query, click SQL View on the View menu. The
following table shows sample SQL statements that employ an expression.
Top of Page
Table expressions
The two most common ways to use expressions in tables are to assign a default
value and to create a validation rule.
Top of Page
The examples in the following table demonstrate the validation rule expressions for
the ValidationRule property and the associated text for
the ValidationText property.
Top of Page
Macro expressions
In some cases, you might want to carry out an action or series of actions in a macro
only if a particular condition is true. For example, suppose you want an action to
run only when the value of the Counter text box is 10. You use an expression to
define the condition in the Condition column of the macro:
[Counter]=10
Top of Page