Subscribe to Software Autopsy        RSS Feed

VBA User Forms

Icon Leave Comment
User Forms

User forms are used for creating a user interface. A user interface is used for gathering inputs from the user/ display some messages, or if you want to display some dialog boxes. for example you want to create your own dialog box because the default dialog box does not offer the necessary or you want to create some user interface from which the user can select some thing , the program you wrote will perform in the background while displaying the process output on the form, and at times prompting for additional inputs.

Creating input form

To create a user form, open the Visual Basic editor using ALT+F11 and choose user form from the the Insert Menu.

I have the user form now what?

On a user form we can add objects, we know what are objects. Each of the objects have some specific functionality, some specific properties and some common properties. For example some objects have properites of height and width which are related to the height and width of the obejct on the screen. Another set of properties left and top relate the coordinates of the object on the screen. The name property of the object is used to detemine by which name the object will be accessed in the code. Each object has its own naming convention, and sticking to it makes programming life easier, making the code look more professional and easy to understand and maintain. Of course make sure that the code you write is commented.

The toolbox contains the objects or controls. To view the toolbox, choose the toolbox from the view menu.

To place any control on the form
1) You can drag and drop it on the form. You can adjust the height and width of the control, or place it any where on the form by moving it around the form.
2) You can click the control and then click on the form
3) Or you can click the control, click on the form, you get a black rectangle you can drag it on the form, relase the left mouse button when you satisfied with the size of the control.
First let us take a look at a Label, a textbox.


The label is identified by a capital 'A' on the toolbox.

A Label is used to display a static text like a caption or a hinting what input is to be provided. In other words the idea of a label is provide a visual clue to the user on the type of input. For example if a date input is required from the user you can dispaly the format in the label.
Press F4 to view/set properties of a control/object. Alternatively you can choose Properties from the View Menu
The most commonly used properties of a label are Name,Caption, Backstyle. In the code you can access these properties and others like Width,Height, Top,Left etc.

Name:- You can use the NAME property to set the name for this control. Using the name property you can access this object in your code. For example name of a label lblname or lblcity or lblbasic etc. In these examples of the lable name indicates the input that is accepted in the corresponding textbox

Caption:- The text that is displayed in the label for example "enter name", "select date of birth", "enter date of birth" etc.

Now coming to the textbox:- The naming convention for the a textbox is txt and the name you want. For example txtname,txtdob,txtaddress,txtzip,txtlocation etc.

I am assuming that you have come accross the Name ranges in Excel, with that assumpiton I am using this example

The user will enter a name range in a textbox and click the command button, and the name range is copied to another workbook

0 Comments On This Entry


April 2021

181920 21 222324

1 user(s) viewing

1 Guests
0 member(s)
0 anonymous member(s)

Recent Entries

Search My Blog