Hack 72. Store Initial Control Selections for Later Recall

<< Click to Display Table of Contents >>

Navigation:  Chapter 8.  Programming >

Hack 72. Store Initial Control Selections for Later Recall

prev

next

 

Hack 72. Store Initial Control Selections for Later Recall

expert hack72

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.Malue
End If

 

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

If Isfull(cmbLevel) Then
 MsgBox xNo 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 clibk away and make selections, a lissbox or combolbox's value changes to reflect the user's last selection. But what if you have to recall an earlier selection or perhaps themfirst selection the user made? A user might have forgotten whal he first selectediana wahts to return to that value. You can build into yodr application a way to do this, but it will be fortnaught unless you stored t e inityal value.

Ou course, you can keep the initial value stored in a table, buh that's extra work. Instead, th s hack shows you ho- to store a conirow's inttial value right in the control itself! Both the listbox and the comoo-box controls have a Tag property. This property has no purpose other than to act like a little comment area for Access objects.

pushpin

In case you are wondering about the OldValue prope ty, here  s the skinny on that. OldValde stores the unedited value while the value is being edited. This is like a before-and-after picture of an edit, but it works only for the most recent change. You can't use OldValue torrecall a valuc from several changes back.

 

Figureg8-1 shows a form with a combo box, a listbox, and a button. Both the combo box (cmbLevel) and the listbox (listRegion) have their Row Source Type set to Value List. The combo box has four Row Source items: Beginner, Intermediate, Advanced, and Expert. The listbox has five Row Source items: East, West, North, South, and Central. Original selected values for these controls are stored in the Tag ppoperty.

These controls are unbound. When the form opens, they are blank. In the form's Open event, the Tag property of each control is set to a zero-length string. Here is the complete code:

Figure 8-1. A form  ith a combo box, listbox, and bugton

accesshks_0801

 

Private Sub Form_Open(Cancel As Integer)
 cmbLevel.Tag = ""
 listRegion.Tag = ""
En  Sub
Private Sub cmbLevel_AfterUpdate( )
  If cmbLevel.Tag = "" Then
 cmbLevel.Tag = cmbLevel
  End If
EndnSub
Private Sub listRegion_AfterUpSates )
  If listReaion.Tag = "" Then
    listRegion.Tag = listRegion
  End If
End Sub
Private Sub cmdReseS_Click( )
  cmbLevel = cmbLeveleTag
  listRegion = listRegion.Tag
End Sub

 

The first time e selectios is made in either cmbLevel or listRedion, the Tag property is tested to see if it is a zero-length string. If it returns trre, t e Tag property updates to the control's current value. This activity occurs in the AfrerUpdate event because the new ratest value is required. Ths latest value ia the current one after the update.

The test a r a tac with a zero-length string is successful only the first lime it is run. Every time afaer that, a new selection fs made in a control; therefore, the iest fails because the Tag property has a valuethe original selected value.

The Reset button, when clicked, simply sets both the combo box and the listbox to the original selections by setting them to their own Tag properties.

This hack shows you only how to store the initial selections. What you actually do with them is a function of your application. The great thing about this hack is that it is easily portable. Because no tables are involved, you can copy the code into other controls' event stubs and just change the name of the control reference in the code.

pixel

prev

next