1. Home
  2. Computing & Technology
  3. Visual Basic
Adding to the System
3 - Good Form in Programming
 Lesson 4
Adding to the System

1 - Making Sure We Understand The Problem (Again)

2 - The Parts Are Greater Than The Whole

3 - Good Form in Programming

4 - Event Filled Programming

5 - Where To From Here?


Let's jump immediately back into real coding again by opening the FormLetter document. If you want to add a control directly into your document, you can do that this way:

  • Make sure the Visual Basic Toolbar is selected (Click View > Toolbars > Visual Basic).
  • Make sure the Toolbox is displayed by clicking the Toolbox icon.
  • Double click or shift-click a control and it will be added in at the insertion point in the document. Note that Design Mode is toggled on automatically when you do this.

For most purposes, adding a control directly into the document isn't what you want to do because it prints as a graphic along with the document as well.

Adding the control is only the first part of the job. In addition, you have to add what is called event code to the macro. We'll get to that in the next section.

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 Form.

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 menu. (Make sure your project and not the Normal template is selected first or it will be inserted in the wrong place.)

The Form is probably the most fundamental object in Visual Basic. It's one of the main "containers" for code (event code, the subject of the next section) is maintained as part of the Form) the Form itself has methods, properties and events that you can use as a programmer. For example, you can assign an image to the Form's Picture property and change the Form's PictureTiling property to True to get an "interesting" background on the Form that you can see below.

To input our Name and 'Great Work' values for substitution into the document, simply drag and drop two TextBox controls onto the Form from the Toolbox. We will also need a CommandButton control to run the program that will do the substitution. Finally, two Label controls can be added to identify the controls. Change the Name property of the TextBox and CommandButton controls so we can reference them in the code we will write next. I used txtAddrName and txtGreatWork 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.
ComboBox 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, as well. Since there isn't any programming for the Form yet, nothing will happen except that the Form will be displayed.

4 - Event Filled Programming

Explore Visual Basic

More from About.com

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

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

All rights reserved.