that's my solution, it append and update from excel files, maybe i'll add delete too but it's easy. I made just one test but it seems to work. Naturally if you have autonumber field there will be changes to make
Code:
Public Function ImportFromExcel() ' controlla se i file excel prodotti da powerapps sono pił aggiornati delle tabelle access e le aggiorna Dim db As DAO.Database, Rst As DAO.Recordset, RstApp As DAO.Recordset, Tbf As DAO.TableDef, FldNum As Integer
Dim TbEx As String, FldName As String, ColName As String
Set db = CurrentDb
'DoCmd.SetWarnings False
For Each Tbf In db.TableDefs
TbEx = Tbf.Name & "ex"
'Debug.Print (Tbf.Name)
If ifTableExists(TbEx) Then
FldName = GetIndex(Tbf)
DoCmd.RunSQL ("INSERT INTO [" & Tbf.Name & "] SELECT [" & TbEx & "].* FROM [" & TbEx & "] LEFT JOIN " & vbCrLf & _
"[" & Tbf.Name & "] ON [" & TbEx & "].[" & FldName & "] = [" & Tbf.Name & "].[" & FldName & "] " & vbCrLf & _
"WHERE ((([" & Tbf.Name & "].[" & FldName & "]) Is Null) AND (([" & TbEx & "].app)=""pw""));")
DoCmd.RunSQL (Updatequery(Tbf))
End If
Next Tbf
'DoCmd.SetWarnings True
End Function
Code:
Public Function Updatequery(Tb As DAO.TableDef) As StringOn Error GoTo ErrHandler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim qry As String
Dim Tbl As String
Dim new_value As String
Dim IdxName As String
'this is the name of the table in question
Tbl = Tb.Name
Set db = CurrentDb
Set rs = db.OpenRecordset(Tbl)
IdxName = GetIndex(Tb)
'build an update query string
qry = "UPDATE [" & Tbl & "ex] INNER JOIN [" & Tbl & "] ON [" & Tbl & "ex].[" & IdxName & "] = [" & Tbl & "].[" & IdxName & "] SET"
For Each fld In rs.Fields
new_value = Tbl & "Ex" & ".[" & fld.Name & "]"
qry = qry & "[" & Tbl & "].[" & fld.Name & "] = " & new_value & ", "
Next
'remove the last comma and space from last loop
qry = Left(qry, Len(qry) - 2)
qry = qry & " WHERE ((([" & Tbl & "ex].[Data/ora modifica])>[" & Tbl & "].[Data/ora modifica]));"
Debug.Print (qry)
Updatequery = qry
'close the recordset. we don't need it anymore
rs.Close
'print the string to the immediate debug window and make sure it looks correct (CTRL-G)
'execute the qry (uncomment the following line when output from debug.print line above is built correctly)
'db.Execute qry, dbFailOnError
ExitHandler:
'clean up
Set fld = Nothing
Set rs = Nothing
Set db = Nothing
Exit Function
ErrHandler:
Debug.Print "*** Error #" & Err.Number & " - " & Err.Description
MsgBox Err.Description, , "Error #" & Err.Number
Resume ExitHandler
End Function
Code:
Public Function GetIndex(Tbf As DAO.TableDef) As String
Dim Idx As Index
For Each Idx In Tbf.Indexes
On Error Resume Next
If Idx.Primary Then GetIndex = Replace(Idx.Fields, "+", "")
Next Idx
End Function