封装ADO的列子运用,出现问题。
Public Sub 查询()Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim spath As String
spath = ThisWorkbook.Path & "\alldata.mdb"
jg = funConnectDataBase(cnn, rst, strpath, "")
sql = "select * from 材料入库"
Range("a1") = subExcuteSQL(cnn, rst, sql, jg)
End Sub
Public Function funConnectDataBase(cnnp As ADODB.Connection, adop As ADODB.Recordset, ByVal strpath As _
String, ByVal strPassword As String) As Boolean
On Error GoTo errFunction
Set cnnp = New ADODB.Connection
Set adop = New ADODB.Recordset
cnnp.Provider = "Microsoft.Jet.OLEDB.4.0"
cnnp.Open "Data Source = " & strpath & ";jet oledb:database password=" & strPassword
funConnectDataBase = True
Exit Function
errFunction:
funConnectDataBase = False
End Function
Public Function funCloseDataBase(cnnp As ADODB.Connection, adop As ADODB.Recordset) As Boolean
On Error GoTo errFunction
Set adop = Nothing
Set cnnp = Nothing
funCloseDataBase = True
Exit Function
errFunction:
funCloseDataBase = False
End Function
Public Sub subExcuteSQL(cnnp As ADODB.Connection, adop As ADODB.Recordset, strSql As String, bolQueryRecord _
As Boolean)
If bolQueryRecord Then '如果是查询记录集
adop.Open strSql, cnnp, adOpenStatic, adLockBatchOptimistic
Else
cnnp.Execute strSql
End If
End Sub
这个是封装ADO为类的代码,执行查询,总是报错:ByRef参数类型不符,大侠们,请问是为什么? Set cnnp = New ADODB.Connection
Set adop = New ADODB.Recordset
去掉试试 Public Sub 查询()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim spath As String
Dim sql As String
Dim jg As Boolean
spath = ThisWorkbook.Path & "\alldata.mdb"
jg = funConnectDataBase(cnn, rst, strpath, "")
sql = "select * from 材料入库"
Range("a1") = subExcuteSQL(cnn, rst, sql, jg)
End Sub
Public Function funConnectDataBase(cnnp As ADODB.Connection, adop As ADODB.Recordset, ByVal strpath As _
String, ByVal strPassword As String) As Boolean
On Error GoTo errFunction
Set cnnp = New ADODB.Connection
Set adop = New ADODB.Recordset
cnnp.Provider = "Microsoft.Jet.OLEDB.4.0"
cnnp.Open "Data Source = " & strpath & ";jet oledb:database password=" & strPassword
funConnectDataBase = True
Exit Function
errFunction:
funConnectDataBase = False
End Function
Public Function funCloseDataBase(cnnp As ADODB.Connection, adop As ADODB.Recordset) As Boolean
On Error GoTo errFunction
Set adop = Nothing
Set cnnp = Nothing
funCloseDataBase = True
Exit Function
errFunction:
funCloseDataBase = False
End Function
Public Sub subExcuteSQL(cnnp As ADODB.Connection, adop As ADODB.Recordset, strSql As String, bolQueryRecord _
As Boolean)
If bolQueryRecord Then '如果是查询记录集
adop.Open strSql, cnnp, adOpenStatic, adLockBatchOptimistic
Else
cnnp.Execute strSql
End If
End Sub
问题是,执行完查询后,怎么关闭数据库呢? 原因不是这个,请大侠们指点1,2 本来一个很简单的问题,非得搞成这么复杂
补充:VB , 网络编程