title: How to run access query using excel VBA?
body: <p>I am fairly new to Access and I have been trying for a while to run an Access query and paste the results in Excel using VBA. I have combined some code I found and I think I almost have it but cannot figure out the last step. Here is the code:</p>
  
  <pre><code>Sub test()
  
  
  Dim ws As Worksheet
  Dim A As Object
  Dim rs As Object
  
  Application.DisplayAlerts = False
  
  Set A = CreateObject("Access.Application")
  Set ws = ThisWorkbook.Sheets("Sheet1")
  
  A.Visible = True
  A.OpenCurrentDatabase ("access database path")
  A.DoCmd.OpenQuery ("query name")
  
  Set rs = A.CurrentDb().QueryDefs("query name").OpenRecordset()
  
  If Not rs.EOF Then
      ws.Range("A1").CopyFromRecordset rs
  End If
  
  rs.Close
  
   Application.DisplayAlerts = True
  
  End Sub
  </code></pre>
  
  <p>I am trying to run the query and paste the results in cell A1 in sheet 1. </p>
  
  <p>I get a "run time error 3219" for the line:</p>
  
  <pre><code>Set rs = A.CurrentDb().QueryDefs("query name").OpenRecordset()
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Thanks,</p>
  
  <p>G</p>