Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Private Sub Command1_Click() Set cnn = New ADODB.Connection cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\temp\nwind.mdb" End SubPrivate Sub Command2_Click() '检测 If cnn.State = 1 Then MsgBox "打开" Else MsgBox "关闭" End If '控制 If cnn.State = 1 Then cnn.Close '再检测 If cnn.State = 1 Then MsgBox "打开" Else MsgBox "关闭" End If End Sub
TCn.Close
end if
if cnn.state=adstateopen then
cnn.close
endif
state是只读属性,具体意思如下adStateClosed 默认,指示对象是关闭的。
adStateOpen 指示对象是打开的。
adStateConnecting 指示 Recordset 对象正在连接。
adStateExecuting 指示 Recordset 对象正在执行命令。
adStateFetching 指示 Recordset 对象的行正在被读取。
Dim rs As ADODB.Recordset
Private Sub Command1_Click()
Set cnn = New ADODB.Connection
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\temp\nwind.mdb"
End SubPrivate Sub Command2_Click()
'检测
If cnn.State = 1 Then
MsgBox "打开"
Else
MsgBox "关闭"
End If
'控制
If cnn.State = 1 Then cnn.Close
'再检测
If cnn.State = 1 Then
MsgBox "打开"
Else
MsgBox "关闭"
End If
End Sub