Access

Esporta Query in Excel

by Andrea Spera

Una della tipiche funzionalità per un utilizzo integrato di Microsoft Access ed Excel è l'esportazione delle query.
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
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".
Codice
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