New Query in VBA

Posted on 2004-04-17
Last Modified: 2008-03-06
I'm just wanting to know if there is a way to open a new query or report from vba.  So if a user clicks an add new query button, it opens a new query window.  It would be even better if the add new tables dialog was open as well.

Question by:Drizzt95
  • 3
  • 2
LVL 11

Accepted Solution

BillPowell earned 80 total points
ID: 10851824
Hi Drizzt,

Heres on way to do it.

1. Create a textbox on your form for users to type in the name of the query they will create.
2. In the onclick event for the command button:

Private Sub cmdQuery_Click()
    CurrentDb.CreateQueryDef Me.txtQueryName
    DoCmd.OpenQuery Me.txtQueryName, acViewDesign
    DoCmd.RunCommand acCmdShowTable
End Sub

This creates a querydef in your database with the name from the textbox and then has it open up with the blank query with the tables dialog so they can begin their query.


Author Comment

ID: 10852726
That works great, what about adding a report?
LVL 11

Expert Comment

ID: 10853709
Here ya go.  Somewhat different than the query.  No name required in the textbox, just creates the blank report.  When you go to close it, it will prompt you for a name.

Private Sub cmdReport_Click()
    Dim rpt As Report
    Set rpt = CreateReport
End Sub

Author Comment

ID: 10855436
Thanks for your help.  I looked everywhere on the net but couldn't find an answer to the report part.
I'll throw in a few extra points for answering a two part question.

Thanks again
LVL 11

Expert Comment

ID: 10855766
No problem, glad it worked out :)

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now