Presentation is loading. Please wait.

Presentation is loading. Please wait.

Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.

Similar presentations


Presentation on theme: "Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement."— Presentation transcript:

1 Date Variables Visual Basic for Applications 5

2 Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement to assign a value to a Date variable n Assign the VBA Date, Time, and Now functions to a Date variable n Control the appearance of dates and times using the Format function n Perform calculations using Date variables 5

3 Objectives n In this tutorial, you will learn how to: n Convert a string to a Date data type using the DateValue and TimeValue functions n Refer to the active cell in Excel n Use the Range object’s Offset property in Excel n Preview and print a document in Word n Refer to a control on an Access form n Create a custom toolbar in Access 5

4 Concept Lesson: Date Variables n You will learn how to reserve a procedure- level Date variable, which is a variable that can store date and time information n You can use the Static statement to create a procedure-level variable that retains its value when the procedure in which it is declared ends 5

5 Reserving a Procedure-level Date Variable n When creating a Date variable, datatype is always the keyword Date n Date variables are automatically initialized to the number 0 n The dtm ID indicates that the variable is a Date variable, which can store date and time information n 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 5

6 Examples of Dim Statements that Reserve Date Variables 5

7 Using an Assignment Statement to Assign a Value to a Date Variable n A date literal constant can be a date (such as #January 7, 2003# and #12/31/2002#), or it can be a time (such as #11:05:00 AM# and #7:30:07 PM#) n Date literal constants also can include both a date and a time 5

8 Using an Assignment Statement to Assign a Value to a Date Variable n In addition to assigning date literal constants to Date variables, you also can assign the value returned by VBA’s Date, Time, and Now functions 5

9 Using VBA’s Date, Time, and Now Functions 5 n VBA’s Date function returns the system date, which is the date maintained by your computer’s internal clock n VBA’s Time function returns the system time, which is the time maintained by your computer’s internal clock n VBA’s Now function returns both the system date and time n The AssignDisplayDate procedure first reserves three Date variables named dtmDurDate, dtmCurTime, and dtmCurDateTime

10 AssignDisplayDate Procedure 5

11 Message Box Displayed by the AssignDisplayDate Procedure 5

12 Using the Format Function n You can use the VBA Format function to control the appearance of dates and times n The syntax of the Format function is Format(Expression:=expression, Format:=format) n In the syntax, expression specifies the number, date, time, or string whose appearance you want to format, and format is the name of a predefined VBA format 5

13 Help Screen Showing the VBA Predefined Date/Time Formats 5

14 Examples of Using the Format Function to Control the Display of Dates and Times 5

15 Using Dates and Times in Calculations n At times, you may need to include dates and times in the calculations performed by a procedure n VBA provides two functions called DateAdd and DateDiff that you can use to perform calculations involving dates and times n The DateAdd function allows you to add a specified time interval to a date or time, and it returns the new date or time 5

16 Valid Settings for the Interval Argument 5

17 Examples of the DateAdd Function 5

18 Using Dates and Times in Calculations n The DateDiff function allows you to determine the time interval that occurs between two dates n 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 5

19 Examples of the DateDiff Function 5

20 Converting Strings to Dates n Before using a string that represents a date or time in a calculation, you should use either the VBA DateValue function or the TimeValue function to convert the string to a date or time, respectively n The syntax of the DateValue function is DateValue(Date:=stringExpression), where stringExpression represents a valid date ranging from January 1, 100 through December 31, 9999 n The DateValue function returns the date equivalent of the stringExpression argument 5

21 Converting Strings to Dates n The syntax of the TimeValue function is TimeValue(Time:=stringExpression), where stringExpression represents a valid time ranging from 0:00:00 (12:00:00 AM) through 23:59:59 (11:59:59 PM) n The TimeValue function returns the time equivalent of the stringExpression argument 5

22 Examples of Using the DateValue and TimeValue Functions to Convert Strings to Dates and Times 5

23 Summary To reserve a procedure-level Date variable: n Use the Dim statement. The syntax of the Dim statement is Dim variablename As datatype n When reserving a Date variable, datatype is always the keyword Date n Variable names must begin with a letter and they can contain only letters, numbers, and the underscore (_) To assign a value to a variable: n Use an assignment statement in the following syntax: variablename = value 5

24 Summary To access the current system date and time: n Use the VBA Date, Time, and Now functions To control the appearance of dates and times: n Use the VBA function, the syntax of which is Format(Expression:=expression, Format:=format) To add a specified time interval to a date or time, and then return the new date or time: n Use the VBA DateAdd function 5

25 Summary To calculate the number of time intervals between two specified dates or times: n Use the VBA DateDiff function To convert a string to a Date data type: n Use the DateValue function to return the date equivalent of a string n Use the TimeValue function to return the time equivalent of a string 5

26 Excel Lesson: Creating the CalcHours Macro Procedure n To open Martin’s workbook and view the code template for the CalcHours procedure, use the steps on pages 287 and 288 of the textbook 5

27 Pseudocode for the CalcHours Procedure 5

28 Creating the CalcHours Macro Procedure n To begin coding the CalcHours procedure, use the steps on pages 290 and 291 of the textbook 5

29 Partially Completed CalcHours Procedure 5

30 The Offset Property n 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 n The syntax of the Offset property is rangeObject.Offset([rowOffset] [,columnOffset]) n You use a positive rowOffset to refer to rows found below the rangeObject, and you use a negative rowOffset to refer to rows above the rangeObject n To use the Offset property to complete the CalcHours procedure, use the steps on pages 292 to 294 of the textbook 5

31 Illustration of the Offset Property 5

32 Completed CalcHours Macro Procedure 5

33 The Offset Property n To test the CalcHours procedure, use the steps on pages 294 and 295 of the textbook 5

34 Word Lesson: Coding the PrintInvitation Procedure n Begin by opening the document and viewing the code template for the PrintInvitation procedure, which has already been inserted into a module n To open Pat’s document and view the code template for the PrintInvitation procedure, use the steps on pages 301 and 302 of the textbook 5

35 Invitation Document 5

36 Pseudocode for the PrintInvitation Procedure 5

37 Coding the PrintInvitation Procedure n To begin coding the PrintInvitation procedure, use the steps on pages 303 to 305 of the textbook 5

38 Printing a Document n You can use the Document object’s PrintPreview method to preview a document on the screen before printing it, and you can use the Document object’s PrintOut method to print the document on the printer n The syntax of the PrintPreview method is documentObject.PrintPreview n The syntax of the PrintOut method is documentObject.PrintOut, which prints one copy of the entire documentObject 5

39 Printing a Document n The PrintOut method’s full syntax contains many optional arguments that give you greater control over the printing of the document n To complete and test the PrintInvitation procedure, use the steps on pages 305 to 308 of the textbook 5

40 The Dinner Macro Toolbar 5

41 Invitation Document Shown in Print Preview 5

42 Access Lesson: Creating the AssignDates Procedure n Begin by opening the database and viewing the ProjectsForm form n Then open the Visual Basic Editor and view the code template for the AssignDates procedure n Use the steps on pages 312 and 313 of the textbook to open the database, view the form, and then view the AssignDates procedure in the Visual Basic Editor 5

43 Pseudocode for the AssignDates Procedure 5

44 Creating the AssignDates Procedure n To code the AssignDates procedure, use the steps on pages 314 to 316 of the textbook 5

45 Referring to a Control on a Form 5 n Each of the text boxes on a form is considered a Control object in VBA, and each belongs to the Form object’s Controls collection n You can use the formObject.Controls(controlName) syntax to refer to a control on a form n In the syntax, controlName is the name of the control and is enclosed in quotation marks n To complete the AssignDates procedure, use the steps on pages 316 and 317 of the textbook

46 Completed AssignDates Procedure 5

47 Creating a Custom Toolbar and Button n Rather than running a macro from the Database window, you can give a user access to the macro by adding a button to either one of Office 2000’s existing toolbars, or you can create your own toolbar and add it there n To create a custom toolbar, and then add to it a button that represents a macro, use the steps on pages 317 to 320 of the textbook 5

48 Projects Toolbar Added to the Database 5

49 AssignDatesMacro Macro Being Dragged to the Projects Toolbar 5

50 Location of Completed Projects Toolbar 5

51 Creating a Custom Toolbar and Button 5 n To test the macro and the AssignDates procedure, use the steps on pages 320 and 321 of the textbook


Download ppt "Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement."

Similar presentations


Ads by Google