AS
VBA per Access
Esportazione di una query in Excel
Una della tipiche funzionalità per un utilizzo integrato di Microsoft Access ed Excel è l'esportazione delle query in modo da aggiornare fogli esistenti (i quali magari rappresentano a loro volta le basi dati di tabelle pivot con aggiornamento automatico).
Ecco il codice:
Function EsportaExcel()
Dim myPath As String
Dim myFile As String
myPath = Application.CurrentProject.Path
myFile = myPath & "\nomefile.xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "myQuery", myFile, , "mySheet"
End Function
Qui sotto l'esportazione di più file.
In questo caso passiamo un parametro alla query da esportare attraverso il campo della maschera start.
Ovvero, la query avrà un campo la cui condizione è "=Forms!myForm!someField".
Public Sub esporta_excel()
On Error GoTo Err
Dim myPath As String
Dim myFile As String
myPath = Application.CurrentProject.Path
Dim myArr As Variant
Dim myElement As String
Dim myIndex As Integer
Dim myDimension As Integer
myArr = Array("Value1", "Value2", [...])
myDimension = UBound(myArr)
myIndex = 0
While myCounter <= myDimension
myElement = myArr(myIndex)
myFile = myPath & "\" & myElement & "_fileName.xlsx"
Forms!myForm!someField.Value = myElement
Forms!myForm.Refresh
Forms!myForm!someField.SetFocus
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "queryName", myFile, , "sheetName"
myIndex = myIndex + 1
Wend
MsgBox "Ok!"
Exit Sub
Err:
MsgBox Err.Description
Exit Sub
End Sub
Qui sotto il link per scaricare un database di esempio.
Scaricando il seguente file accettate che venga rilasciato così come è, senza alcun tipo di garanzia.