Working with Date Variables (Unit 5)
description
Transcript of Working with Date Variables (Unit 5)
![Page 1: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/1.jpg)
Working with Date Variables(Unit 5)
Visual Basic for Applications
![Page 2: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/2.jpg)
Objectives In this unit, you will learn how to:
Reserve a Date variable
Use an assignment statement to assign a value to a Date variable
Assign the VBA Date, Time, and Now functions to a Date variable
Control the appearance of dates and times using the Format function
Perform calculations using Date variables
![Page 3: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/3.jpg)
Objectives
In this unit, you will learn how to:
Convert a string to a Date data type using the DateValue and TimeValue functions
Refer to the active cell in Excel
Use the Range object’s Offset property in Excel
Preview and print a document in Word
Refer to a control on an Access form
Create a custom toolbar in Access
![Page 4: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/4.jpg)
Concept Lesson:Discussing Date Variables
A Date variable is a variable that can store date and time
information
![Page 5: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/5.jpg)
Reserving a Procedure-level Date Variable When creating a Date variable, datatype is always
the keyword Date
Date variables are automatically initialized to the number 0
The dtm ID indicates that the variable is aDate variable, which can store date andtime information
After using the Dim statement to both reserve and initialize a Date variable, you can use an assignment statement to assign a different value to the variable
![Page 6: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/6.jpg)
Examples of Dim Statements that Reserve Date Variables
Exhibit 5-1: Some examples of Dim statements that reserve Date variables
![Page 7: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/7.jpg)
Using an Assignment Statement
Date literal constant examples: #January 7, 2003#
#12/31/2002#
#11:05:00 AM#
#7:30:07 PM#)
Exhibit 5-2: Some examples of assignment statements
![Page 8: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/8.jpg)
Using VBA’s Date, Time,and Now Functions
VBA’s Date function returns the system date, which is the date maintained by your computer’s internal clock
VBA’s Time function returns the system time, which is the time maintained by your computer’s internal clock
VBA’s Now function returns both the system date and time
![Page 9: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/9.jpg)
AssignDisplayDate Procedure
Exhibit 5-3: The AssignDisplayDate procedure
![Page 10: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/10.jpg)
Message Box Displayed by the AssignDisplayDate Procedure
Exhibit 5-4: The message box displayed by the AssignDisplayDate procedure
![Page 11: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/11.jpg)
Using the Format Function
You can use the VBA Format function to control the appearance of dates and times
The syntax of the Format function:
Format(Expression:=expression, Format:=format)
• expression specifies the number, date, time, or string whose appearance you want to format
• format is the name of a predefined VBA format
![Page 12: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/12.jpg)
Help Screen Showing the VBA Predefined Date/Time Formats
Search for “Format Function” in VBE Help box
![Page 13: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/13.jpg)
Examples of Using Format Function(Try the following examples in the Immediate Window)
dtmTime = #17:04:23# dtmDate = #January 27, 1993#
Print Format(Time, "Long Time") Print Format(dtmTime, "Medium Time") Print Format(#8:30:00 PM#, "Short Time")
Print Format(Date, "Long Date") Print Format(dtmDate, "Medium Date") Print Format(#March 21 2000#, "Short Date")
Print Format(dtmTime, "h:m:s") Print Format(dtmTime, "hh:mm:ss AMPM") Print Format(dtmDate, "dddd, mmm d yyyy")
Print Format(1,"General Date")
![Page 14: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/14.jpg)
Using Dates andTimes in Calculations
VBA provides two functions called DateAdd and DateDiff that you can use to perform calculations involving dates and times
The DateAdd function allows you to add a specified time interval to a date or time, and it returns the new date or time
![Page 15: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/15.jpg)
Using Dates andTimes in Calculations
The DateDiff function allows you to determine the time interval that occurs between two dates
Unlike the DateAdd function, which returns either a future or past date or time, the DateDiff function returns an integer that represents the number of time intervals between two specified dates or times
![Page 16: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/16.jpg)
DateAdd and DateDiff Syntax DateAdd Function
DateAdd(interval, number, date)
DateDiff Function DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
![Page 17: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/17.jpg)
Valid Settings forthe Interval Argument
The valid settings for the interval argument
![Page 18: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/18.jpg)
Examples of theDateAdd Function
Exhibit 5-8: Some examples of the DateAdd function
![Page 19: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/19.jpg)
Examples of theDateDiff Function
Exhibit 5-9: Some examples of the DateDiff function
![Page 20: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/20.jpg)
Converting Strings to Date or Time
VBA DateValue function or the TimeValue function converts the string to a date or time, respectively
The syntax of the DateValue function is: DateValue(Date:=stringExpression) stringExpression represents a valid date ranging from January 1,
100 through December 31, 9999
The syntax of the TimeValue function is: TimeValue(Time:=stringExpression)
stringExpression represents a valid time ranging from 0:00:00 (12:00:00 AM) through 23:59:59 (11:59:59 PM)
![Page 21: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/21.jpg)
Examples of Using the DateValue and TimeValue Functions
Exhibit 5-10: Using DateValue and TimeValue functions
![Page 22: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/22.jpg)
Summary Use Date, Time, and Now functions to return system date and
time
Use Format function to control the appearance of date and time
Use DateAdd Function to add a specified time interval to a date or time, and then return the new date or time
Use DateDiff Function to calculate the number of time intervals between two specified dates or times
Use the DateValue function to convert a date string to a Date data type
Use the TimeValue function to convert a time string to a Time data type
![Page 23: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/23.jpg)
Excel Lesson
Open Excel and select Cell A1
Open Object Browser in VBE
Search for ActiveCell object
What does ActiveCell object represent?
Open immediate window and type: Application.ActiveCell.value = 108
Application.workbooks(1).Worksheets(1).range(“C5”).select
Print ActiveCell.address
Why do think active cell is a member of application object?
![Page 24: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/24.jpg)
The Offset Property You can use a Range object’s Offset property to
refer to a cell located a certain number of rows or columns away from the range itself
The syntax of the Offset property is: rangeObject.Offset([rowOffset] [,columnOffset])
rowoffset and columnOffset could be positive or negative
Positive rowOffset refers to rows found below the rangeObject
Positive columnOffset refers to columns to the right of the rangeObject
![Page 25: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/25.jpg)
Illustration of the Offset Property
Exhibit 5-12: An illustration of the Offset property
Select Cell B5 in ExcelTry the following examples in the Immediate window:
• activecell.Offset(-1,-1).Value = "upper left“• activecell.Offset(-1,1).Value = "upper right"• activecell.Offset(1,-1).Value = "lower left“• activecell.Offset(1,1).Value = "lower right”
![Page 26: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/26.jpg)
Excel Lesson:Creating the CalcHours
Macro Procedure
Open Martin’s workbook
View the code template for the CalcHours procedure
![Page 27: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/27.jpg)
Pseudocode for theCalcHours Procedure
Exhibit 5-11: The pseudocode for the CalcHours procedure
![Page 28: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/28.jpg)
Word lessonPrinting a Document
You can use the Document object’s PrintPreview method to preview a document on the screen before printing it
You can use the Document object’s PrintOut method to print the document on the printer
The syntax of the PrintPreview method is:
documentObject.PrintPreview
The syntax of the PrintOut method is:
documentObject.PrintOut, which prints one copy of the entire documentObject
![Page 29: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/29.jpg)
Word Lesson:Using date variables in Word
Open Pat’s document and view the code template for the
PrintInvitation procedure
![Page 30: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/30.jpg)
Pseudocode for the PrintInvitation Procedure
Exhibit 5-13: The pseudocode for the PrintInvitation procedure
![Page 31: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/31.jpg)
Access Lesson:Using date variables in Access
Begin by opening the database and viewing the ProjectsForm form
Then open the Visual Basic Editor and view the code template for the AssignDates procedure
![Page 32: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/32.jpg)
Referring to a Control on a Form
Each of the text boxes on a form is considered a Control object in VBA
Each control belongs to the Form object’s Controls collection
You can use the formObject.Controls(controlName) syntax to refer to a control on a form
controlName should be enclosed in quotation marks
![Page 33: Working with Date Variables (Unit 5)](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813ff4550346895dab14f7/html5/thumbnails/33.jpg)
Pseudocode for theAssignDates Procedure
Exhibit 5-14: The pseudocode for the AssignDates proedure