请教VBA使用EXCEL的数据--用ADO方法
Sub OpenPrintXLDataSource()
Dim cnn1 As New ADODB.Connection
Dim rst1 As New ADODB.Recordset
'Open and set recordset.
cnn1.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\book3.xls; " & _
"Extended Properties=Excel 8.0; "
Set rst1 = New ADODB.Recordset
rst1.CursorType = adOpenKeyset
rst1.LockType = adLockOptimistic
rst1.Open "book3 ", cnn1, , , adCmdTable
'Open recordset and print a test record.
Do Until rst1.EOF
Debug.Print rst1.Fields(0).Value, rst1.Fields(1).Value
rst1.MoveNext
Loop
End Sub
出现错误提示如下:
运行时错误 '-2147217865(80040e37) '
微软jet数据库引擎找不到对象 'book3 '。请确定对象是否存在,并正确写出它的名称和路径。
事实上我已经检查过EXCEL表名称和路径无误。我用的数据库软件是ACCESS XP。
急!!!!!!!!!!!!!!!!
[解决办法]
不知你表名是怎么写的?
rst1.Open "Select * from [表名$] ", cnn1, adOpenStatic, adLockReadOnly
上句的表名应该写book3里面的sheet的名字,例如:
rst1.Open "Select * from [Sheet1$] ", cnn1, adOpenStatic, adLockReadOnly
请问VBA使用EXCEL的数据-用ADO方法
请教VBA使用EXCEL的数据--用ADO方法SubOpenPrintXLDataSource()Dimcnn1AsNewADODB.ConnectionDimrst1AsNew
