Microsoft Access VBA Macro Programming

(Tina Sui) #1

with some very nice icons on them, such as Close Form, and if you choose the wizard
approach, you do not even have to write the code!
You can access the code behind the button by right-clicking the control and selecting
Build Event from the pop-up. Select Code Builder in the next window. This will take you to
the Visual Basic Editor window. Once you have done this, if you go back to your form and
look in the Properties window for your new button, you will see that the On Click event
property now reads [Event Procedure]. You can revisit the code by right-clicking the On
Click property value and then clicking Build in the pop-up.
The sample code to close the current form would be:


Private Sub Mybutton_Click()
DoCmd.Close


End Sub


List Box


The list box control works in a similar way to the combo box, but it displays a list of options
that is never hidden. It is not as popular as the combo box because it can take up more room
on the form.
It can be used in exactly the same way as the combo box by manipulating the same
properties described earlier in this chapter. The advantage is that you do not have a Limit To
List property since the user has no scope for input.


Image


The image control allows you to insert a picture into your form. If used, the picture becomes
embedded in the form and the application, thus increasing the size of the file. This control
can be very useful for displaying a company logo on each form to provide a corporate flavor.


Check Box


The check box control allows the user to check or uncheck a check box. You can easily find
out what action the user has taken by reading the value property (which returns True or
False):


Private Sub Detail_Click()
If Me.MyCheckBox.Value Then
MsgBox "Box is checked"
Else
MsgBox "Box is unchecked"


End If


End Sub


You can also set the Default Value property for the control to 0 orβˆ’1 to give it a starting
value. This can also be changed by setting the value property using VBA.


Chapter 9: Forms and Reports 109

Free download pdf