ListBox Object (Access)

This object corresponds to a list box control. The list box control displays a list of values or alternatives.

Remarks

In many cases, it's quicker and easier to select a value from a list than to remember a value to type. A list of choices also helps ensure that the value that's entered in a field is correct.

Control:

Tool:

Ff195480.t-lstbox_ZA06053984(en-us,office.14).gif

Ff195480.listbox_ZA06044481(en-us,office.14).gif

Note

The list in a list box consists of rows of data. Rows can have one or more columns, which can appear with or without headings.

Ff195480.cfrmlst2_ZA06047456(en-us,office.14).gif

If a multiple-column list box is bound, Microsoft Access stores the values from one of the columns.

You can use an unbound list box to store a value that you can use with another control. For example, you could use an unbound list box to limit the values in another list box or in a custom dialog box. You could also use an unbound list box to find a record based on the value you select in the list box.

If you don't have room on your form to display a list box, or if you want to be able to type new values as well as select values from a list, use a combo box instead of a list box.

Links provided by: Community Member Icon The UtterAccess community | About the Contributors

Example

This example demonstrates how to filter the contents of a list box while you are typing in a text box.

In this example, a list box named ColorID displays a list of colors stored in the Colors table. As you type in the FilterBy text box, the items in ColorID are filtered dynamically

To do this, use the Change event of the text box to build a SQL statement that will serve as the new RowSource of the list box.

Sample code provided by: Community Member Icon The UtterAccess community | About the Contributors

Private Sub FilterBy_Change()

    Dim sql As String
    
    'This will match any entry in the list that begins with what the user 
    ‘has typed in the FilterBy control
    sql = "SELECT ColorID, ColorName FROM Colors WHERE ColorName Like '" & Me.FilterBy.Text & "*' ORDER BY ColorName"
    
    'If you want to match any part of the string then add wildcard (*) before
    ‘the FilterBy.Text, too:
    'sql = "SELECT ColorID, ColorName FROM Colors WHERE ColorName Like '*" & Me.FilterBy.Text & "*' ORDER BY ColorName"
    
    Me.ColorID.RowSource = sql
    
End Sub

About the Contributors

UtterAccess is the premier Microsoft Access wiki and help forum. Click here to join.

See Also

Concepts

ListBox Object Members

Access Object Model Reference