Home

Introduction to Forms

 

Forms Fundamentals

 

Introduction to Forms

A computer application, such as those that run on Microsoft Windows, is equipped with objects called Windows controls. These are the objects that allow a person to interact with the computer.

The primary control used on most applications is called a form.

 

Practical LearningPractical Learning: Introducing Forms

  1. Start Microsoft Excel
  2. On the Ribbon, click Developer
  3. In the Code section, click Visual Basic

Creating a Form

To create a form, on the main menu of Microsoft Visual Basic, you can click Insert -> UserForm. This would add a form to your project. In the same way, you can add as many forms as you want.

The form is primarily used as a platform on which you add other controls. For this reason, a form is referred to as a container. By itself, a form is not particularly useful. You should add other objects to it.

When you create or add a form, a module is also automatically created for it. To access the module associated with a form, you can right-click the form and click View Code.

Practical LearningPractical Learning: Creating a Form

  1. To create a form, on the main menu, click Insert -> UserForm
  2. To access its associated module, right-click the form and click View Code
  3. To return to the form, on the main menu, click Window and click the menu item that has (UserForm).

Using a Form

 

Showing a Form

Although you create a form in Microsoft Visual Basic, you view its results in Microsoft Excel. You have various options.

A form is referred to as modal if the user cannot access anything from the same application while the form is displaying. A form is called modeless if the user can click something of the same application behind that form while the form is displaying.

To display the run-time view of a form in modal view:

  • While in Microsoft Visual Basic, you can press F5
  • On the main menu of Microsoft Visual Basic, you can click Run -> Run Sub/UserForm
  • On the Standard toolbar of Microsoft Visual Basic, you can click the Run Sub/UserForm button Run. This would send the form to Microsoft Excel and display it in the normal view

You can also programmatically display a form. To support this, the UserForm object is equipped with a method named Show. Its syntax is:

Public Sub UserForm.Show(Optional ByVal Modal As Boolean)

This method takes one Boolean argument that is optional. If you omit it, the form would display as modal and the user cannot do anything else in Microsoft Excel as long as the form is not closed. That's the default behavior. If you want to display the form as modeless, pass the argument as False. Here is an example:

Private Sub Exercise()
    UserForm1.Show False
End Sub

Printing a Form

If you have equipped a form with many aesthetic objects you want to see on a piece of paper, you can print it. To support printing, the UserForm object is equipped with a method named PrintForm. Its  syntax is:

Public Sub PrintForm

This method takes no argument. When called, it sends the (body of the) form directly to the printer. Here is an example of calling it:

Private Sub Exercise()
    UserForm1.PrintForm
End Sub

Hiding a Form

As opposed to displaying a form, if it is already showing, you can hide it. To allow you to hide a form, the UserForm object is equipped with a method named Hide. Its syntax is:

Pyblic Sub UserForm.Hide

This method takes no argument. When called, it hides the form (without closing it). Here is an example of calling it:

Private Sub Exercise()
    UserForm1.Hide
End Sub

Closing a Form

After using a form, the user can close it by clicking the system close button. To programmatically close a form, use the End statement. Here is an example:

Private Sub Exercise()
    End
End Sub
 

 

 
 

The Characteristics of a Form

  

The Name of a Form

 Like every object on a computer, the primary characteristic of a form is its name. After creating a form, access its Properties window, click (Name), type the desired name and press Enter

Practical LearningPractical Learning: Naming a Form

  • If the Properties window is not displaying, right-click the form and click Properties window.
    In the Properties window, click (Name) and type frmCleaningOrders

The location of a Form

When a form displays in normal view to the user, it is usually centered. The user can then drag its title bar to move it around. You too can move a form.

If you want to specify the position a form would assume when it displays, at design time, access its Properties window. Then change the values of the Left and the Top properties. You can also programmatically control the location of a form. You have two options. You can assign the desired values to its Left and/or Top properties. Here is an example:

Private Sub Exercise()
    UserForm1.Left = 400
End Sub

Al alternative is to call the Move method. Its syntax is:

Public Sub UserForm.Move(ByVal Left As Single, ByVal Top As Single, Optional ...)

This method takes two required arguments. The first represents the left distance and the second is the top distance. Here is an example:

Private Sub Exercise()
    UserForm1.Move 200, 200
End Sub

The Size of a Form

When you have just added a new form, it assumes a default size. If that size doesn't fit your intentions, you can change.

To change the size of a form, you can access its Properties window. Then change the values of the Height and Width. To programmatically change the size of a form, you have many options. You can assign the desired values to its Height and/or to its Width. Here is an example:

Private Sub Exercise()
    UserForm1.Width = 600
End Sub

Another option is to call the Move method. In reality, this method takes two required arguments and two optional arguments. Its actual syntax is:

Public Sub UserForm.Move(ByVal Left As Single, ByVal Top As Single, _
	            Optional ByVal Width As Single, Optional ByVal Height As Single)

The last two optional arguments allow you to specify the size of the form. Here is an example:

Private Sub Exercise()
    UserForm1.Move 200, 200, 1040, 600
End Sub

Practical LearningPractical Learning: Resizing a Form

  1. Position the mouse in  the lower-right section of the form
  2. Click and drag right and down
  3. Return to Microsoft Excel
  4. To close Microsoft Excel, click the Office Button and click Exit Excel
  5. When asked whether you want to save the file, click No
 
 
   
 

Previous Copyright © 2008-2010 FunctionX, Inc. Next