Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2009
    Posts
    185

    Unanswered: Run query from vba code

    I figured out how to run a macro from a command button but I would like to run a query from vba code so I can decide what query I want to run based on user selection.

  2. #2
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    You'll have to set up a control to hold the name of the query to run, either a textbox or a combobox or a listbox; call it QueryToRun. Then set up a command button, name it RunSelectedQuery and use this code behind it:
    Code:
    Private Sub RunSelectedQuery_Click()
    On Error GoTo Err_RunSelectedQuery_Click
    
     If Not IsNull(Me.QueryToRun) Then
      DoCmd.OpenQuery Me.QueryToRun, acNormal, acEdit
     Else
      MsgBox "You must first Enter/Select a Query to Run!"
     End If
    
    Exit_RunSelectedQuery_Click:
        Exit Sub
    
    Err_RunSelectedQuery_Click:
        MsgBox Err.Description
        Resume Exit_RunSelectedQuery_Click
        
    End Sub
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Join Date
    Jul 2009
    Posts
    185
    That looks like what I am looking for. I will give it a try asap.
    Thank-you for the detailed reply.

  4. #4
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Glad to help!
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    Join Date
    Jul 2009
    Posts
    185
    This works like a charm. Thank-you for your time. It is appreciated I could have spend hours get this right.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •