0

i have a very complex query that is running from a listbox rowsource. i just do a listbox1.requery and it populates the listbox.

instead of doing it this way, i would like to:

  1. i just want to save the query in the queries section
  2. call it from there.
  3. then i want to save the results of the query into a string
  4. then i want to feed the string into the listbox

can you please help me with the code for these four questions.

thanks!

3 Answers 3

3

another solution is to open the query in a recordset and then set the recordset property of the listbox control to it. I have my own function for that (I use it mostly for comboboxes). If necessary, you can add an extra 'connection' parameter to the sub when you want to open a recordset from another database.

Public Sub addQueryToCombobox(x_query As String, x_control As Control)
Dim rs As ADODB.Recordset

On Error GoTo ERREUR

Set rs = New ADODB.Recordset

Set rs.ActiveConnection = CurrentProject.AccessConnection

rs.CursorType = adOpenStatic
rs.LockType = adLockReadOnly
rs.CursorLocation = adUseClient

rs.Open x_Query

Set rs.ActiveConnection = Nothing

Set x_control.Recordset = rs

Set rs = Nothing

On Error GoTo 0
Exit Sub

ERREUR:
'add here your own error manager'
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

I think your first 3 items are addressed by this answer to your other question:

ms-access save query result in a string

As for the fourth item in this question, set your list box Row Source Type to "Value List" and write your string to its Row Source property.

1 Comment

But keep in mind that there's a limit on the length of the string that can be assigned to a value list. I forget the actual length in different versions (I believe it's much longer than it used to be), but if your list is going to be of any length, you might want to use a call-back function instead.
0

you could just set the rowsource of the listbox to your query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.