Using Public Sub Procedures and Functions

To implement a method for a custom class, you simply add a public sub procedure or function to the class itself. You are already familiar with creating new procedures, so creating a custom method will be really easy for you. To add a method called TestMethod to a class module, you simply add the following code:

Public Sub TestMethod()

'code for the method goes here End Sub

Now, create the four methods for the clsCar class: Retrieve, Add, Update, and Delete. You may wish to refer to Figure 4.2 to see how the class diagram maps to the code you write.

Try It Out Creating the Methods for the Car Class

1. Add the Retrieve method to the clsCar class.

Public Function Retrieve(ByVal VehicleId As Integer) As ADODB.Recordset

'Retrieve the detail record for the car 'from the database

'code to call the data access layer that retrieves the record from 'the database

Debug.Print "clsCar.Retrieve method"

Debug.Print "VehicleID: " & VehicleId & " will be retrieved from the database."

End Function

2. Add the Add method to the clsCar class.

Public Sub Add()

'Add the current values in the Car object to the database

'code to call the data access layer that adds the record to the database

Debug.Print "clsCar.Add method"

Debug.Print "New record being added for VehicleId:

" & Me.VehicleIdNumber

End Sub

3. Add the Update method to the clsCar class.

Public Sub Update()

'Update the database with the current contents of

the Car object

'Code to call the data access layer that updates 'record in the database

the existing

Debug.Print "clsCar.Update method"

Debug.Print "Record being updated for VehicleId:

" & Me.VehicleIdNumber

End Sub

4. Add the Delete method to the clsCar class.

Public Sub Delete()

'Delete the Car record from the database

'Code to call the data access layer that deletes database

'Pass the VehicleId value as a parameter

the record from the

Debug.Print "clsCar.Delete method"

Debug.Print "Record being deleted for VehicleId:

" & Me.VehicleIdNumber

End Sub

5. Click the Save button on the toolbar to save your changes to the class.

0 0

Responses

  • oliver findlay
    How do I create code in a public sub in access 2003?
    7 years ago

Post a comment