|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
大家好:
我在Excel中,用VBA写了一小段代码,希望从Access表中取得数据。代码如下:
Public Sub ImportData()
Dim mydata As String, mytable As String, SQL As String
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
ActiveSheet.Cells.Clear
mydata = ThisWorkbook.Path & "\成绩管理系统.mdb"
mytable = "成绩单"
Set cnn = New ADODB.Connection
With cnn
.Provider = "microsoft.jet.oledb.4.0"
.Open mydata
End With
SQL = "select * from " & mytable
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
For j = 1 To rs.Fields.Count
Cells(1, j) = rs.Fields(j - 1).Name
Next j
Range("A2").CopyFromRecordset rs
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub
可是在运行时,在.Open mydata处提示错误,如附件图示,我可以肯定文件名没有错,路经更不会错,因为Excel和Access处于同一文件夹下。这是为什么呀?谢谢。
|
|