10.2 MultiPage Controls, Labels, Textboxes
Ongoing tutorial - First part is here: Part One
Open up the Excel VBA Editor. From the menus at the top, click on Insert. From the Insert menu, select User Form. With the form selected, locate the properties area on the left, and the Name property. (If you can't see any Properties, click View from the menu at the top of the Excel VBA Editor. From the View menu, select Properties Window. You can also press F4 on your keyboard as a shortcut.) Delete the default of UserForm1 and type PictureViewer. Change the following properties, as well:
Caption: Excel Picture Viewer
MultiPage Controls (Tabs)
The first object we want to add to the form is the MultiPage control. This will give us the tabs we need.
Make sure your form is selected and examine the toolbox. (If you can't see the toolbox, click View from the menu at the top of the Excel VBA Editor. From the View menu, select Toolbox.) Now click on the MultiPage control, which is circled in red in the image below:
In the properties area for the MultiPage, change the Height to 378 and the Width to 558. Set the Top property to 10 and the Left property to 6.
By default, the MultiPage control has two pages. This is fine for us. To change the captions on the pages, click on the first one, which has the default name Page1. (Click exactly on the text Page1.) This will open up the properties for that page, rather than the MultiPage control as a whole. Locate the Caption property of Page1 and change it to View Photos. Now do the same for Page2. Click on the text Page 2 and change the Caption property to Add New Photo. The top of your form should now look like this:
We can now add the controls to the MultiPage. The controls we want on the View Photos page are 7 Labels, 6 Textboxes, 1 ComboBox, 2 option buttons, 3 command buttons, and 1 picture frame. We'll add the labels first.
Adding Labels to a VBA User Form
Locate the label control in the toolbox, the one circled in the image below:
Draw out the first label anywhere on your form. Now set the following properties for the label:
Caption: File Name
Locate the Font property and click the grey button with the three dots in it:
Clicking the button brings up the Font dialogue box:
Set the font to Tahoma, Bold, 10 points. Then click OK.
Instead of drawing out the other 6 labels and setting the properties, you can copy and paste the first label. The pasted labels will then have all the same properties as the copied one (except for Top and Left).
Click on your first label to highlight it, then press CTRL + C to copy it. Press CTRL + V on your keyboard to paste it. The label should appear somewhere in the middle of the form. Drag it to the left somewhere, and then paste another 5 times. You'll then have 7 labels that say File Name.
To left-align all the labels, hold down the CTRL key on your keyboard. Keep it held down and left click each label in turn until they are all selected, as in the image below:
In the properties area, locate the Left property. Type in a value of 24 and press the enter key on your keyboard. All your labels will then left-align to the desired position.
Now set the following values for each label:
Caption: Date Taken
Caption: Image Size
Adding Textboxes to a Form
Now that the labels are all done, add 6 Textboxes. For the first Textbox you can set the following properties:
Font: Tahoma, Bold, 10 points
Now copy and paste the first Textbox 5 times. Set the Left property for the other 6 textboxes by highlighting them all and typing 96 into the Left property area, just like you did for the labels.
Set the following individual properties for the Textboxes:
Your form should now look like this:
In the next lesson below, you'll add the Option Buttons and the Command Buttons.
Next Lesson: 10.3 Buttons >
<< Excel VBA Course Menu
Lots more free online courses here on our main Home and Learn site
© All course material copyright Ken Carney