Jon von der Heyden
MrExcel MVP, Moderator
- Joined
- Apr 6, 2004
- Messages
- 10,912
- Office Version
- 365
- Platform
- Windows
I've done this a thousand times, yet now I get error 'Unrecognised Database Format' followed by the full name of the workbook. Uploading from Excel to Access:
The upload method:
The ExecuteCommand
And how I'm pulling it together:
I'm sure I'll get it after an hour or so of further frustration, but can anyone spot anything obviously wrong with the above? The format of the workbook that the upload data is contained in is .xlsx.
The upload method:
Code:
Public Sub BatchUpload(ByVal command As ADODB.command, ByVal strFile As String)
'Note to self: Use doc properties to validate file selection
Dim strCommandText As String
Dim wkbUpload As Workbook
Const strDescription As String = "Error executing batch upload statement!"
Set wkbUpload = ReferenceWorkbook(strFile)
With wkbUpload
.Sheets(1).Rows("1:6").Delete Shift:=xlUp
.Close SaveChanges:=True
End With
strCommandText = "INSERT INTO KPI_TBL SELECT * FROM [Upload$] IN '" & _
strFile & "' 'Excel 8.0'"
Call ExecuteCommand(command, strCommandText, strDescription)
Finish:
End Sub
The ExecuteCommand
Code:
Private Sub ExecuteCommand(ByVal command As ADODB.command, _
ByVal strCommandText As String, _
ByVal strDescription As String)
Dim lngRecordsAffected As Long
command.CommandText = strCommandText
Call command.Execute(RecordsAffected:=lngRecordsAffected, _
Options:=CommandTypeEnum.adCmdText Or ExecuteOptionEnum.adExecuteNoRecords)
Call CheckError(lngRecordsAffected, 1, strDescription)
End Sub
And how I'm pulling it together:
Code:
Public Sub TestBatchUpload()
Dim strFile As String
Dim clsDB As clsManageDB: Set clsDB = New clsManageDB
Dim objCommand As ADODB.command: Set objCommand = New ADODB.command
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.ButtonName = "Select"
If .Show = -1 Then
strFile = .SelectedItems(1)
End If
End With
With clsDB
.DBpath = Evaluate(ThisWorkbook.Names("str_const_DBPATH").RefersTo)
objCommand.ActiveConnection = .DBconnection
Call .BatchUpload(objCommand, strFile)
End With
End Sub
I'm sure I'll get it after an hour or so of further frustration, but can anyone spot anything obviously wrong with the above? The format of the workbook that the upload data is contained in is .xlsx.