List and combo boxes offer various properties and methods that are accessible only through VBA code. The next two sections describe programming techniques that take advantage of additional list and combo box features. Iterating List Box Items and Selecting an ItemAccess list boxes share many common properties with the native ListBox control of Visual Basic 5.0 and earlier. The ListCount property returns the number of items in the list, the ItemData or Column property returns a value from the list, and the Selected property sets or returns whether the row is selected. This example emphasizes a product in the Line Items list box by automatically selecting the row corresponding to the cboProduct selection. The Column property is more versatile than the ItemData property; ItemData is restricted to values in the bound column. Follow these steps to add the code required to automatically select a product in the lstLineItems list box:
Figure 29.12. Adding a few lines of VBA code automatically highlights the Order Details record for the selected product in the Line Items list.
Tip You can use code similar to what's in this example to emulate a SELECT query against the content of any list box or combo box. Selecting a list box item ensures that the item is visible in the text box, regardless of its location in the list. Adding an Option to Select All Countries or ProductsIt's often useful to give users the option to pick all items represented by combo box selections. In this chapter's example, selecting all countries or all products (but not both) represents an enhancement to the application. How you add an "(All)" choice to cboCountry and cboProduct and write the code for the appropriate SELECT query to fill lstOrders isn't obvious, at best.
A UNION query is the most straightforward way to add custom rows to a combo or list box populated by an SQL statement. You specify your own values for each column returned by the SELECT query to which the UNION clause applies. The Jet UNION query to populate cboCountry is as follows: SELECT Country FROM Customers UNION SELECT '(All)' FROM Customers ORDER BY Country; You don't need the DISTINCT modifier of the original SELECT statement because UNION queries don't return duplicate rows. The '(All)' custom item is surrounded with parentheses because ( sorts before numerals and letters, making (All) the first item in the list. The Customers table has no (All) record, but all UNION queries require a FROM TableName clause.
For UNION query syntax, see "Using UNION Queries and Subqueries," p. 452. Similarly, the UNION query to fill cboProduct is as follows: SELECT ProductID, ProductName FROM Products UNION SELECT 0, '(All)' FROM Products ORDER BY ProductName; Here you must supply a numeric value in this case 0 for the first column of the query (ProductID) and the '(All)' string value for the second column (ProductName). UNION queries require that both SELECT statements return the same number of columns, and all rows of each column must be of the same field data type. ProductID is an AutoNumber field, which starts with 1 unless you make the effort to begin autonumbering with a higher value. In addition to adding the (All) item to the combo boxes, you must alter your SELECT queries to populate lstOrders when you select (All). In the all-countries case, the Jet SELECT query is as follows: SELECT CompanyName, OrderID, ShippedDate FROM qryCombo1 WHERE ProductID = cboProduct.Value ORDER by ShippedDate DESC; For the all-products situation, the Jet query is the following: SELECT CompanyName, OrderID, ShippedDate FROM qryCombo1 WHERE Country = cboCountry.Value ORDER by ShippedDate DESC; The preceding changes require you to add logic to detect when you select (All) and change the assembly of the SQL statement to suit. The following steps add the (All) selection to both combo boxes:
|