Hack #72. Store Initial Control Selections for Later Recall

The Tag property is a great place to store data about controls. Here's how to put it to good use.

Combo boxes and listboxes are great for presenting users with choices. As the user makes selections in a combo box or listbox, the value is available to use in code simply by referring to the control's Value property:

If IsNull(cmbLevel) Then
  MsgBox "No Value Selected"
Else
  MsgBox cmbLevel.Value
End If

You can even refer only to the control itself and leave the Value property off, like this:

If IsNull(cmbLevel) Then
 MsgBox "No Value Selected"
Else
  MsgBox cmbLevel
End If

Note that both code snippets begin with a test for a null. The listbox or combo box might initially be null, so it is good practice to include a way to avoid bombing out if this is the case.

As users click away and make selections, a listbox or combo box's value changes to reflect the user's last selection. But what if you have to recall an earlier selection or perhaps the first selection the user made? A user might have forgotten what he first selected and wants to return to that value. You can build into your application a way to do this, but it will be for naught unless you stored the initial value.

Of course, you can keep the initial value stored in a table, but that's extra work. Instead, this hack shows you how to store a control's initial value— right in the control itself! Both the listbox and the combo-box controls have a Tag property. This property ...

Get Access Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.