709-218-7927

The Landfall Garden House

60 Canon Bayley Road

Bonavista, Newfoundland

CANADA A0C 1B0

CPRGreaves@gmail.com

Home

Christopher Greaves

Programming Command Buttons

In the previous lesson we built a GUI form and on that form we placed some controls; specifically, four controls were placed – a text box, a list box and two command buttons.

In this lesson we will build program code to make the command buttons “work”.

In VBE double-click on the Cancel button. The GUI form will temporarily disappear and an empty procedure will appear in its place:

Private Sub cmdCancel_Click()
End Sub

Introduce one line of code into this skeletal procedure – the code says “Unload Me”.

“Me” is a standard name for the current user form; no matter what names or captions we have assigned the user form, within itself we can always refer to the form itself as “Me”.

Private Sub Cancel_Click()
Unload Me
End Sub

Run the form (with the F5 key) and observe that you can now clear the form away (as a user) by either tapping the Escape key or by clicking the Cancel button.

In VBE double click the OK button:

Private Sub cmdOK_Click()
End Sub

We want the OK button to display something useful BEFORE it hides the user form:

Private Sub cmdOK_Click()
MsgBox Me.tbInput
Me.Hide
End Sub

When you run the form, type some text in the text box and then click the OK command button; a small message-box should pop up containing the text you had keyed into the text box.

Of course, if you had typed no text, you’d get a message box with no message!

Private Sub cmdOK_Click()
If Me.tbInput <> "" Then
MsgBox Me.tbInput
Else
End If
Me.Hide
End Sub

In the next lesson we will learn how to initialize a list box, and how to make use of it in our GUI form.

709-218-7927 CPRGreaves@gmail.com

Bonavista, Sunday, December 08, 2024 9:31 AM

Copyright © 1990-2024 Chris Greaves. All Rights Reserved.