代码:Private Sub CommandButton1_Click()
    
    Dim con As New ADODB.Connection
    Dim cmd As ADODB.Command
    Dim strSQL As String
  
    
    Dim strCon As String
    con.ConnectionString = "driver={SQL Server};" & _
      "server=server;uid=sa;pwd=1111;database=pubs"
        If con.State = adStateOpen Then
      MsgBox "连接成功!"
    Else
      MsgBox "失败."
    End If    con.Close
    
    
End Sub

解决方案 »

  1.   

    好像在if之前少了 con.connect吧
      

  2.   

    Private Sub CommandButton1_Click()
        on error resume next
        Dim con As New ADODB.Connection
        Dim cmd As ADODB.Command
        Dim strSQL As String
      
        
        Dim strCon As String
        con.ConnectionString = "driver={SQL Server};" & _
          "server=server;uid=sa;pwd=1111;database=pubs"
        
        con.open
        If con.State = adStateOpen Then
          MsgBox "连接成功!"
        Else
          MsgBox "失败."
        End If    con.Close
           
    End Sub
      

  3.   

    Private Sub CommandButton1_Click()
        
        Dim con As New ADODB.Connection
        Dim cmd As ADODB.Command
        Dim strSQL As String
      
        
        Dim strCon As String
        con.ConnectionString = "driver={SQL Server};" & _
          "server=server;uid=sa;pwd=1111;database=pubs"
            con.Open '加上这句========================    If con.State = adStateOpen Then
          MsgBox "连接成功!"
        Else
          MsgBox "失败."
        End If    con.Close
        
        
    End Sub
      

  4.   

    這人不是粗心吧.少了个con.open