I am using this code to copy the data from excel sheets to access db. For each table i am creating additional 2 columns to hold the excel file name (Data.xlsx) and run date.
I am getting "ByRef argument type mismatch error" when the routine gets called.
Welcome any suggestions to address this issue.
Sub ImportExcelSheetsToAccessTables()
Dim excelFilePath As String
Dim runDate As Date
Dim sheetName As String
excelFilePath = "D:\Migration\Data.xlsx"
runDate = Now() ' Get the current date and time
' Array of sheet names and corresponding table names
Dim sheetTableMapping As Variant
sheetTableMapping = Array( _
Array("Main Table!", "Agr"), _
Array("Assets!", "Assets"), _
Array("Rentals!", "Rentals"), _
Array("CFlows!", "CFlows"), _
)
Dim i As Integer
For i = LBound(sheetTableMapping) To UBound(sheetTableMapping)
sheetName = sheetTableMapping(i)(0)
' Import each sheet into its corresponding table without deleting existing data
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, sheetTableMapping(i)(1), excelFilePath, True, sheetName
' Append DataSource and RunDate to each table
AppendTrackingData sheetTableMapping(i)(1), sheetName, runDate
Next i
MsgBox "Data import complete!", vbInformation
End Sub
Sub AppendTrackingData(tableName As String, dataSource As String, runDate As Date)
Dim sql As String
sql = "UPDATE " & tableName & " SET DataSource = '" & dataSource & "', RunDate = #" & Format(runDate, "yyyy-mm-dd hh:nn:ss") & "# WHERE DataSource IS NULL;"
DoCmd.RunSQL sql
End Sub
AppendTrackingDatawill be sufficent for the error you mention.AppendTrackingData(ByVal tableName As String, dataSource As String, runDate As Date)should do it. And it is always a good idea to runDebug/Comilebefore you run the code.