Listbox selection to execute a query

  • Thread starter Thread starter Ixtreme
  • Start date Start date
I

Ixtreme

Hi,

I have a listbox that contains a list of all queries I have created.
The rowsource of the listbox control =
SELECT MSysObjects.Name
FROM MsysObjects
WHERE (Left$([Name],1)<>"~") AND
(MSysObjects.Type)=5
ORDER BY MSysObjects.Name;

What I want is to execute the query from the list if the user
doubleclicks on it or clicks a button (Go!) on the same form. Somehow
I can't find an answer to this simple question.
 
Ixtreme said:
Hi,

I have a listbox that contains a list of all queries I have created.
The rowsource of the listbox control =
SELECT MSysObjects.Name
FROM MsysObjects
WHERE (Left$([Name],1)<>"~") AND
(MSysObjects.Type)=5
ORDER BY MSysObjects.Name;

What I want is to execute the query from the list if the user
doubleclicks on it or clicks a button (Go!) on the same form. Somehow
I can't find an answer to this simple question.


You can do something like this:

'----- start of code -----
Sub RunSelectedQuery()

With Me!lstYourListbox
If IsNull(.Value) Then
MsgBox "Please select a query to run."
Else
DoCmd.OpenQuery .Value
End If
End With

End Sub

Private Sub lstYourListbox_DblClick(Cancel As Integer)

RunSelectedQuery

End Sub

Private Sub cmdGo_Click(Cancel As Integer)

RunSelectedQuery

End Sub
'----- end of code -----

In the above example code, I've assumed the listbox is named
"lstYourListbox" and the button is named "cmdGo". You'll want to correct
those names, of course.
 
Back
Top