VB中rs集怎么在两个类之间传递
展开全部
'引用Microsoft ActiveX Data Objects 2.8 Library
'窗体:功能,获得表的字段名放在Text1中
Dim cl2 As New Class2
Dim Rs As ADODB.Recordset
Private Sub Command1_Click()
Set cl2 = New Class2
Set Rs = New ADODB.Recordset
cl2.getRs Rs
Text1.Text = Rs.Fields(0).Name
End Sub
Private Sub Command2_Click()
Set cl2 = Nothing
End Sub
Private Sub Form_Load()
Command1.Caption = "打开"
Command2.Caption = "关闭"
Text1.Text = ""
End Sub
'类模块Class1:打开数据库,获得Recordset
Private Cnn As ADODB.Connection
Private Rs As ADODB.Recordset
Friend Sub returnRS(Rs1 As ADODB.Recordset) '通过该过程,将Recordset传递给调用的类模块
openRS
Set Rs1 = Rs
End Sub
Private Sub openRS() '打开数据库(access数据库文件名为abc.mdb,需要有一个名称为"表1"的表)
Dim connStr As String '连接字符串
Dim Cnn_c As New ADODB.Command
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\abc.mdb;Persist Security Info=False"
Set Cnn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Cnn.ConnectionString = connStr
With Cnn
.CursorLocation = adUseClient
.Open
End With
Cnn_c.CommandType = adCmdText
Cnn_c.CommandText = "select * from 表1"
Set Cnn_c.ActiveConnection = Cnn
Set Rs = Cnn_c.Execute
End Sub
Friend Sub closeLink() '关闭记录集、连接
On Error Resume Next
Rs.Close
Cnn.Close
Set Rs = Nothing
Set Cnn = Nothing
End Sub
'类模块Class2:调用类模块Class1,并获得Class1的Recordset
Private Rs As ADODB.Recordset
Private cl As Class1
Private Sub Class_Initialize()
Set cl = New Class1
cl.returnRS Rs '获得Class1的Recordset
End Sub
Friend Sub getRs(Rs1 As ADODB.Recordset)
Set Rs1 = Rs
End Sub
Private Sub Class_Terminate()
cl.closeLink
Set cl = Nothing
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询