Hey there! I'm new around here, so I ask for your patience! I tried reading other threads, but none helped me.
My case is: I have lots of tables from wich i need to export data to Excel. These tables countain historical data about pluviometry measured monthly. I biult queryDefs like:
/*Obs.: in portuguese Data is the english Date. and the english Data is Dado. And Data is a field of my tables. I think you already got this, but it's important to make things clear! hehehe */
Those querys get the data i need monthly. I want to create a single .xlsx for each table i'm querying. Inside these .xlsx I want to have a sheet for each month.
I tried lots of ways to do that, like using recordsets, the methods docmd.outputto() and docmd.transferspreadsheet(). But none worked just right. I'm really struggling and in need of help. So, if you please...!
Rigght now i'm facing the TransferSpreadsheet monster. It does not exports the data i need to the existing excel workbook. It creates another workbook named codEstacao & " FEVEREIRO". Here it its my full vba code:
My case is: I have lots of tables from wich i need to export data to Excel. These tables countain historical data about pluviometry measured monthly. I biult queryDefs like:
Code:
Set objConsulta = CurrentDb.CreateQueryDef(codEstacao & " FEVEREIRO", "SELECT Data,Total FROM " & objTabela.Name & " WHERE Month(Data) = 2")
Those querys get the data i need monthly. I want to create a single .xlsx for each table i'm querying. Inside these .xlsx I want to have a sheet for each month.
I tried lots of ways to do that, like using recordsets, the methods docmd.outputto() and docmd.transferspreadsheet(). But none worked just right. I'm really struggling and in need of help. So, if you please...!
Rigght now i'm facing the TransferSpreadsheet monster. It does not exports the data i need to the existing excel workbook. It creates another workbook named codEstacao & " FEVEREIRO". Here it its my full vba code:
Code:
Public Function criaNome() As String
'Excel objects
Dim objXL As Excel.Application
Dim objWbk As Excel.Workbook
'Miscelaneous objects
Dim objTabela As AccessObject
Dim objConsulta As QueryDef
Dim rcdSet As Recordset
Dim fldLoop As Field
Dim strSQL As String
Dim strCaminho As String
For Each objTabela In Application.CurrentData.AllTables
If objTabela.Name = "Chuvas" Then
strSQL = "SELECT DISTINCT EstacaoCodigo FROM " & objTabela.Name
Set rcdSet = CurrentDb.OpenRecordset(strSQL)
For Each fldLoop In rcdSet.Fields
codEstacao = fldLoop.Value
Next fldLoop
strCaminho = CurrentProject.Path & "\" & codEstacao '& ".xlsx"
Set objXL = New Excel.Application
objXL.SheetsInNewWorkbook = 1
objXL.Visible = True
If testaExistenciaArquivo(strCaminho) Then
Set objWbk = objXL.Workbooks.Open(strCaminho)
objWbk.Save
Else
Set objWbk = objXL.Workbooks.Add
objWbk.SaveAs FileName:=strCaminho, ReadOnlyRecommended:=False
End If
'JANEIRO
Set objConsulta = CurrentDb.CreateQueryDef(codEstacao & " JANEIRO", "SELECT Data,Total FROM " & objTabela.Name & " WHERE Month(Data) = 1")
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, objConsulta.Name, strCaminho & "!" & codEstacao & " JANEIRO" & ".xlsx"
'FEVEREIRO
Set objConsulta = CurrentDb.CreateQueryDef(codEstacao & " FEVEREIRO", "SELECT Data,Total FROM " & objTabela.Name & " WHERE Month(Data) = 2")
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, objConsulta.Name, strCaminho & "!" & codEstacao & " FEVEREIRO" & ".xlsx"
End If
Next objTabela
End Function