Microsoft training courses | Wise Owl - home page

Phone (01457) 858877 or email

ADVANCED CONTROLS IN USERFORMS WITH VBA MACROS

Part four 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
  3. Combo boxes (and list boxes)
  4. Multiple Column Combo Boxes and Listboxes (this article)
  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

Multiple Column Combo Boxes and Listboxes

Why Use a Multi-Column List

It can sometimes be useful to display one thing in a combo or list box, but store another.  Here's a case study where we want to get the id field of the person ordering a drink, but display the name:

List of person names and ids

We have a list of records from a database query or view, and want to allow a user to choose a person.

 

We want to be able to choose by name, but store by number:

Drop list of people Worksheet showing person id chosen
You choose the person ... ... but store the name

Combobox or Listbox Properties for Multiple Columns

To get our example above to work you need to set some properties of the combo or list box:

Properties of combo box

We'll need to set the following properties:

  1. The ColumnCount is 2, because there are two columns: the one shown, and the one whose value is stored.
  2. The BoundColumn is 2, because it's column 2 whose value we are storing.
  3. The ColumnWidths shows that the first column will be visible, but the second one won't be (it'll have zero width).
  4. The ControlSource shows that the value chosen will be stored in the range called PersonId on the worksheet.
  5. The RowSource should be left blank, as we'll populate the items in the combo box dynamically.
 

On a rainy afternoon, you could try playing about with the ColumnHeads, ListRows and ListStyle properties to see how they affect how the combo or list box looks.

Writing Code to Populate the Combo or List Box

Having created a combo (or list) box and set its main properties as above, you can now use ADO code to get the underlying records and add them into the list of possible values.  Here's an example:

Private Sub UserForm_Initialize()

'=============================

'ADO code to get at data

'=============================

'create a new connection string

Dim cn As New ADODB.Connection

'say where the connection string is pointing to, and open connection

cn.ConnectionString = "driver={SQL Server};" & _

"Server=ANDYB\SQL2008R2;Database=Movies;Trusted_Connection=True;"

cn.Open

'create a new recordset

Dim rs As New ADODB.Recordset

'open a recordset of table of people

rs.Open "vwPerson", cn

'=============================

'populate values in combo box

'=============================

'first clear any existing people's names

cmbPerson.Clear

'add in all of the people one by one

Dim NumPeople As Integer

NumPeople = 0

Do Until rs.EOF

'for each record in table, increment number of people

NumPeople = NumPeople + 1

'add a new item to list

cmbPerson.AddItem

'set the value for first column to be person's name

cmbPerson.List(NumPeople - 1, 0) = rs("PersonName")

'set the value for second (hidden) columb to be person's id

cmbPerson.List(NumPeople - 1, 1) = rs("PersonId")

'go on to the next person

rs.MoveNext

Loop

rs.Close

cn.Close

End Sub

Notice that you add each item to the list, and then set the values for the item's first and second columns (ie the person's name and id). 

 

ADVANCED CONTROLS IN USERFORMS WITH VBA MACROS

Part four 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
  3. Combo boxes (and list boxes)
  4. Multiple Column Combo Boxes and Listboxes (this article)
  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.