Microsoft training courses | Wise Owl - home page

Phone (01457) 858877 or email

ADVANCED CONTROLS IN USERFORMS WITH VBA MACROS

Part two of a nine-part series of blogs

The two previous parts of this mini-blog have shown how to draw forms and how to write code to handle form events.  This final part shows how to add some of the more exotic controls to user forms, like combo boxes, list boxes, multipage controls, spinners and option buttons.

  1. Advanced Controls - Our Example
  2. Multipage Controls (this article)
  3. Combo boxes (and list boxes)
  4. Multiple Column Combo Boxes and Listboxes
  5. Multi-select ListBoxes
  6. SpinButtons (Spinners)
  7. Check Boxes
  8. Option Buttons (Radio Buttons)
  9. The Calendar Control

This blog is part of our Excel VBA tutorial.  Wise Owl's main business is running Excel, VBA and other courses for businesses.

Posted by Andy Brown on 28 February 2012 | 3 comments

Multipage Controls

A multipage control allows you to get lots of information into a single form:

Multipage - page 1 Multipage - page 2 Multipage - page 3
Page 1 Page 2 Page 3

A TabStrip control, by contrast, is useless - for a TabStrip every "page" of the form contains the same boxes, so they can only be used to display records from a table.

Creating a MultiPage Control

Make sure that you use the MultiPage (not the TabStrip) control!

MultiPage control selected

Click on the MultiPage tool, and click where you want it to go on your form.

 

You can then insert, move, rename and delete pages using the right mouse button:

Context menu for multipage control

Right-click on the page tabs at the top of the control to add new pages or rename or move existing ones in a fairly obvious way.

 

How MultiPage Controls Work

In a multipage control, if you paste or add a control onto a page, it belongs to that page; otherwise, it belongs to the form:

Multipage with two boxes showing controls

In this form:

  • Controls in the red box were added to the About you tab and hence belong to it.
  • Controls in the blue box were added to the form itself, and will stay visible whichever tab you click on.
 

Each page of a multipage control has its own tab order:

Tab order dialog box for one page

Here we're looking at the tab order for the About you page of the multipage control.  The other pages will have their own tab orders.

 

Setting Focus to a Control on a Multipage

The only slight complication with a multipage is going to a control on it.  You do this - surprisingly - by setting the Value property of the MultiPage:

'check user chosen a name

If Len(Me.txtName.Text) = 0 Then

'if not, go to first page ...

MultiPage1.Value = 0

'... and go to the offending text box

Me.txtName.SetFocus

'display error message and abort

MsgBox "You must say who is ordering drink!"

Exit Sub

End If

In the code above, the line:

'if not, go to first page ...

MultiPage1.Value = 0

makes the first page of the multipage the current one (the pages are numbered from 0).  Without it, the line:

'... and go to the offending text box

Me.txtName.SetFocus

would crash the macro, because you can't go to a control which isn't on the active page of the MultiPage.

 

ADVANCED CONTROLS IN USERFORMS WITH VBA MACROS

Part two of a nine-part series of blogs

The two previous parts of this mini-blog have shown how to draw forms and how to write code to handle form events.  This final part shows how to add some of the more exotic controls to user forms, like combo boxes, list boxes, multipage controls, spinners and option buttons.

  1. Advanced Controls - Our Example
  2. Multipage Controls (this article)
  3. Combo boxes (and list boxes)
  4. Multiple Column Combo Boxes and Listboxes
  5. Multi-select ListBoxes
  6. SpinButtons (Spinners)
  7. Check Boxes
  8. Option Buttons (Radio Buttons)
  9. The Calendar Control

This blog is part of our Excel VBA tutorial.  Wise Owl's main business is running Excel, VBA and other courses for businesses.

Comments on this blog

This blog has 3 comments:

Comment added by VBAGuy on 02 October 2012 at 02:41 GMT
I was looking at the option boxes and got stuck when you went to the me.option..... I keep getting an invalid use of me error message. I am not sure if you defined it somewhere up above or what. I would use a combo box but I have been trying to figure out option boxes just to know how to use them if I did and I think they look cleaner
Reply from Andy Brown (blog author)
You're probably trying to use Me to refer to the current form, but doing it within a code module (in which case Me would actually refer to the active Excel workbook).
 
Comment added by BernieG on 24 December 2012 at 13:20 GMT
Cheers Andy
Have been struggling with userform multipages & yours is the best article I've seen by miles.
I only program now and again and like to construct my code in the same manner as you.
Putting the bouncers on the door making sure the correct information is in the right place.

Cheers
          Bernie
  
 
Comment added by kevinzimm on 27 December 2012 at 07:42 GMT
I'm not finding the calendar in my list of additional controls?  Thoughts?
Reply from Andy Brown (blog author)
Choose Tools from the menu in VBA, choose Additional Controls...   and tick the Calendar Control 11.0 (or whatever number appears after it) in the list.  This should reference the required calendar control.  While you're there, you could have a look at the hundreds of other add-ins!

All content copyright Wise Owl Business Solutions Ltd 2013. All rights reserved.