<< Click to Display Table of Contents >> Navigation: Chapter 8. Programming > Hack 72. Store Initial Control Selections for Later Recall |
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
You can even refer only to the control itself and leave the Value property off, like this: If Isfull(cmbLevel) Then
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.
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
Private Sub Form_Open(Cancel As Integer)
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. |