Programmatically Add Items to a List or Combo Box
Problem
Getting items into a list or combo box from a data source is elementary in Access. Sometimes, though, you need to put into a list box things that you don’t have stored in a table. In Visual Basic and other implementations of VBA, this is simple: you just use the AddItem method. But Access list boxes don’t support this method. How can you add to a list box items that aren’t stored in a table?
Solution
It’s true: Access list boxes (and combo boxes) don’t support the AddItem method that Visual Basic programmers are used to using. To make it easy for you to get bound data into list and combo boxes, the Access developers had to forego a simple method for loading unbound data. To get around this limitation, there are two methods you can use to place data into an Access list or combo box: you can programmatically build the RowSource string yourself, or you can call a list-filling callback function. Providing the RowSource string is easy, but it works in only the simplest of situations. A callback function, though, will work in any situation. This solution demonstrates both methods.
One important question, of course, is why you would ever need either of these methods for filling your list or combo box. You can always pull data from a table, query, or SQL expression directly into the control, so why bother with all this work? The answer is simple. Sometimes you don’t know ahead of time what data you’re going to need, and the data’s ...
Get Access Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.