看看我写的!
Option Explicit
Public Con As New ADODB.Connection
Public Rs As New ADODB.RecordsetPublic Function Connecttoserver() As Boolean
    On Error GoTo connecterr
    '打开连接对象
    'If Connecttoserver = False Then Exit Function    'Set rs = New ADODB.Recordset
    Con.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=graduate;Data Source=."
    Con.Open
    Connecttoserver = True
    Exit Function
connecterr:
    '加一句
    Err.Clear
    MsgBox "不能连接!"
    Connecttoserver = False
End Function
Public Function Disconnect() As Boolean
    Rs.Close
    Set Rs = Nothing '不要忘记关闭recodset与释放
    Con.Close
    Set Con = Nothing '同上
    
End Function下面是按钮的:
Private Sub cmdOK_Click()
    'M = False
On Error GoTo on_error
    Dim Strsql As String
    If Connecttoserver Then
        Strsql = "select * from user_info "
        If Rs.State = 1 Then Rs.Close
        Rs.Open Strsql, Con, 3, 3
    End If    Strsql = "select * from user_info"
    'Querydata (Strsql)
    Rs.MoveFirst
    '检查正确的用户及密码
    If Trim(txtUserName.Text) = Empty Then
        MsgBox "用户名称不能为空", vbInformation + vbOKOnly, "数据不完整"
        txtUserName.SetFocus
        Exit Sub
    End If
    If Trim(TxtPassWord.Text) = Empty Then
        MsgBox "用户密码不能为空", vbInformation + vbOKOnly, "数据不完整"
        TxtPassWord.SetFocus
        Exit Sub
    End If
    If Trim(txtUserName.Text) = Rs("username") And TxtPassWord.Text = Rs("password") Then
        'M = True
        Unload Me
        FrmMain.Show
        Exit Sub
    End If
    Rs.MoveNext
    Do While Not Rs.EOF
        If Trim(txtUserName) = Rs("username") And TxtPassWord.Text = Rs("password") Then
            Unload Me
            FrmMain.Show
            Exit Sub
        End If
        Rs.MoveNext
    Loop
    If MsgBox("无效的用户名或密码,请重试!", vbRetryCancel, "登录错误") = vbCancel Then
        Unload Me
        End
    End If
    txtUserName.SetFocus
    Exit Sub
cmdOK_Click_exit:
    Set Rs = Nothing
on_error:
    MsgBox "错误代码:" & Err.Number & vbCrLf & "错误描述:" & Err.Description, vbCritical + vbOKOnly, "添加数据错误"
    Set Rs = Nothing
End Sub为什么电击登陆按钮显示“无效有户或密码呢“我换数据库就能读过去 为什么呢!

解决方案 »

  1.   

    觉得你程序结构很混乱啊,很多重复的地方.
    其实有没有查询到记录集,你单步跟踪一下,看Rs("username"),Rs("password")
    有没有返回就知道了.
      

  2.   

    查询用户名密码的方式不好,如果有10000个记录,你要找的在最后一个,是不是效率很低?
    考虑这种方式:
        userName=replace(userName,"'","")
        userPwd=replace(userPwd,"'","")  ' 去除用户名密码当中有可能的空格    strSQL="select * from User_Info where uID = '" & userName & "'"    set rs=con.Execute(strSQL)
        if(rs.EOF AND rs.BOF) '说明没找到
           strSQL="insert into User_Info values('" & userName & "','" & userPwd & "')"
           con.Execute strSQL
        else
           msgbox "用户名已经存在"
        end if