Need help with the Access VBA I am trying to import spreadsheets from excel into access. Once it is imported it will be appended into a different table and the import table will be deleted so that i can then import a new spreadsheet. However i have error when appending i need to append the entire table because i will not always know the header from the spreadsheet and how many their are. Please Help. Below is the code i have been work on.
Module-
Option Compare Database
Function selectFile()
Dim fd As FileDialog, fileName As String
On Error GoTo ErrorHandler
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
If fd.Show = True Then
If fd.SelectedItems(1) <> vbNullString Then
fileName = fd.SelectedItems(1)
End If
Else
'Exit code if no file is selected
End
End If
'Return Selected FileName
selectFile = fileName
Set fd = Nothing
Exit Function
ErrorHandler:
Set fd = Nothing
MsgBox "Error " & Err & ": " & Error(Err)
End Function
VBA -
Private Sub cmdImportNoDelete_Click()
'Unset warnings
DoCmd.SetWarnings False
'Import spreadsheet
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Import", selectFile, True
'SQL append statement, Needs to append entire table (must be able to append multiply table with different headers
DoCmd.RunSQL "INSERT INTO Tbl_ImportDump * FROM Import"
'SQL delete Table
DoCmd.DeleteObject acTable, "Import"
DoCmd.SetWarnings True
End Sub
Tbl_ImportDump
andImport
have different sets of fields ("headers") ... one table includes fields not present in the other, or maybe both have fields for the same data but the field names and/or data types don't match exactly? If that is the problem, showing us the code you use to select an Excel file is not helpful. – HansUp