Home

Date and Time Values

Fundamentals of Times

Introduction to Times

A time is a non-spatial measure of the number of seconds that have elapsed in a set period. Starting at a specified period and counting incrementally, if the number of seconds reaches 60, the measure is called a minute. If the number of minutes reaches 60, the measure is called an hour. If 24 measures of hours occur, the measure is called a day (actually a day is 24 hours plus a few more seconds but the seconds are taken into consideration only every 4 years).

Practical Learning: Introducing Time Values

  1. Start Microsoft Access and, to create a new database, click Blank Database
  2. Set the File Name to Georgetown Dry Cleaner4 and click Create
  3. Right-click the Table1 tab and click Design View
  4. Set the name to Cleaning Orders and press Enter
  5. Replace ID with CleaningOrderID
  6. Click the empty box under CleaningOrderID and create the following fields:
     
    Field Name Data Type Field Size Format Default Value
    CleaningOrderID        
    Customer Name   50    
    Customer Phone   20    
    Unit Price Shirts Number Double Currency 1.15
    Quantity Shirts Number Integer   0
    Unit Price Pants Number Double Currency 1.95
    Quantity Pants Number Integer   0
    Other Item1   80   "None"
    Unit Price Item1 Number Double Currency 0.00
    Quantity Item1 Number Integer   0
  7. In the top section of the table, set the Data Type of the Other Item1 field to Lookup Wizard...
  8. In the first page of the wizard, click the second radio button: I will type in the values that I want and click Next
  9. In the second page of the wizard, click the first empty field under Col1 and create the following items
     
    None
    Tie
    Coat
    Dress
    Other
    Jacket
    Swede
    Silk Shirt
    Sweater
    Comforter
    Women Suit
    Regular Skirt
    Men's Suit 2Pc
    Men's Suit 3Pc
    Skirt With Hook
  10. Click Next
  11. Accept the column label as Other Item1 and click Finish
  12. Click the box on the left side of Other Item1
  13. Press and hold Shift
  14. Click the box on the left side of Quantity Item1
  15. Press Ctrl + C to copy
  16. Click the first empty box under Quantity Item1
  17. Press Ctrl + V to paste
  18. Change the field names to Other Item2, Unit Price Other2, and Quantity Other2 respectively
  19. Click the first empty box under Quantity Item2
  20. Press Ctrl + V to paste
  21. Change the new field names to Other Item3, Unit Price Other3, and Quantity Other3 respectively
  22. Click the first empty box under Quantity Item3
  23. Press Ctrl + V to paste
  24. Change the field names to Other Item4, Unit Price Other4, and Quantity Other4 respectively
  25. Click the first empty box under Quantity Item4 and create the following two fields:
     
    Field Name Data Type Field Size Format Default Value
    Tax Rate Number Double Percent 0.0775
    Notes Memo      
  26. Right-click the Notes field and click Insert Rows
  27. Set the name of the new field to Order Status and set its Data Type to Lookup Wizard
  28. In the first page of the wizard, click the second radio button: I will type in the values that I want and click Next
  29. In the second page of the wizard, click the first empty field under Col1 and create the following items
     
    Not Ready
    Ready
    Picked Up
  30. Click Next
  31. Accept the column label and click Finish
  32. In the lower section of the table, set its Field Size to 50 and its Default Value to "Not Ready"
  33. Save the table
     
    Georgetown Cleaning Services - Cleaning Orders
  34. To change the view, on the ribbon, click the arrow of the View button and click Datasheet View

Time Rules

By default, to display or handle time values in a database, Microsoft Access uses some letters and characters. The default rules are defined in the Regional and Language Settings of Control Panel:

Time Formats

To support time values, Microsoft Access uses a data type named Date. Actually, if you are creating a field in a table, to support a time value, you use a data type category called Date/Time. You can specify the type in Datasheet View or in Design View.

To create a time field in the Datasheet View of a table, after specifying the column name or clicking a cell under it, in the Data Type & Formatting section of the Datasheet tab of the ribbon, click the arrow of the Data Type combo box and select Date/Time.

If you want, you can use the Field Templates window:

If you are creating the table in Design View, to configure a field that can hold time values, specify its Data Type as Date/Time:

Practical Learning: Applying the Time Type to a Field

  1. If the Field Templates window is not available, on the ribbon, click Datasheet and, in the Fields & Columns section, click New Field.
    From the Field Templates, under Under Events, drag Start Time and drop it on the table on the right side of Customer Phone
  2. On the form, double-click Start Time to put it into edit mode, type Time Deposited, and press Enter
  3. To switch the view, on the ribbon, click the arrow of the View button and click Design View
  4. In the top section of the table, right-click Unit Price Shirt and click Insert Rows
  5. Type Time Ready as the name of the new column
  6. Set its Data Type to Date/Time

Time Formats

After setting the data type to Date/Time, you can further customize how the time values would appear on the field. Because there are different ways to display a time values, the Format property is used to specify how this value should appear:

As you can see, the Format property provides two columns of information. The left column shows the names of time formats. The right column shows a preview of each option.

Practical Learning: Applying Time Formats

  1. While the Time Ready field is still selected in the top section, in the lower section of the table, click Format and click the arrow of its combo box. Select Short Time
  2. To change the view of the table, in the lower-right section of Microsoft Access, click the Datasheet View button
  3. When asked to save the table, click Yes
  4. On the table, click Time Deposited
  5. On the ribbon, click Datasheet. In the Data Type & Formatting section, click the arrow of the Format combo box and select Short Time

Time Masks

The Format property is used to show how the time values should appear in the control. First, the user must enter the value. To assist the user with how he or she should enter the time, you use the Input Mask. To use it, you can click Input Mask and type a mask. Otherwise, you can click the ellipsis button and follow the wizard.

As stated already, the rules that specify what characters and symbols are used to display time values are in the Time tab of the Customize Regional Options. The characters used for the hours, the minutes, and the seconds are defined in the Time Format combo box:

To control how time values should display in a field, after setting its Data Type to Date/Time, use the Format property. The characters used to create a format are:

Format Used For Used to Display
: Separator The character separator for time values
This character is set in the Regional (and Language) Settings of Control Panel 
h or H Hours An hour number from 0 to 23
If the hour is less than 10, it would display without the leading 0
hh or HH Hours An hour number from 0 to 23
If the hour is less than 10, it would display with the leading 0 such as 08
n or N Minutes A minute number from 0 to 59
If the number is less than 10, it would display without the leading 0
nn or NN Minutes A minute number from 0 to 59
If the number is less than 10, it would display with the leading 0 such as 06
s or S Seconds A second value from 0 to 59
If the number is less than 10, it would display without the leading 0
ss or SS Seconds A second value from 0 to 59
If the number is less than 10, it would display with the leading 0 such as 04
ttttt   The time using the formula of the Long Time of the Regional Settings of Control Panel
am/pm AM and PM am (in lowercase) if the time is configured to display in the standard (non military time) and if the time occurs in the morning, or pm (in lowercase) if the time is configured to display in the standard (non military time) and if the time occurs in the afternoon
AM/PM AM and PM AM (in uppercase) if the time is configured to display in the standard (non military time) and if the time occurs in the morning, or PM (in uppercase) if the time is configured to display in the standard (non military time) and if the time occurs in the afternoon
a/p AM and PM a (in lowercase) if the time is configured to display in the standard (non military time) and if the time occurs in the morning, or p (in lowercase) if the time is configured to display in the standard (non military time) and if the time occurs in the afternoon
A/P AM and PM A (in uppercase) if the time is configured to display in the standard (non military time) and if the time occurs in the morning, or P (in uppercase) if the time is configured to display in the standard (non military time) and if the time occurs in the afternoon
AMPM AM and PM The AM-PM character. Microsoft Access would refer to the format set in the Regional (and Language) Settings of Control Panel

When combining these characters to create a format, you should follow the rules of your language. You should refer to the formula set in the Time property page of the Regional (and Language) Settings of Control Panel. Microsoft Access also refers to it for the character separator. If you want to include any other character, type it in double-quotes.

To create a time value, you use an appropriate combination of the above letters and characters. In most cases, you should include the combination between two # signs. An example would be:

#05:42#

This would represent 5:42 AM. Another example is:

#10:26 AM#

This also represents a time in the morning. Remember that you can also include the seconds in your time value.

Operations on Time Values

Converting to a Time Value

There are two main ways you start with a time value. You can provide it to an object or you can get it from your database. If you are providing it, you can use the rules and combinations we reviewed above. On the other hand, if a time value already exists in your database, you can retrieve and use it as you see fit. In most cases, the users know how to specify a time value. In some cases, when getting a time, it may not be in a correct or recognizable format. Normally, before involving a value in a time-based operation, you should first check and convert it to a recognizable format. To support this, you can called a function called CDate. Its syntax is:

CDate(Expression) As Time

This function takes one argument that can be passed as the name of a control, as a string that holds a time value, or as an expression that is supposed to produce a time value. If the value passed as argument holds a recognizable time, the function returns that time.

The Components of a Time Value

As mentioned in our introduction, a time value is made of the hours, the minutes, and the seconds. If you already have a time or if you retrieve one from an object or you get one from an expression, you may be interested in only one or its components. There are functions you can use to get these values.

To get the hour value of a time, you can call the Hour() function. Its syntax is:

Hour(Expression) As Integer

To get the minute value of a time, you can call the Minute() function. Its syntax is:

Minute(Expression) As Integer

To get the second value of a time, you can call the Second() function. Its syntax is:

Second(Expression) As Integer

Each of these functions takes an argument that can be an expression that should produce a time value. The argument can also be the name of a control that holds a time value. The function then examines the argument and produces a number that represents the hour, the minute, or the second respectively.

The Current Time

Microsoft Access provides various functions to perform date and time related operations. These functions allow you to add times, find the difference between time values, or add constant values to time values.

To give you the current time, Microsoft Access provides a function called Time.

Practical Learning: Getting the Current Time

  1. To change the view of the table, in the lower-right section of Microsoft Access, click the Design View button
  2. In the top section, click Time Deposited.
    In the lower section, click Default Value and type =Time()

Adding to a Time Value

One of the operations you can perform on a time consists of adding a value to it. To support this, you can use the DateAdd() function. To find a backward time, you can use the DateDiff() function.

The DateAdd() function is used to add an interval time value to a specific time. With it, you can add a number of seconds, minutes, or hours to another time. The syntax of the DateAdd() function is

DateAdd(Interval, Number, date) As Time

The Interval argument is required and it specifies the kind of value you want to add. This argument is passed as a string, thus enclosed between double quotes and can have one of the following values:

Interval Used To Get
s Second
n Minute
h Hour
w Numeric Weekday

The Number argument is required also. It specifies the number of units you want to add. If you set it as positive, its value will be added. If you want to subtract, make it negative.

The date argument is the date to which you want to add the number.

Subtraction From a time

The DateDiff() function is used to find the difference between two time values. It allows you to find the number of seconds, minutes, or hours when you supply two recognizable values. The DateDiff() function takes 5 arguments, 3 are required and 2 are optional.

The syntax of the function is

DateDiff(Interval, Time1, Time2, Option1, Option2) As Time

The Interval argument is required and it specifies the kind of value you want to subtract. This argument is passed as a string and can have one of the following values:

Interval Used To Get
s Second
n Minute
h Hour

Required also, the Time1 and Time2 arguments specify the time values that will be used when performing the operation.

Fundamentals of Dates

Introduction to Dates

A date is a non-spatial measure of the number of days that have occurred in a period. When a certain number of days, namely 28, 29, 30, or 31 (depending on some factors we will not discuss in our lessons), the measure is called a month. When 12 months have occurred, the measure is called a year (after 10 years have occurred, the measure is called a decade but the decade is not used in calculations; after 100 years have occurred, the measure is called a century but the century is not used in calculations; after 1000 years have occurred, the measure is called a millennium but the millennium is not used in calculations).

Introduction to Date Rules

By default, to display date values in a database, Microsoft Access uses some letters and characters. As mentioned for times, the rules for dates are built in the operating system and you can check them in the Regional and Language Settings of Control Panel:

As reviewed for the time, to support date values, Microsoft Access internally uses a data type named Date. In a table, this data type is referred to as Date/Time. Over all, dates and times are considered differently but, to specify that a field would use date, time, or both, set its Data Type to Date/Time. You can specify this data type for a field what creating a table either in the Datasheet View or in the Design View.

To apply the date type in the Datasheet View of a table:

If creating a table in Design View, to configure a field that can hold any type of string, specify its Data Type as Date/Time.

Practical Learning: Applying a Date Type

  1. In the top section of the table, right-click Deposit Time and click Insert Rows
  2. Type Date Deposited as the name of the new field
  3. Set its Data Type to Date/Time
  4. To change the view, right-click the Cleaning Orders tab and click Datasheet View
  5. When asked to save the table, click Yes
  6. On the Field Templates, under Projects, drag Begin Date and drop it on the table on the left side of Retrieve Time
  7. On the table, double-click Begin Date and type Date Ready to replace it

Dates Formats

After a valid date value has been entered in a field, you can specify how the date would display, not necessarily how the date was entered. The Format combo box provides already created formats you can use:

Those are the most popular formats used for dates. If none of those satisfies you, you can use the following characters or symbols to create a desired format:

Format Used For Used to Display
d Days The day as a number from 1 to 31
dd Days The day as a number with a leading 0 if the number is less than 10
ddd Weekdays The name of a weekday with 3 letters such as Mon, Tue, etc
dddd Weekdays The complete name of a week day such as Monday, etc
w Week The numeric day of the week such as 1
ww Week The numeric week of the year, ranging from 1 to 53
m Months The numeric month from 1 to 12
mm Months The numeric month with a leading 0 if the number is less than 10
mmm Months The short name of the month such as Jan, Feb, Mar, etc
mmmm Months The complete name of the month such as January, February, etc
q Quarters The numeric quarter of the year
yy Years Two digits for the year as 00 for 2000 or 03 for 2003
yyyy Years The numeric year with 4 digits

To use these letters, simply type the desired combination in the Format field. Here is an example:

This is configured to display a single digit for a day of the month if the day is less than 10, followed by the complete name of the month, followed by the year in 4 digits.

During data entry, the data entry person can enter a valid date. Once the field looses focus, it displays the date based on the format. Here is an example:

Based on this, you can use any combination of formats but you should use a combination most regularly used in your language so the users would not be confused.

Besides the indicated characters, you may want to use some other characters to separate them. An example would be January 5, 2004, which uses a comma in the display. To create such sections, include the characters in double-quotes. What you would be doing is to ask Microsoft Access to display such characters “as is” while considering the non-quotes characters as part of the format. Here is an example:

Practical Learning: Applying a Date Format

  1. On the table, click Date Deposited
  2. On the ribbon, click Datasheet.
    In the Data Type & Formatting section, click the arrow of the Format combo box and select Long Date
  3. To change the view, right-click the Cleaning Orders tab and click Design View
  4. In the top section, right-click Notes and click Insert Rows
  5. Set its name to Date Picked Up and set its Data Type to Date/Time
  6. In the lower section, click the arrow of the Format combo box and select Long Date

Dates Masks

The Format property allows you to specify how a date would display in a field but not how the user must enter it. To specify how the date must be typed in a field or control, you can use the Input Mask property. You can either type the mask or you can click the ellipsis button and follow the wizard. If you want to type your mask, you can use an appropriate combination of the letters and symbols we saw above.

Practical Learning: Using Dates

  1. Open the Red Oak High School database you created in Lesson 5 and continued in Lesson 17
  2. In the Navigation Pane, under Students: Table, right-click the Students form and click Design View
  3. Double-click the DOB text box to access its Properties window.
    In the Properties window of the DOB field, change the Format value to Long Date
  4. Save the form
  5. Preview the result
  6. Save and close the Students form

Operations on Date Values

The Components of a Date

As mentioned for the time, before involving a value or an expression in a date-based operation, you should first convert it. To support this operation, you can call the CDate() function.

We saw that a date was made of the days, the months, and the years components. On an existing date, you can get these values by calling their corresponding functions. To get the year part of a date, you can call the Year() function whose syntax is:

Year(Expression) As Integer

This function takes an argument that represents a date value. It then isolates the year that it returns as a number.

To get the month of a date, you can call the Month() function whose syntax is:

Month(Expression) As Integer

The function considers a date value as argument, gets its year, and returns it.

The Week in a Date

The week is a combination of 7 days that consecutively occur in a date. To identify them, each day has a specific index and a specific name. In US English, the names of the days are Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. In some cases, the days may be counted from Sunday (instead of Monday). The option to specify what is considered as the first day of the week is referred to as firstdayofweek and is represented in Microsoft Access by one of the following values:

Value Name
0 National Language Support (NLS) Setting
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday

In most operations that involve date values, you will consider that the first day of the week is Sunday, represented with 1. In some cases, if you want to use another day as the starting point of a week, you can specify it from one of the above values.

To get the name of a week, you can use the WeekdayName() function whose syntax is:

WeekdayName(weekday As Integer,
	    Option As Boolean,
	    Optional firstdayofweek As Integer) As String

This function takes three arguments with only the first required. The first argument is passed as an integer and can be one of the above. The second argument is optional and it specifies whether the weekday name will be abbreviated. The third argument, firstdayofweek, also optional, specifies what day should be considered as the first day of the week. It can be one of the above numbers.

To get the index position of a day of a week from a date value, you can call the Weekday() function. Its syntax is:

Weekday(Expression, Optional firstdayofweek As Integer) as Integer

The first argument is required. It can be an expression or a control that must produce a date. If the function succeeds, it produces a number that represents the day of the week.

The Current Date

As opposed to the time, if you want to know the current date on a computer, you can call a function named Date. To get both the current date and the current time of the computer, you can call a function named Now.

Practical Learning: Getting the Current Date

  1. Re-open the Georgetown Dry Cleaner4 database and open the Cleaning Orders table in Design View
  2. In the top section of the table, click Date Deposited
  3. In the lower section, click Default Value and type =Date()
     
  4. Save and close the table

Adding to a Date

Some of the operations performed on date values are also performed using either the DateAdd() or the DateDiff() functions.

The DateAdd() function is used to add an interval date value to a specified date. It is used to add a number of days, weeks, months, or years to another date. The syntax of the DateAdd() function is

DateAdd(Interval, Number, date) As Date

The Interval argument is required and it specifies the kind of value needed as a result. This argument can be passed as a string in double-quotes and can have one of the following values:

Interval Used To Get
w Numeric Weekday
ww Week of the Year
d Day
y Numeric Day of the Year
m Month
q Quarter
yyyy Year

The Number argument is required also. It specifies the number of units you want to add. If you set it as positive, its value will be added. If you want to subtract, pass it as a negative value.

The date argument is the date to which you want to add the number.

Subtraction From a Date

The DateDiff() function is used to find the difference between two date values. It allows you to find the number of days, weeks, months, or years from two date values. The DateDiff() function takes 5 arguments, 3 are required and 2 are optional. Its syntax is:

DateDiff(Interval, Date1, Date2, Option1, Option2)

The Interval argument is required and it specifies the kind of value you want as a result. This argument is passed as a string and can have one of the following values:

Interval Used To Get
w Numeric Weekday
ww Week of the Year
d Day
y Numeric Day of the Year
m Month
q Quarter
yyyy Year

The Date1 and Date2 arguments are required. They specify the date values for the operation.

By default, the days of a week are counted starting on Sunday. If you want to start counting from another day, pass the Option1 argument using one of the following values: 1, 2, 3, 4, 5, 6, 7. There are other variances to that argument.

If your calculation involves weeks or finding the number of weeks, by default, the weeks are counted starting January 1st. If you want to count your weeks starting at a different date, use the Option2 argument to specify where the program should start.

Practical Learning: Subtracting From a Date

  1. Re-open the Red Oak High School database
  2. In the Navigation Pane, under Students: Table, right-click the Students form and click Design View
  3. In the Control section of the Ribbon, click Text Box and click the middle of the Form Footer section
  4. Change the accompanying caption to Age: and double-click the text box itself
  5. In the Properties window, change the following characteristics:
    Name: txtAge
    Control Source: =DateDiff("yyyy",[DOB],Date())
  6. Save the Students form and preview it
     
    Red Oak High School
  7. Close the form

Lesson Summary

MCAS: Using Microsoft Office Access 2007 Topics

C4 Create fields and modify field properties

Exercises

Yugo National Bank

  1. Open the Yugo National Bank1 database
  2. Open the Employees form and set the Can Create New Account field as follows for the following employees:

    Last Name Can Create New Account?
    Yuen Checked
    Marconi Checked
    Young Checked
    Holland Checked
    Kast Checked
    Lourde Checked
    Kirkland Checked
  3. Close the form

  4. Open the ChargeReasons Table in Design View
  5. Add one more field named Description and set its Data Type to Memo
  6. Change the captions of the first two fields as following:
     
    Field Name Field Size Caption
    ChargeReasonID   Charge Reason ID
    ChargeReason 50 Charge Reason
    Description    

    Save and close the table

  7. Create a form based on the ChargeReasons table and save it as ChargeReasons. Design it to appear like the other already created forms:
     
    Charge Reasons
  8. Switch the ChargeReasons form to Form View and create the following records
     
    Charge Reason ID Charge Reason Description
    1 Monthly Charge Applied every month to each accounts
    2 Overdraft Applied if a customer's account remains negative for 72 hours

    Close the form

Watts A loan

  1. Open the Watts A Loan1 database
  2. Open the Transactions table in Design View and change it as follows:
     
    Field Name Data Type Field Size Caption
    TransactionID AutoNumber   Transaction ID
    TransactionNumber Number Integer Transaction Number
    TransactionDate Date/Time   Transaction Date
    TransactionAmount Number Double, Fixed Transaction Amount
    Notes Memo    
  3. Save and close the table
  4. Open the Customers table in Design View and change it as follows:
     
    Field Name Data Type Field Size Caption
    CustomerID AutoNumber   Customer ID
    AccountDate Date/Time   Account Date
    AccountNumber Text 20 Account Number
    FirstName Text 50 First Name
    MiddleName Text 50 Middle Name
    LastName Text 50 Last Name
    Address Text 100  
    City Text 50  
    State Text 50  
    ZIPCode Text 20 ZIP Code
    Country Text 50  
    HomePhone Text 40 Home Phone
    WorkPhone Text 40 Work Phone
    EmailAddress Text 60 Email Address
    Notes Memo    
  5. Save and close the table
  6. Create a form for the Customers table. Save the form as Customers and design it as you see fit. Here is an example:
     
    Customers
  7. Close the form
  8. Open the Customers form and create its records

World Statistics

  1. Open the World Statistics1 database
  2. Open the Countries table in Design View and complete it as follows:
     
    Field Name Data Type Field Size Caption
    CountryID      
    CommonName      
    InternetCode      
    LongName      
    GovernmentType      
    Area Number    
    Population Number    
    Nationality Text 50  
    Capital Text 50  
    Independence Date/Time    
    NationalHoliday Date/Time   National Holiday
    Map OLE Object    
    Flag OLE Object    
    Pictures Attachment    
    WebSite      
    Description Memo    
  3. Save and close the table
  4. Create a form for the Countries table and save it as Countries
  5. Design it as you see fit. Here is an example:
     
    Countries
     
    Countries
  6. Using (legitimate and official) information you can find in books or web sites, perform data entry using the Countries form. You can use, but do not have to limit yourself to, the CIA World Fact Book web site. Any information you can find somewhere else such as the presidency web site of a country, or the official web sites of a country, is fine

US Senate

  1. Open the US Senate1 database
  2. Go to http://www.senate.gov
  3. Get the information about each senator and populate the Senators form with that information

Previous Copyright © 2008-2019, FunctionX, Inc. Next