visual basic for application - microsoft access 2003 programming applications using objects

17
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Upload: margaret-pearson

Post on 17-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Visual Basic for Application - Microsoft Access 2003

Programming applications using Objects

Page 2: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST2

Introduction

• VBA is an object-oriented programming (OOP) language.

• OOP is a type of programming in which programmers define a complete data structure from data types to the operations that can be applied to the data structure.

• The basic building block of OOP is the object. An object is a set of code that represents a thing in our program. A “thing” can be anything: customer, car, house, employee, etc.

• Objects have two fundamental things that they do:– Store Data– Logic to perform an action

Page 3: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST3

Course objectives

• This course will explain you the basic concepts of the object-oriented programming environment, such as using objects in your programs.

• You will learn the definition of properties, methods, and events, and when to use each one.

• Because the code is encapsulated into objects with properties, methods, and events available for the objects - a concept called object-oriented programming (OOP)- you can build reusable objects that save a lot of development time.

• Coding is simplified because the system is organized into smaller sets of interrelated objects, instead of one huge object.

• Your code will be easier to maintain because it will be easier to understand.

Page 4: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST4

Elements of an object

• Definition: Objects are all the things that make up your Access database, such as forms, reports, modules, macros, and tables. Each object has properties that determine its behavior or appearance, as well as methods that determine which actions can be taken upon it. Objects also respond to events that are triggered by actions taken by the user or caused by the system.

Page 5: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST5

What is a Property?

• Definition: Properties are characteristics of an object that determine its behavior or appearance.

Examples of properties include the name, caption, or size of a control.

• The property values that are set in the Properties dialog box, such as the Caption property shown previously, are called design time settings. Design time settings are those you specify at design time that become the starting value settings for that object. The starting value can be modified at runtime in your VBA code, or at a later time in design view. For example, to change the Caption property of the preceding form at runtime while the program executes, you could use the following VBA command:

Forms.Item("frmTestProperties").Caption = "New caption goes here."

Page 6: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST6

What is a Method?

• Definition: Methods are actions that can be taken on an object. They are really sub procedures or functions that are called to perform a particular action. You do not see the code behind sub procedures or functions when using existing Access objects, but they are there.

An example of a method you have already used is: Debug.Print

• Remark: After you make the call to the object, you type a dot (or period). As soon as you do, Auto List Members opens, giving you a list of properties and methods available.

Page 7: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST7

What is an Event?

• Definition: Events are triggered by actions taken by the user and the application, such as changes to data, mouse movements, or the opening of a form. You use VBA code or macros to respond to events. In other words, you link up the code you write with the user interface of your application through events.

For example, you specify which VBA procedure or function to run when a particular event happens.

• Microsoft Windows already has the code to handle routine events built right into it. Because VBA and Access are so closely tied to the Windows environment, a lot of routine functions will be handled without your writing a drop of programming code. All you need to do is tell Access what action you want to occur.

• Once you assign the action, Access writes an event procedure. This is a small block of VBA code that is written for you.

Page 8: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST8

What is Collection?

• Definition: A collection comprises multiple objects. Thus, an object can be a member of a collection. Sometimes you may want to manipulate a specific object;

For example, a particular form, whereas other times you may want to manipulate a collection of objects (all the forms in your database).

Page 9: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST9

Elements of an object–Exercise 1

• It’s your turn now to try your hand at an example that uses properties, methods, and events. As you work, try to keep track of which items are properties, which are methods, and which are events.

1. Create a new database and specify the file name as ObjectExamples. You can close the empty table that opens by default, as you will not be using a table in this example.

2. Add a new form to the database and then select the Design View.

3. On the new form, use the ToolBox to select and draw a text box control and a command button on the form. You can select Cancel to the wizard dialog boxes that appear.

Page 10: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST10

Elements of an object–Exercise 1

4. Select the text box control and from the Properties dialog box, select the All tab, and change the Name property to txtDateOfBirth.

5. Select the label. From the Properties dialog box, change the Name property of the label to lblDateOfBirth. Change the Caption property of the label to Enter Date of Birth.

6. You may need to resize the label to display all its contents.

7. Select the Command button. From the Properties dialog box, change the Name property of the Command button to cmdCalculate, and change the Caption property to Calculate. The form should now look like this:

Page 11: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST11

Elements of an object–Exercise 1

• Close the form to save it, and when prompted, specify the name frmTestObjects for the form. Reopen the form by selecting it in the Navigation Pane, and switch back to design view.

• Select the txtDateOfBirth text box again. Select the Event tab of the Properties dialog box, and locate the On Exit event. Select the Code Builder option to open VBE to write code for the event. One way to do so is to choose Event Procedure in the drop-down list and then click the ellipsis (…). You can also select the Ellipsis button and then choose Code Builder from the dialog box that appears.

• Add the following code to the Exit event:Private Sub txtDateOfBirth_Exit(Cancel As Integer)

'if the user did not enter a date, then 'display an error and set the focus back

'to the date of birth text box control.

If Not IsDate(txtDateOfBirth.Text) Then

MsgBox "You must enter a date for the Date of Birth field."

txtDateOfBirth.SetFocus Cancel = True

End If

End Sub

Page 12: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST12

Elements of an object–Exercise 1

• Select the cmdCalculate command button. On the Event tab of the Properties dialog box, select the On Click event, then click the ellipsis, then Code Builder, and add the following code to the newly created event procedure as shown here:Private Sub cmdCalculate_Click()

'declare local variable for month of birth

Dim intMonthOfBirth As Integer

'convert the value in the text box to a

'month and assign the value to the month of birth variable

intMonthOfBirth = DatePart("M", CDate(txtDateOfBirth))

'display a message to the user regarding the month of birth

MsgBox "Month you were born: " & intMonthOfBirth

End Sub

• Click the Save button from the toolbar to save all the changes. Return to the frmTestObjects form in Access and click the Save button to save all changes on the form. Run the form by clicking the Home ribbon and clicking the Form View item on the View list.

Page 13: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST13

Elements of an object–Exercise 1

• Enter something in the text box other than a date:

• Now, click the Calculate button. The message should be similar to the one shown

• After you click the OK button, focus returns to the Date of Birth text box field. Type your date of birth:

• Click the Calculate button. You should get a message box giving your month of birth.

Page 14: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST14

Viewing Objects Using the Object Browser

• Now that you understand what objects and their respective properties, methods, and events are, let’s look at another way to view objects. The Object Browser is a tool that allows you to view and manage the objects in your application. You can use the Object Browser to learn about the objects available in Access 2003.

• The Object Browser can be opened from the Visual Basic Editor in one of three ways: – by choosing the Object Browser button on toolbar, – by selecting View Object Browser, – or by pressing F2.

Page 15: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST15

Ways to Refer to Objects

• You should also know the various ways to refer to an object. Here are some examples:

Forms.frmTestObjects

Forms!frmTestObjects

Forms("frmTestObjects")

Forms(0)

• Using the period (Forms.frmTestObjects) is the preference for most situations because you can see the list of available properties, methods, events, and constants as you type.

• You can also refer to objects using the Me keyword as a shortcut. Me refers to the current object. So if you use Me in a form module, it refers to the form. Here is an example:

Me.txtDateOfBirth

• An equivalent, but longer, version of the same statement is:

Forms.frmTestObjects.txtDateOfBirth

• Only use the Me keyword when you want to refer to the existing object (not to a totally different object).

Page 16: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST16

Any Question?

Page 17: Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects

Copyright © 2007 - CIST17

The End