VBA, MS Access MS Access в примерах

MS Excel - Экспорт данных двух запросов в два разных листа

Private Sub cmdTest01_Click()
Dim exApp As Object, rs As Object, qdf As DAO.QueryDef
Dim i%
Dim objWbk As Object
Dim objWsh As Object

    Set exApp = CreateObject("Excel.application")
    Set objWbk = exApp.Workbooks.Add
    exApp.Visible = True
   
'Первый лист 1
    Set objWsh = objWbk.Worksheets(1)
 
    Set qdf = CurrentDb.QueryDefs("Query_1")
    Set rs = qdf.OpenRecordset
 
    For i = 0 To rs.Fields.Count - 1
        objWsh.cells(1, i + 1) = rs.Fields(i).Name
    Next
    objWsh.Range("A2").CopyFromRecordset rs
 
 
'Следующий лист 2
    Set objWsh = objWbk.Worksheets(2)
    Set qdf = CurrentDb.QueryDefs("Query_2")
    Set rs = qdf.OpenRecordset
    
    For i = 0 To rs.Fields.Count - 1
        objWsh.cells(1, i + 1) = rs.Fields(i).Name
    Next
    objWsh.Range("A2").CopyFromRecordset rs
 
 
Set objWsh = Nothing
Set objWbk = Nothing
Set exApp = Nothing
Set rs = Nothing
End Sub


Назад ToTop
L.E. 28.01.2020
Рейтинг@Mail.ru