Download presentation
Presentation is loading. Please wait.
Published byMargaret Pearson Modified over 9 years ago
1
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects
2
2 Copyright © 2007 - CIST 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
3
3 Copyright © 2007 - CIST 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.
4
4 Copyright © 2007 - CIST 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.
5
5 Copyright © 2007 - CIST 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."
6
6 Copyright © 2007 - CIST 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.
7
7 Copyright © 2007 - CIST 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.
8
8 Copyright © 2007 - CIST 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).
9
9 Copyright © 2007 - CIST 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.
10
10 Copyright © 2007 - CIST 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:
11
11 Copyright © 2007 - CIST 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
12
12 Copyright © 2007 - CIST 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.
13
13 Copyright © 2007 - CIST 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.
14
14 Copyright © 2007 - CIST 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.
15
15 Copyright © 2007 - CIST 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).
16
16 Copyright © 2007 - CIST Any Question?
17
17 Copyright © 2007 - CIST The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.