I have created an access 2010 form where I have a listbox and two command buttons. Listbox includes all of the query names and command button one is for "query print preview" and the command button 2 is for "opening the query" which should be same as "double clicking the query name" in the listbox. So, how do I make these buttons and queries to open when double clicked in the list work?
Asked
Active
Viewed 6,009 times
0
-
So I can use OpenQuery action, but this will open only one query no matter which query I select. However I want to be able to open any query from the list box when I double click them. Also I should be able to select a query and click a command button called show- and it should do the same action as double clicking the query. I don't know how to use macros for this action. – makalele Jul 15 '12 at 00:36
1 Answers
2
Edit: I've updated my answer with the generic code you would need to open a query via the button or the Listbox. This assumes that the values in the Listbox are valid query names within your database.
This is easily done with a little VBA.
Option Explicit
Private Sub List_DblClick(Cancel As Integer)
Call Show_Click
End Sub
Private Sub Show_Click()
DoCmd.OpenQuery Me.List.Value
End Sub
This assumes that your listbox is called List. And your command button is called Show.
Basically, the code you want run in the button's Click event and call that sub from the list box's DblClick event.

Daniel
- 12,982
- 3
- 36
- 60
-
I am probably doing something wrong. Is this code supposed to show the query I double click from the list? – makalele Jul 15 '12 at 01:16
-
Only if you are using VBA in the Show button and the Show button does what it is supposed to do. I didn't realize we would need to provide that to you as well. – Daniel Jul 15 '12 at 01:23
-
uhm.. So simply I need to be able to access any query I select from the listbox either by doubleclicking on them or chosing and then clicking show button. – makalele Jul 15 '12 at 01:28
-
I updated my answer with sample code for the Show button that would open a query based upon the value of your list box. – Daniel Jul 15 '12 at 05:12
-
Oh thank you very much, this perfectly solved my question. I am just a beginner in VBA and Access, but this solution will definitely effect my learning curve. I used acViewPreview for my print preview button too. – makalele Jul 15 '12 at 19:14