Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


New Query in VBA

Posted on 2004-04-17
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 11

Accepted Solution

BillPowell earned 320 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

688 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