User Form And Controls in Visual Basic 6 (VBA)
User forms allow to defined the custom Graphics User Interface (GUI) to collect user inputs, show outputs or provide an interaction with your application.
User form can be added by calling the Insert UserForm command
By default forms will be named as UserForm1, UserForm2, etc., but it is recommended to give forms meaningful names.
Adding Controls
Form can be customized and additional controls can be placed onto the form.
- User Form design layout
- Toolbox with controls
- Control placed on the form layout
- Properties of the control
Properties of the controls can be customized.
Code Behind
Form and its controls are exposing different events, such as click, select, mouse move etc.
Event handlers are defined in the code behind of the form.
Available control events can be selected from the drop-down list.
Private Sub CommandButton1_Click() MsgBox "CommandButton1 Clicked!" End Sub
Displaying Form
Form can be displayed by calling the Show method. This method should be called on the variable which equals to the form name. Note, it is not required to declare or instantiate the form variable explicitly (as it is required with the classes). This is done automatically when form is added to the project.
For can be displayed in 2 modes
Modal
In this mode form is opened foreground and parent window is not accessible until the form is closed.
Sub main() UserForm1.Show End Sub
Modeless
Form is opened in a way that parent window is accessible and not blocked. To open the form in the modeless mode it is required to pass the vbModeless parameter to Show method.
Sub main() UserForm1.Show vbModeless End Sub