将Excel工作表导入Access表

时间:2016-07-21 20:14:23

标签: vba excel-vba access-vba access excel

当我尝试将Excel文件导入Access表时,我收到错误Run-time error 3125 'Import$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long。当其他Excel文件打开时,会收到此错误,否则Excel文件将按预期加载到表中。以下是我正在使用的一些代码:

Dim f As FileDialog, str As String
Set f = Application.FileDialog(msoFileDialogFilePicker)
f.Show
On Error GoTo Err1
str = f.SelectedItems(1)

Dim xl As Excel.Application
Set xl = New Excel.Application
xl.Visible = True

Dim xlWB As Excel.Workbook
Set xlWB = xl.Workbooks.Open(str)

'manipulate Excel file

'load spreadsheet
On Error GoTo Err2
DoCmd.TransferSpreadsheet acImport, 8, "JobCode", str, True, "Import!"

我原本以为并尝试更改文件名,但未成功。看起来代码正在以只读方式打开导入Excel文件的新版本,这可能会混淆要导入工作表的Access数据库。但是我不确定它为什么这样做以及为什么它只在另一个Excel文件打开时才这样做。这是问题吗?有谁知道如何解决这一问题?

谢谢!

0 个答案:

没有答案