CUSTOMIZING A FORM

In Access 2, you had to live with a predefined set of properties and methods. Today, VBA lets you define your own additional form properties. With VBA, you can create properties and methods that can be referenced just as though they were built in. You're now ready for the next step of adding public properties and methods to the form.

Note

The forms created in this section are on the CD-ROM in the back of this book in the Chap02.mdb file, located in the ExamplesChap02 folder.


Writing Custom Properties

You can expose a form in two ways. The first way, which is very simple, is just to expose a form variable as public. This is done by placing the keyword Public in front of the variable's declaration. Follow these steps:

1.
Create a new form in Design view. From the View menu, choose Code to display the code window. In the declaration section of the code module, type the following line:

Public intLockForm as Integer

2.
Create a button on the form. Type Show Lock for the caption; then right-click the button and select Build Event. Click Code Builder and then click OK.

3.
Type the following line of code in the event handler:

MsgBox intLockForm

4.
Name the form frmUseVariable and save it.

5.
Open the form in Form view.

6.
Create a new form.

7.
Place a command button on the form. Name it cmdLockForm and set its caption to Lock Form.

8.
Right-click the button and select Build Event. Click Code Builder and then click OK.

9.
Type the following line of code in the event handler:

Form_frmUseVariable.intLockForm = _
    Not Form_frmUseVariable.intLockForm

10.
Save the form as frmTestVariable.

11.
Switch the form to Form view. Click the Locked button.

12.
Activate the frmUseVariable form and click the button. The value of intLockForm is displayed. If you go back to the other form and click the button, intLockForm changes.

You've created your first public form property. However, this isn't very exciting. You have no way to respond to changes in the property's value. Don't despair—VBA provides such functionality through a pair of special subroutines: Property Get and Property Let. These subroutines allow you to write code that is executed every time the variable is set or returned.

Now, create a new form called frmUseProperty. You're going to create a property called LockForm that has a little more intelligence than the intLockForm variable used on the frmUseVariable form, which was saved in step 4.

Note

When you use the Leszynski Naming Conventions, internal object properties don't include the data type in the name. Hence, the variable named intLockForm becomes the property LockForm.


The LockForm property, when set to true, locks all the controls that can be locked on the form; when set to false, it unlocks all the controls on the form. Follow these steps:

1.
You need to declare a private variable to store the state of the lock. Under the declarations section of the form, type the following line of code:

Private intLock as Integer

2.
Write the procedure that returns the value of intLock. Returning values of properties is done by using the Property Get statement:

Property Get LockForm as Integer
     LockForm = intLock
End Property

3.
When setting the LockForm property, you need to lock all the controls on the form. The code for assigning properties uses the Property Let statement:

Property Let LockForm(intValue as Integer)
     Dim ctlCurrent as Control
     On Error Resume Next
     For Each ctlCurrent in Me
          ctlCurrent.Locked = intValue
     Next
     intLock = intValue
End Property

The On Error statement in this code ensures that errors aren't generated for controls that don't provide a locked property. When such a control is encountered, it's skipped over.

4.
In the form's Load event, initialize LockForm to false with the following line of code:

LockForm = False

5.
Add a few more text box controls to the form.

Note

Because I'm using the Locked property to demonstrate using custom properties, you must have bound controls to lock. Therefore, I've created a table named tblUseProperty with three Text type fields in it: Text1, Text2, and Text3. You can see this table structure and the frmUseProperty form in Design view in Figure 2.8.

Figure 2.8. The tblUseProperty table supplies the text boxes for the form frmUse Property.


Another issue is that you can't lock unsaved bound forms. So in the routine attached to the OnDeactivate event of the frmUseProperty, you want the following lines of code:

Private Sub Form_Deactivate()
   Application.RunCommand acCmdSaveRecord
End Sub


6.
Save the form and switch the form to form view.

7.
Type something in the text boxes.

8.
Copy the frmTestVariable form to one called frmTestProperty. Switch to the frmTestProperty form in Design view.

9.
Modify the line of code behind the cmdLockForm command button's OnClick event from

Form_frmUseVariable.intLockForm = _
    Not Form_frmUseVariable.intLockForm

to be

Form_frmUseVariable.LockForm = Not Form_frmUseVariable.LockForm

10.
Add the following lines of code after the one just changed to have the caption on the cmdLockForm command button reflect the lock state of the frmUseProperty form accordingly:

If Forms!frmUseProperty.LockForm Then
      Me!cmdLockForm.Caption = "Unlock Form"
   Else
      Me!cmdLockForm.Caption = "Lock Form"
End If

The cmdLockForm_Click routine would then look like this:

Private Sub cmdLockForm_Click()
   Forms!frmUseProperty.LockForm = _
Not Forms!frmUseProperty.LockForm
   If Forms!frmUseProperty.LockForm Then
      Me!cmdLockForm.Caption = "Unlock Form"
   Else
      Me!cmdLockForm.Caption = "Lock Form"
   End If
End Sub

11.
Open the frmTestProperty form in Form view and click the button.

12.
Return to the frmProperty form. You should no longer be able to type in any of the controls, and the cmdLockForm caption should say Unlock Form.

You just created your first custom property. You can set as well as retrieve the value of the property.

Tip

To create a read-only property, just write a Property Get subroutine. Without the corresponding Property Let routine, no value can be assigned to the property, but a value can be returned.


Note

When writing code within the form, you can reference the intLock variable directly. Keep in mind that assigning a value directly to the internal variable doesn't cause the form to get locked. Therefore, even when writing code within the form, you should call the property instead of the private variable.


Writing Object-Valued Properties

Object-valued properties are properties that return a value or can be assigned an object. For example, when you assign a control to a variable by using the Set statement, you're using an object-valued property. You also can write code that returns objects by using the Property Set statement. An example of returning object-valued properties is covered in Chapter 4.

Writing Custom Methods

The LockForm property demonstrated earlier in the section “Writing Custom Properties” also can be easily created as a method as follows:

Public Sub LockForm(intLock as Integer)
     Dim ctlCurrent as Control
     On Error Resume Next
     For Each ctlCurrent in Me
          ctlCurrent.Locked = intLock
     Next
End Sub

This method works as expected. Calling LockForm(True) locks the form, and calling LockForm(False) unlocks the form. However, there are fundamental differences in the calling code.

Using the LockForm method:

LockForm True

Accessing the LockForm property:

LockForm = True

With a property, you're assigning a value to the property. If you use a method, there's no way to easily determine the value of LockForm. Rather than create a Public Sub, you can create a Public Function that returns a value. This value can be used to indicate the success or failure of the operation. However, this still doesn't provide a way to determine the current lock state.

Public methods and functions are useful when you want an action to occur that doesn't remember any state. Examples of methods in Access are the Repaint method on the form, which causes the form to paint itself, and the Requery method on a list box, which causes the contents of the list to be retrieved from the database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.39.32