Dim uid, pwd As String
Dim cn As ADODB.Connection
uid = txtUser.Text
pwd = txtPwd.Text
If uid <> "" Or pwd <> "" Then
cn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;" & "User ID=" & uid & ";password=" & pwd & ";Data Source=(local);Initial Catalog=telphone"
cn.Open
MsgBox "登錄成功", vbOKOnly
ConnectString = cn.ConnectionString
cn.Close
Unload Me
Else
   MsgBox "用戶名或密碼不能為空!", vbInformation, "錯誤"
   txtUser.SetFocus
End If为什么老出错

解决方案 »

  1.   

    DatabaseName = databasenametxt
            cn.ConnectionString = "provider=" & providercob & ";server=" & servernametxt & ";uid=" & uidtxt & ";pwd=" & userpwd & ";database=" & DatabaseName
            cn.CursorLocation = adUseClient
            cn.Open
        Dim msg  As String
        Set snp = Nothing
        s = " SELECT  授权.*"
        s = s & " FROM 授权 "
        s = s & " where 授权.编号='" & Trim$(nametxt.Text) + "' and (管理类别=" & IIf(app_type = "系统维护", "'系统维护'", "'" & app_type & "管理'") & ")"
        snp.Open s, cn
        If snp.EOF Then
            Chk = 1
        Else
            If Not IsNull(snp("口令")) Then
                If txtpwd.Text <> snp("口令") Then
                    Chk = 2
                Else
                    If IsNull(snp("WorkAt")) Then
                        Chk = 0
                    Else
                        If snp("WorkAt") <> ComputerName Then
                            Chk = 3
                        Else
                            Chk = 0
                        End If
                    End If
                End If
            Else
                If txtpwd.Text <> "" Then
                    Chk = 2
                Else
                    If IsNull(snp("WorkAt")) Then
                        Chk = 0
                    Else
                        If snp("WorkAt") <> ComputerName Then
                            Chk = 3
                        Else
                            Chk = 0
                        End If
                    End If
                End If
            End If
        End If
        If Chk = 0 Then
            DoEvents
            '进入
            LogSuccess
            welfrm.Visible = True
            welfrm.Refresh
            Me.MousePointer = 0
            Unload Me
            Exit Sub
        ElseIf Chk = 3 Then
            msg = Chr$(13) & "该用户已在" & snp("WorkAt") & "上工作" & Chr$(13) & "不准再次登录!"
            MsgBox msg, 48
        Else
            counter = counter - 1
            If counter = 0 Then
                msg = Chr$(13) & "你不是合法用户!" & Chr$(13) & "对不起,再见!"
                MsgBox msg, 48
                cn.Close
                Set snp = Nothing
                Set cn = Nothing
                End
            ElseIf counter = 1 Then
                msg = Chr$(13) & "这是最后一次.请仔细!"
            Else
                msg = Chr$(13) & "别急,想好.请再试一次"
            End If
            If Chk = 1 Then
                MsgBox "无此编号∶" & nametxt.Text & msg, 48
            ElseIf Chk = 2 Then
                MsgBox "错误口令" & Chr$(13) & msg, 48
            End If
            txtpwd.Text = ""
        End If
      

  2.   

    就是cnn.connectstring的属性出错
    说对象变量没有设置
      

  3.   

    在之前添加一句: set cn =new adodb.connection
    因为楼主只申明了变量没有实列化
      

  4.   

    Dim cn As ADODB.Connection
      

  5.   

    象这样:
    工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)    Dim CN   As New ADODB.Connection                '定义数据库的连接
        Dim Rs   As New ADODB.Recordset
        CN.ConnectionString = "Provider=sqloledb;Data Source=pmserver;Initial Catalog=northwind;User Id=sa;Password=sa;"
        CN.Open
        Rs.CursorLocation = adUseClient
        Rs.Open "select * from employees", CN, adOpenDynamic, adLockOptimistic