vb中将excel导入access的问题
Private Sub Command1_Click()
If Combo1.Text = "" Then
MsgBox "请选择要导入的excel文件!", vbInformation, "提示"
Exit Sub
Else
Dim mfile As String, mfile2 As String
On Error Resume Next
mfile = Combo1.Text '得到别处的excel文件的路径
mfile2 = App.Path & "\dm.mdb" '要覆盖掉当前文件夹下的 dm.mdb"
Call ExportExcelSheetToAccess("sheet1", mfile, "sbjbztb.mdb", mfile2)
End If
End Sub
Private Sub ExportExcelSheetToAccess(sSheetName As String, _
sExcelPath As String, sAccessTable As String, sAccessDBPath As String) ‘sSheetName为要导入的excel文件的工作簿名,sExcelPath为excel文件的路径,sAccessTable为access数据库中要导入数据的表名,sAccessDBPath为access数据库的路径
Dim db As Database
Dim rs As Recordset
Set db = OpenDatabase(sExcelPath, True, False, "Excel 9.0")
Call db.Execute("Select * into [;database=" & sAccessDBPath & "]." & _
sAccessTable & " FROM [" & sSheetName & "$]")
MsgBox "Table exported successfully.", vbInformation, "Yams"
End Sub
我执行后,不报错,但是结果也不对,请大家帮着看一下。
[解决办法]
补正一下:
确保在工程中对以下组件的引用:
1、Microsoft Excel 9.0 Object Library
2、Microsoft ActiveX Data Objects 2.8 Library
4、Microsoft DAO 2.5/3.51 Compatibility Library
执行过程不报错是因为你的源码中存在“On Error Resume Next ”这条语名,将该行语句屏蔽掉,然后在逐语句调试模式下(F8)可看到存在的错误之处。在你的工程中可能缺乏对以上三个组件的引用,你需要确认一下。
[解决办法]
的确是这样的,而你的工程组件中的引用应为"Microsoft Excel 11.0 Object Library"。
将源码中的"Excel 9.0" 替换成"Excel 11.0",或者将Set db = OpenDatabase(sExcelPath, True, False, "Excel 9.0")改为Set db = OpenDatabase(sExcelPath)