1. Home
  2. Computing & Technology
  3. Visual Basic

Good Form in Programming

By Dan Mabbutt, About.com

May 30 2008

The most common way for controls to be added to a document is to add a UserForm to the project first and then place controls on the UserForm. This way, the document is separate from the UserForm. The form is probably the most fundamental object in Visual Basic. It's one of the main "containers" for code. A form also has methods, properties and events that you can use as well. For example, you can assign an image to the UserForm's Picture property.

In this case, we'll use a UserForm to add our 'Name' and 'Great Work' values for substitution into the document. Two TextBox controls will allow the values to be entered and a CommandButton control will contain the code to add them. Finally, two Label controls will be added to identify TextBox controls.

If you added components to your document during the earlier discussion, you might want to delete them again just to keep things organized.

To add a UserForm to the project, first open the Visual Basic editor. You can add a UserForm by either right-clicking the project in the Project Explorer window and then selecting Insert > UserForm, or select Insert > UserForm from the VB editor menu. (Make sure the Normal template is not selected or it will be inserted in the wrong place.) Do this now.

To input our 'Name' and 'Great Work' values for substitution into the document, simply click the a TextBox control and then 'draw' it by dragging the crosshair on the UserForm. If the Toolbox isn't visible, you can display it by clicking the icon for it in the Standard toolbar. To prevent errors, you can only activate the Toolbox when a UserForm is in the active pane because components can only be inserted into a UserForm.

We will also need a CommandButton control to run the program that will do the substitution so drag one of those to the UserForm. Change the Name property of the TextBox and CommandButton controls so we can reference them in the code we will write next. I used txtName and txtBook for the TextBox controls and cmdRunIt for the CommandButton. If you're in a hurry, there's no real reason to change the default names assigned to the Label controls since we won't reference them in the code. Just change their Caption properties to identify the TextBox controls. To make everything look right, change the Caption property of the CommandButton and the UserForm.

Here's what my form looked like after these changes were made. You can 'run' the form if you want to see it in more of a 'production' view. Since no programming code has been written yet, nothing will happen except that the form will be displayed. So let's fix that next.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB 6
  5. Learn VBA
  6. Learn VBA Macro Coding with Word 2007 - A "From the Ground Up" Tutorial

©2009 About.com, a part of The New York Times Company.

All rights reserved.