|
zez 发表于 2008-10-13 10:00
Public Sub 将查询结果进行排序() Dim mydata As String, mytable As String, SQL As St ...
我修改了下代码
提示自动化错误
Sub 读入()
Dim mydata As String, mytable As String, SQL As String
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Integer
'指定数据库和数据表
mydata = "E:\CTA涉税鉴证软件" & "\mb.mdb" '指定数据库
mytable = "kh" '指定数据表
'建立与数据库的连接
Set cnn = New ADODB.Connection
With cnn
.Provider = "microsoft.jet.oledb.4.0"
.Open mydata
End With
'查询数据表
SQL = "select mc as Age1,gsdjzzhm as Age2 " & "from " & mytable & "where sh = " & Range("c5")
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
'也可以使用下面的语句
' Set rs = cnn.Execute(SQL)
'复制数据
Range("c6:c7") = Array(rs!Age1, rs!Age2)
'设置工作表格式
ActiveSheet.Cells.Font.Size = 10
ActiveSheet.Columns.AutoFit
'关闭记录集及数据库连接,并释放变量
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub
|
|