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.

Slides:



Advertisements
Similar presentations
Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to.
Advertisements

Chapter 3: Using Variables and Constants
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Tutorial 8: Developing an Excel Application
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
VBA for MS Excel Hamze Msheik. Open the Visual Basic Editor in Excel 2007 Click on the Microsoft Office button in the top left of the Excel window and.
String Variables Visual Basic for Applications 4.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Using the Visual Basic Editor Visual Basic for Applications 1.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
1 Chapter 20 — Creating Web Projects Microsoft Visual Basic.NET, Introduction to Programming.
Adding Automated Functionality to Office Applications.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Using the Select Case Statement and the MsgBox Function (Unit 8)
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
Object Variables Visual Basic for Applications 3.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Using the selection structure (Unit 7) Visual Basic for Applications.
Working with Numeric Variables (Unit 6) Visual Basic for Applications.
Numeric Variables Visual Basic for Applications 6.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
OCC Network Drives  H:\  P:\ 
The Object Model Visual Basic for Applications 2.
Key Applications Module Lesson 21 — Access Essentials
Working with option button, check box, and list box controls Visual Basic for Applications 13.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 9 Macros And Visual Basic For Applications.
Working with Date Variables (Unit 5)
Chapter 5: More on the Selection Structure
Microsoft® Excel Key and format dates and times. 1 Use Date & Time functions. 2 Use date and time arithmetic. 3 Use the IF function. 4 Create.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Advanced Repetition Structure and String Functions (Unit 10) Visual Basic for Applications.
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
XP 1 ﴀ New Perspectives on Microsoft Office 2003, Premium Edition Excel Tutorial 2 Microsoft Office Excel 2003 Tutorial 2 – Working With Formulas and Functions.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Chapter 3: Using Variables and Constants
Microsoft Access Illustrated
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Excel
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

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 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

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

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

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

Examples of Dim Statements that Reserve Date Variables 5

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

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

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

AssignDisplayDate Procedure 5

Message Box Displayed by the AssignDisplayDate Procedure 5

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

Help Screen Showing the VBA Predefined Date/Time Formats 5

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

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

Valid Settings for the Interval Argument 5

Examples of the DateAdd Function 5

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

Examples of the DateDiff Function 5

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

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

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

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

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

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

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

Pseudocode for the CalcHours Procedure 5

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

Partially Completed CalcHours Procedure 5

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

Illustration of the Offset Property 5

Completed CalcHours Macro Procedure 5

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

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

Invitation Document 5

Pseudocode for the PrintInvitation Procedure 5

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

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

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

The Dinner Macro Toolbar 5

Invitation Document Shown in Print Preview 5

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

Pseudocode for the AssignDates Procedure 5

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

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

Completed AssignDates Procedure 5

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

Projects Toolbar Added to the Database 5

AssignDatesMacro Macro Being Dragged to the Projects Toolbar 5

Location of Completed Projects Toolbar 5

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