Examples of expressions

advertisement
Access overview
Files on desktops and in:
\\Itep68.itep.nau.edu\itep\AIAQTP\ AIAQTP Course Folders\GIS for Air Quality\Access Overview
Basic concepts of database:
 normalization, where every value is the same so values are the same every time so can be searched and tables can be related
(relational database)
 minimizes space, repetition and errors
 many small tables that are related so info can be combined efficiently
Different names of things onscreen:
 Title bar
 Viewing objects panel: F11
o Object
o Relationships window
The Database window
Database Toolbar
The Database toolbar presents operations that can be performed against different database objects, including buttons to open
an object, design an object, create a new object and delete an object. On the Left Pane appears the list of objects in the database:
Database objects are created and opened by choosing any of the buttons listed in the left pane of the database
window.
Page | 1
Right Pane: Any of the first four options -- Tables, Queries, Forms, or Reports -- opens additional choices related to that
selection in the right pane.
Choosing the Tables button, for example, displays at least three options in the right pane: 1) Create a table in Design view, 2)
Create a table by using wizard, and 3) Create table by entering data. It also shows any objects that you created in the
database.
Structure of a table:
 Each row is a record
 Each column is a field.
 Properties of each field, including the input mask and what to do if you input the phone # the wrong way, and how that
preserves the values
 PRIMARY KEYS relate tables to each other. Date is used as a primary key because it is unique to each field, if each row
represents one 24-hour value (such as for PM filter data) and there is only one row for each date.
Views:
 Objects can be viewed in database
 or design view, and can toggle back and forth
Forms:
Page | 2

Experiment with using the form wizard to enter data in the tbl_Notes table:
Page | 3
Queries:
 Easiest query is the select query, which just shows rows meeting the criteria specified
Page | 4

Update queries add or change data already in a table
Page | 5

Append queries add rows to a table

Make table queries make a table:
Page | 6
Reports:
Pulls info from queries or tables and can be simple—use the Report Wizard
Things to Remember:
 Closing a database: Access saves the data as soon as you select a field in a new row (in a table) or new page in a form and
does not depend on the “save” button, as does Excel.
 Always click on the small x in the upper right hand corner of the form or table you are in; the large X closes the whole
database. However, if you accidentally close the database, the data you were working on will be saved in most cases.
 Tracking database copies: cannot save from within a database but must copy a database with it closed.
Page | 7
Access: Examples of expressions
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. These examples are used in the “query design grid.” Example queries are in the database
“Example Data for GIS course.mdb”
Examples using DATE values
Expression
Today: Date()
ThisWeek: Format(Now(), "ww")
Description
Creates a new field called “Today” which shows the current date in that
field (column) 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).
Creates a new field called that displays the current week number, where
Page | 8
ww is 1 through 53.
The year: DatePart("yyyy", [Date])
TenDaysBefore:DateAdd("d", -10, [StartDate])
Difference: DateDiff("d", [StartDate], [EndDate])
MonthNo: DatePart("M", [OrderDate])
PriorDate: Date( ) - 30
Creates a new field called The year that uses the DatePart function to
display the four-digit year of the value of the Date field. Note that you
could use “m” if you just want the month, “d” for day, and “q” for quarter.
Creates a new field called TenDaysBefore that displays a date that is 10
days before StartDate
Creates a new field called Difference that displays the difference in days
between the values of the StartDate and EndDate fields. Use “yyyy” for
years, “m” for months, “q” for quarter
Displays in the MonthNo field the number of the month
Displays in the PriorDate field the date 30 days prior to the current date
Between Date( ) And DateAdd("M", 3, Date( ))
< Date( ) – 30
Year([OrderDate]) = 1999
DatePart("q", [OrderDate]) = 4
DateSerial(Year ([OrderDate]), Month([OrderDate]) + 1, 1) - 1
Year([OrderDate]) = Year(Now()) And Month([OrderDate]) =
Month(Now())
between today's date and three months from today's
date
more than 30 days old
with order dates in 1999
for the fourth calendar quarter
for the last day of each month
for the current year and month
Examples using TEXT values
Expression
Whatever:"N/A"
FullName: [FirstName]&"
"&[LastName]
Description
Creates a new field called Whatever that displays N/A.
Creates a new field called FullName that displays the values of the FirstName and LastName fields
separated by a space.
Page | 9
Creates a new field called Initial that uses the Left function to display the first character of the
value of the ProductName field.
EndTwoDigits:
Creates a new field called EndTwoDigits that uses the Right function to display the last 2
Right([AssetCode], 2)
characters of the value of the AssetCode field.
Creates a new field called NoExtraSpaces that uses the Trim function to display the value of the
NoExtraSpaces: Trim([Address])
Address field, removing any leading or trailing spaces.
Initial: Left([ProductName], 1)
Examples using arithmetic operations
Expression
[Conc1] + [Value]
[RequiredDate] [ShippedDate]
[Price] * 1.06
[Quantity] * [Price]
[EmployeeTotal] /
[CountryTotal]
Description
Shows the sum of the values of the Conc1 and Value fields.
Shows the difference between the values of the RequiredDate and ShippedDate fields.
Shows the product of the value of the Price field and 1.06 (adds 6 percent to the Price value).
Shows the product of the values of the Quantity and Price fields.
Shows the quotient of the values of the EmployeeTotal and CountryTotal fields.
NewLabel: IIf(([pm25]<15),"low","too high")
Creates a new field called NewLabel that shows in each row of this column as the word “low” if the
value in the pm-25 field is less than 15, and “too high” if not (no quotation marks, those are just to tell
Access that it is a text value). Note that depending on your version of Access, the If may be IIf
(Access 2007 is IIf)
TotalStock: [UnitsInStock] +
[UnitsOnOrder]
Displays in the TotalStock field the sum of the values in the UnitsInStock and UnitsOnOrder fields
Page | 10
ValuePercentage:
Displays in the ValuePercentage field the percentage of value charges in each conc1, by dividing the
Sum([Value]) / Sum([Conc1]) sum of the values in the Value field by the sum of the values in the Conc1 field.
* 100
The Total row in the design grid must be displayed, and the Total cell for this field must be set to
Expression.
If the Format property of the field is set to Percent, don't include the *100.
Displays in the SumofUnits field the sum of all units in stock and on order.
SumofUnits:
When you use an arithmetic operator (+, -, *, /) in an expression and the value of one of the fields in
Nz([UnitsInStock], 0) +
the expression is Null, the result of the entire expression will be Null. If some records in one of the
Nz([UnitsOnOrder], 0)
fields you used in the expression might have a Null value, you can convert the Null value to zero
using the Nz function.
Ranges of values (>, <, >=, <=, <>, or Between...And)
Expression
Result
> 234
For a Quantity field, numbers greater than 234
>= "Callahan"
all names from Callahan through the end of the alphabet
Between #2/2/1999# And #12/1/1999# dates from 2-Feb-99 through 1-Dec-99 (ANSI-89)
Between '2/2/1999' And '12/1/1999'
dates from 2-Feb-99 through 1-Dec-99 (ANSI-92)
Values that don't match (Not)
Expression
Result
Not "USA" Shows all values other than “USA”
Not starting with the letter "T" (ANSINot T*
89)
Not starting with the letter "T" (ANSINot T%
92)
Page | 11
Work with Null values
Expression
CurrentCountry: IIf(IsNull([Country]), " ", [Country])
LeadTime: IIf(IsNull([RequiredDate] - [ShippedDate]),
"Check for a missing date", [RequiredDate] [ShippedDate])
Result
Displays in the CurrentCountry field an empty string if the Country field
is Null; otherwise, it displays the contents of the Country field
Displays in the LeadTime field the message "Check for a missing date" if
the value of either the RequiredDate or ShippedDate fields is Null;
otherwise, it displays the difference
UPDATE QUERIES add data to a table:
Examples of expressions used in update queries
Page | 12
See the query “qry_Update_example” in the example database. Note that each time you run an update query it tacks onto previous
text values (see the field with APAPAPAPAP values).
Expression
"analyzer problem"
#8/10/99#
Yes
"AP" & [Null Data Code]
[UnitPrice] * [Quantity]
[Value] * 1.5
DSum("[Quantity] * [UnitPrice]",
"Order Details", "[ProductID] " &
[ProductID])
Right([ShipPostalCode], 5)
IIf(IsNull([UnitPrice]), 0,
[UnitPrice])
Result
Changes a text value to analyzer problem
Changes a date value to 10-Aug-99
Changes a No value in a Yes/No field to Yes
Adds AP to the beginning of each Null Data Code
Calculates the product of UnitPrice and Quantity
Increases value charges by 50 percent
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
Truncates the leftmost characters, leaving the five rightmost characters
Changes a Null value to a zero (0) in the UnitPrice field
Examples of conditional expressions
Expression
Description
IIf([Confirmed] = "Yes", "Order Confirmed", "Order Displays the message "Order Confirmed" if the value of the Confirmed field is
Not Confirmed")
Yes; otherwise, it displays the message "Order Not Confirmed."
Displays an empty string if the value of the Country field is Null; otherwise, it
IIf(IsNull([Country])," ", [Country])
displays the value of the Country field.
IIf(IsNull([Region]),[City]&" "& [PostalCode],
Display the values of the City and PostalCode fields if Region is Null;
[City]&" "&[Region]&" " &[PostalCode])
otherwise, it displays the values of the City, Region, and PostalCode fields.
Display the message "Check for a missing date" if the result of subtracting
IIf(IsNull([RequiredDate] - [ShippedDate]), "Check
ShippedDate from RequiredDate is Null; otherwise, it displays the difference
for a missing date", [RequiredDate] - [ShippedDate])
between the values of the RequiredDate and ShippedDate fields.
Page | 13
Examples of expressions that use aggregate functions—to show this row in the query design pane, click on the SUMMA symbol in
the toolbar:
which pulls up a new row in the query design grid labeled “Total” with a value of “Group By” as the
default value. Click in this row and change the values in the drop down box in the Total row.
Page | 14
Download