if cnn.state=1 then ...

解决方案 »

  1.   

    if TCn.State <> adStateClosed then
        TCn.Close 
    end if
      

  2.   

    检查adoconnection对象的State属性
    if cnn.state=adstateopen then 
        cnn.close
    endif
    state是只读属性,具体意思如下adStateClosed 默认,指示对象是关闭的。 
    adStateOpen 指示对象是打开的。 
    adStateConnecting 指示 Recordset 对象正在连接。 
    adStateExecuting 指示 Recordset 对象正在执行命令。 
    adStateFetching 指示 Recordset 对象的行正在被读取。 
      

  3.   

    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