Hi, sorry to reply to my old topic but i have the last issue.
i made this code (maybe it needs some polish but it works)
Code:
Public Function ExportExcel()
Dim db As DAO.Database, RstUpdt As DAO.Recordset, RstApp As DAO.Recordset, RstDel As DAO.Recordset, Tbf As DAO.TableDef, FldNum As Integer
Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
Dim blnEXCEL As Boolean, FileName As String, WorksheetName As String, CellRange As Range
Dim Fld As DAO.Field, FldName As String, FldValue As Variant
Dim Idx As Index, i As Integer, ColNum As Integer
Dim TbEx As String, ColName As String, RowNum As Long
Set db = CurrentDb
' stabilisce un'applicazione oggetto excel
blnEXCEL = False
On Error Resume Next
Set xlx = GetObject(, "Excel.application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
xlx.Visible = False ' se setto vero mi apre il file excel in questione
For Each Tbf In db.TableDefs
On Error Resume Next
TbEx = Tbf.Name & "Ex"
If ifTableExists(TbEx) Then 'controlla se esiste la tabella collegata (per evitare tabelle che non ho esportato)
Debug.Print (TbEx)
Set Fld = Tbf.Fields
For Each Idx In Tbf.Indexes ' cerca tra i campi della tabella quali sono i primary key
If Idx.Primary Then
Fld = Idx.Fields
FldName = Replace(Idx.Fields, "+", "") 'ritorna il nome del campo primary key
Debug.Print (FldName)
Set RstUpdt = db.OpenRecordset("SELECT [" & Tbf.Name & "].* FROM [" & TbEx & "] INNER JOIN [" & Tbf.Name & "] ON [" & TbEx & "].[" & FldName & "] = [" & Tbf.Name & "].[" & FldName & "] " & vbCrLf & _
"WHERE ((([" & Tbf.Name & "].[Data/ora modifica])>[" & TbEx & "].[data/ora modifica]));")
Set RstApp = db.OpenRecordset("SELECT [" & Tbf.Name & "].[" & FldName & "] FROM [" & Tbf.Name & "] LEFT JOIN [" & TbEx & "] ON [" & Tbf.Name & "].[" & FldName & "] = [" & TbEx & "].[" & FldName & "] WHERE ((([" & TbEx & "].[" & FldName & "]) Is Null));")
Set RstDel = db.OpenRecordset("SELECT [" & TbEx & "].[" & FldName & "], [" & TbEx & "].[Data/ora creazione], [" & TbEx & "].[Data/ora modifica] " & vbCrLf & _
"FROM [" & TbEx & "] LEFT JOIN [" & Tbf.Name & "] ON [" & TbEx & "].[" & FldName & "] = [" & Tbf.Name & "].[" & FldName & "] " & vbCrLf & _
"WHERE ((([" & Tbf.Name & "].[" & FldName & "]) Is Null));")
RstUpdt.MoveLast '!!!
RstApp.MoveLast
RstDel.MoveLast
Debug.Print (RstUpdt.RecordCount)
Debug.Print (RstApp.RecordCount)
Debug.Print (RstDel.RecordCount)
If RstUpdt.RecordCount > 0 Or RstApp.RecordCount > 0 Or RstDel.RecordCount > 0 Then 'se ci sono record allora faccio l'update del file
FileName = "C:\Users\diego\OneDrive - Indipendente\Lavoro\Informatica\Gestionale\Dati\" & Tbf.Name & ".xlsx"
Set xlw = xlx.Workbooks.Open(FileName)
Debug.Print (FileName)
WorksheetName = Tbf.Name
Set xls = xlw.Worksheets(WorksheetName)
FldNum = CurrentDb.TableDefs(Tbf.Name).Fields.Count ' numero di campi nella tabella
Debug.Print (CurrentDb.TableDefs(Tbf.Name).Fields.Count)
Debug.Print (WorksheetName)
Debug.Print (FldNum)
For i = 1 To FldNum ' trovo la colonna corrispettiva in excel del campo access: cerco dalla prima colonna della prima riga fino al numero di campi della tabella access se il nome campo access è uguale all'intestazione colonna di excel
Debug.Print (xls.Cells(1, i).Value)
If xls.Cells(1, i).Value = FldName Then
ColName = (Left(Right(xls.Cells(1, i).Address, Len(xls.Cells(1, i).Address) - 1), InStr(xls.Cells(1, i).Address, "$")))
Debug.Print (ColName)
Exit For
End If
Next i
If RstUpdt.RecordCount > 0 Then
RstUpdt.MoveFirst
Do While Not RstUpdt.EOF Or RstUpdt.BOF
Debug.Print (RstUpdt.Fields(FldName))
FldValue = RstUpdt.Fields(FldName)
Set xlc = xls.Range(ColName & ":" & ColName).Find(FldValue) 'Rstupdt.Fields(FldName))
Debug.Print (xlc)
RowNum = xlc.Row
Debug.Print (RowNum)
For ColNum = 0 To FldNum - 1
xlc.Offset(0, ColNum).Value = RstUpdt.Fields(ColNum).Value
Debug.Print (xlc.Offset(0, ColNum).Value)
Debug.Print (RstUpdt.Fields(ColNum).Value)
Next ColNum
RstUpdt.MoveNext
Loop
End If
End If
End If
Next Idx
End If
Next Tbf
Set xlc = Nothing
Set xls = Nothing
xlw.Close True ' close the EXCEL file and save the new data
Set xlw = Nothing
If blnEXCEL = True Then xlx.Quit ' se esiste l'applicazione excel la chiude
Set xlx = Nothing
End Function
the problem is that when the code stops it saves the excel file in a new file, but i want to update the existing file. I cannot figure out how to get this