Code:
Option Compare Database
Option Explicit
Private Sub EstraiExcel_Click()
Dim db As DAO.Database
Dim rs As DAO.recordset
Dim table_name As String
Dim Record As String, strSQL As String
table_name = "Estrazione"
Dim excel_application As Excel.Application
Dim workbook As Excel.workbook
Dim sheet As Excel.Worksheet
Dim excel_file_name As String
Dim sheet_name As String
Dim Corso As String
Dim Ed As Integer
Dim ETICHETTA As String
Dim NOME As String
Dim COGNOME As String
Dim NUM As Integer
Dim Armatore As String
Dim DAL As Date
Dim AL As Date
Dim TOT As Integer
Dim TOTALE As Integer
excel_file_name = "E:\Documenti\Access\db\Estrazione_imbarchi_per_corso.xlsx"
Set db = CurrentDb
strSQL = "FROM (Imbarchi_anagrafica INNER JOIN Imbarchi_stage ON Imbarchi_anagrafica.ID_imbarchi_anagrafica = Imbarchi_stage.ID_imbarchi_anagrafica) INNER JOIN Imbarchi_corso ON Imbarchi_anagrafica.ID_imbarchi_anagrafica = Imbarchi_corso.ID_imbarchi_anagrafica" & _
"WHERE (((Imbarchi_corso.Corso) = [Digita il corso]) And ((Imbarchi_corso.Ed) = [Digita l'edizione]) And ((Imbarchi_corso.ETICHETTA) = [Digita l'etichetta]))" & _
"ORDER BY Imbarchi_anagrafica.Cognome, Imbarchi_stage.N;"
Set rs = db.OpenRecordset("select * from imbarchi_estrpercorso WHERE Imbarchi_anagrafica_ID_imbarchi_anagrafica=" & Me.Imbarchi_anagrafica_ID_imbarchi_anagrafica & "Imbarchi_corso.Corso=" & Me!Corso & "Imbarchi_corso.Ed=" & Me!Ed & "Imbarchi_corso.ETICHETTA =" & Me!ETICHETTA)
Set excel_application = New Excel.Application
Set workbook = excel_application.Workbooks.Add
Set sheet = workbook.Sheets.Add
Dim rowIndex As Integer
sheet.Cells(1, 1) = "CORSO"
sheet.Cells(1, 1).Font.Bold = True
sheet.Cells(1, 2) = "EDIZIONE"
sheet.Cells(1, 2).Font.Bold = True
sheet.Cells(1, 3) = "ETICHETTA"
sheet.Cells(1, 3).Font.Bold = True
sheet.Cells(1, 4) = "NOME"
sheet.Cells(1, 4).Font.Bold = True
sheet.Cells(1, 5) = "COGNOME"
sheet.Cells(1, 5).Font.Bold = True
sheet.Cells(1, 6) = "N."
sheet.Cells(1, 6).Font.Bold = True
sheet.Cells(1, 7) = "ARMATORE"
sheet.Cells(1, 7).Font.Bold = True
sheet.Cells(1, 8) = "DAL"
sheet.Cells(1, 8).Font.Bold = True
sheet.Cells(1, 9) = "AL"
sheet.Cells(1, 9).Font.Bold = True
sheet.Cells(1, 10) = "TOT"
sheet.Cells(1, 10).Font.Bold = True
sheet.Cells(1, 11) = "N."
sheet.Cells(1, 11).Font.Bold = True
sheet.Cells(1, 12) = "ARMATORE"
sheet.Cells(1, 12).Font.Bold = True
sheet.Cells(1, 13) = "DAL"
sheet.Cells(1, 13).Font.Bold = True
sheet.Cells(1, 14) = "AL"
sheet.Cells(1, 14).Font.Bold = True
sheet.Cells(1, 15) = "TOT"
sheet.Cells(1, 15).Font.Bold = True
sheet.Cells(1, 16) = "N."
sheet.Cells(1, 16).Font.Bold = True
sheet.Cells(1, 17) = "ARMATORE"
sheet.Cells(1, 17).Font.Bold = True
sheet.Cells(1, 18) = "DAL"
sheet.Cells(1, 18).Font.Bold = True
sheet.Cells(1, 19) = "AL"
sheet.Cells(1, 19).Font.Bold = True
sheet.Cells(1, 20) = "TOT"
sheet.Cells(1, 20).Font.Bold = True
sheet.Cells(1, 21) = "N."
sheet.Cells(1, 21).Font.Bold = True
sheet.Cells(1, 22) = "ARMATORE"
sheet.Cells(1, 22).Font.Bold = True
sheet.Cells(1, 23) = "DAL"
sheet.Cells(1, 23).Font.Bold = True
sheet.Cells(1, 24) = "AL"
sheet.Cells(1, 24).Font.Bold = True
sheet.Cells(1, 25) = "TOT"
sheet.Cells(1, 25).Font.Bold = True
sheet.Cells(1, 26) = "N."
sheet.Cells(1, 26).Font.Bold = True
sheet.Cells(1, 27) = "ARMATORE"
sheet.Cells(1, 27).Font.Bold = True
sheet.Cells(1, 28) = "DAL"
sheet.Cells(1, 28).Font.Bold = True
sheet.Cells(1, 29) = "AL"
sheet.Cells(1, 29).Font.Bold = True
sheet.Cells(1, 30) = "TOT"
sheet.Cells(1, 30).Font.Bold = True
sheet.Cells(1, 31) = "N."
sheet.Cells(1, 31).Font.Bold = True
sheet.Cells(1, 32) = "ARMATORE"
sheet.Cells(1, 32).Font.Bold = True
sheet.Cells(1, 33) = "DAL"
sheet.Cells(1, 33).Font.Bold = True
sheet.Cells(1, 34) = "AL"
sheet.Cells(1, 34).Font.Bold = True
sheet.Cells(1, 35) = "TOT"
sheet.Cells(1, 35).Font.Bold = True
sheet.Cells(1, 36) = "N."
sheet.Cells(1, 36).Font.Bold = True
sheet.Cells(1, 37) = "ARMATORE"
sheet.Cells(1, 37).Font.Bold = True
sheet.Cells(1, 38) = "DAL"
sheet.Cells(1, 38).Font.Bold = True
sheet.Cells(1, 39) = "AL"
sheet.Cells(1, 39).Font.Bold = True
sheet.Cells(1, 40) = "TOT"
sheet.Cells(1, 40).Font.Bold = True
sheet.Cells(1, 41) = "TOTALE"
sheet.Cells(1, 41).Font.Bold = True
For rowIndex = 1 To rs.RecordCount
sheet.Cells(rowIndex + 1, 1) = Me.Corso
sheet.Cells(rowIndex + 1, 2) = Me.Ed
sheet.Cells(rowIndex + 1, 3) = Me.ETICHETTA
sheet.Cells(rowIndex + 1, 4) = Me.NOME
sheet.Cells(rowIndex + 1, 5) = Me.COGNOME
rs.MoveNext
Next
rs.Close
workbook.SaveAs excel_file_name
workbook.Close
excel_application.Quit
Set sheet = Nothing
Set workbook = Nothing
Set excel_application = Nothing
Set rs = Nothing
Set db = Nothing
End Sub