title: Copy Query from Access to Excel
body: <p>I'm having some problems when I try to import a Query from Access to Excel. </p>
  
  <p>Some days ago I programmed a code (with some help of Google haha) to import a Table from Access to Excel:</p>
  
  <pre><code>Sub importQuery(DBFullName As String, data_sht As Worksheet)
  
       Dim cn As Object, rs As Object
  
       Dim i As Integer
  
       Dim TargetRange As Range
  
       Dim rows As Long, cols As Long
  
       Dim dataEmpty As Boolean
  
       Dim lastColString As String
  
  
  
       data_sht.Activate   
  
       Application.ScreenUpdating = False
  
       Set TargetRange = data_sht.Range("A1")
  
       Set cn = CreateObject("ADODB.Connection")
  
       cn.Open "Provider=Microsoft.Ace.OLEDB.12.0; Data Source=" &amp; DBFullName &amp; ";" 'the Access file is .accdb
  
  
       Set rs = CreateObject("ADODB.Recordset")
  
       rs.Open "SELECT * FROM C_Paso2_SM_Cuplas", cn, , , adCmdUnspecified
  
       cols = rs.Fields.Count
  
       rows = data_sht.Range("A" &amp; data_sht.rows.Count).End(xlUp).Row 
  
       ' Copy titles of the Access Query
  
       For i = 0 To (cols - 1)
  
           TargetRange.Offset(0, i).Value = rs.Fields(i).Name
  
       Next
  
       ' Copy data
  
       TargetRange.Offset(1, 0).CopyFromRecordset rs
  End Sub
  </code></pre>
  
  <p>That code works but when I do this: </p>
  
  <pre><code>rs.Open "SELECT * FROM C_Paso2_SM_Cuplas", cn, , , adCmdUnspecified
  </code></pre>
  
  <p>I'm importing another Query called C_Paso<strong>1</strong>_SM_Cuplas, from the same file. What can I do? Why am I importing C_Paso<strong>1</strong>_SM_Cuplas when I say C_Paso<strong>2</strong>_SM_Cuplas? Is there other possibility to import an Access Query to Excel?</p>