The information in this article applies to:
SUMMARY
Moderate: Requires basic macro, coding, and interoperability skills.
MORE INFORMATIONMicrosoft Access Version 2.0In Microsoft Access version 2.0, you can use the ItemData method to cause a list box or combo box to default to any row. To have the first row selected automatically, set the box's DefaultValue property as follows:
NOTE: When the field names in a combo box are used as column headings the syntax would be as follows: =[<MyCombo>].[ItemData](1) Microsoft Access Version 1.xThe first method below uses a user-defined Access Basic function, and the second method uses the built-in DLookUp() function to display the first value in the list automatically.Method 1: The following example demonstrates a sample Access Basic function called GetFirst() that can be used to find the first item in the underlying table or query. The function's result can be used by the DefaultValue property to automatically select the first item in the list. To create the GetFirst() function, add the following lines to a new or existing module:
Note that the first argument of the GetFirst() function is the name of the field that is used as the BoundColumn property for the combo box. The second argument is the name of the table or query specified in the RowSource property of the combo box. The following example demonstrates how to use the GetFirst() function to automatically select the first employee in the Salesperson combo box on the Orders form in the sample database NWIND.MDB:
Differences Between GetFirst() and DFirst(): The GetFirst() function is similar to the built-in DFirst() aggregate (totals) function. However, DFirst() may return unexpected results when used to find the first item in a list. If the underlying table or query is indexed, the value returned by DFirst() will be the first indexed record. Otherwise, DFirst() will return items in the actual order in which they were entered in the database. Therefore, if the RowSource property of a combo box is a query that sorts the data by a non-indexed field, DFirst() may not return the expected value. For example, if you change the DefaultValue property of the Salesperson combo box on the Orders form to
the item returned will be "Davolio, Nancy," which is not the first item in the combo box, but the first indexed item in the Employees table. Method 2: This method uses the DLookUp() function to look up the first record in the list. The expression will be the field referred to in the BoundColumn property (or the ControlSource property) of the combo box or list box. The domain will be the same table or query that the combo box or list box uses as its RowSource property. The optional criteria will not be used so that the DLookUp() function will return the first record. The following example demonstrates how to use DLookUp() to automatically select the first employee in the Salesperson combo box on the Orders form in the sample database NWIND.MDB:
REFERENCES
For more information about the ItemData method, search for "ItemData," and
then "ItemData Method" using the Microsoft Access Help menu.
Additional query words: listbox combobox
Keywords : kbusage FmsCmbo |
Last Reviewed: March 27, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |