Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate a ComboBox with a Recordset using VBA

There is some literature available at expert's exchange and at teck republic about using the combobox.recordset property to populate a combobox in an Access form.

These controls are usually populated with a "SELECT *" string in the 'rowsource' properties of the control, referencing a table or query available on the client's side of the app. When I need to display server's side data in a combobox, I create a temporary local table and import requested records. This is time consuming, specially with large tables.

Being able to use a recordset to populate a combobox control would allow the user to directly display data from the server's side.

Inspired by the 2 previous examples, I wrote some code as follow:

Dim rsPersonne as ADODB.recordset
Set rsPersonne = New ADODB.Recordset

Set rsPersonne.ActiveConnection = connexionActive
rsPersonne.CursorType = adOpenDynamic
rsPersonne.LockType = adLockPessimistic
rsPersonne.CursorLocation = adUseClient

rsPersonne.Open "SELECT id_Personne, nomPersonne FROM Tbl_Personne"

fc().Controls("id_Personne").Recordset = rsPersonne

Where:

  • connexionActive: is my permanent ADO connection to my database server
  • fc(): is my current/active form
  • controls("id_Personne"): is the combobox control to populate with company's staff list
  • Access version in 2003

Unfortunately, it doesn't work!

In debug mode, I am able to check that the recordset is properly created, with requested columns and data, and properly associated to the combobox control. Unfortunately, when I display the form, I keep getting an empty combobox, with no records in it! Any help is highly appreciated.

EDIT:

This recordset property is indeed available for the specific combobox object, not for the standard control object, and I was very surprised to discover it a few days ago. I have already tried to use combobox's callback function, or to populate a list with the "addItem" method of the combobox,. All of these are time consuming.

like image 389
Philippe Grondier Avatar asked Jun 08 '09 22:06

Philippe Grondier


People also ask

How do I add data to a ComboBox in VBA?

Go to the VBA window (Alt + F11) > double-click the UserForm from the Project window (Ctrl + R if it's not visible) and then, once the form is visible, click the ComboBox that you want to fill with values. Look to the Properties window and scroll down to RowSource. If the Property window isn't visible, hit F4.

How do I link a cell to a ComboBox in Excel VBA?

Format a Form Control combo boxRight-click the combo box and pick Format Control. Input range: Type the range of cells containing the list of items. Cell link: The combo box can be linked to a cell where the item number is displayed when you select an item from the list.


1 Answers

As was said, you have to get the RowSourceType to "Table/List" (or "Table/Requête" if in french) in order to show query results in the combobox.

Your memory problems arise from opening the recordset (rsPersonne) without closing it. You should close them when closing/unloading the form (but then again you would have scope problems since the recordset is declared in the function and not in the form).

You could also try to create and save a query with Access's built-in query creator and plug that same query in the RowSource of your combobox. That way the query is validated and compiled within Access.

like image 54
Marcand Avatar answered Oct 21 '22 14:10

Marcand