用户登录界面


【实例说明】
    在系统登录时,常常会需要密码登录。

【编程思路】
    不用控件打开数据库。


【设计步骤】
    1.新建一个标准工程,创建一个新窗体,默认名为Form1。
    2.在Form1窗体上放置两个Command控件、一个ComboBox、一个TextBox。
    3.建一个Access表,表的字段分别是UserName、PassWord。
    4.添加一条记录:(UserName)Biirch (PassWord)123

    5.源程序  [素材源程序下载]


Option Explicit

Private User As Recordset

Private Sub cmdOK_Click()
        '和创建创建Recordset对象成对,必须,意思未知
        Set User = New Recordset

        '判断窗体上的文本框中是否有为空的,假如有
        If Len(cmbUser.Text) = 0 Then
                MsgBox "请选择一个用户名。", vbInformation, "系统提示"
                cmbUser.SetFocus
                txtPassword.Text = ""
                Exit Sub
        ElseIf Len(txtPassword.Text) = 0 Then
                MsgBox "请输入您的密码。", vbInformation, "系统提示"
                txtPassword.SetFocus
                Exit Sub
        End If

        '////////////////////////////////////////////////////////////////////////
        User.Open "Select Password From UserManage Where UserName='" & cmbUser.Text & " '", _
                "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=Data.mdb", _
                adOpenKeyset, adLockOptimistic
        '打开User的open方法, SQL & DataBase类型Access & 路径 & 打开方式,加锁方式
        '////////////////////////////////////////////////////////////////////////

                '假如用"user!字段名"语法获得的表中的密码与文本框中输入的密码相等,那么
                If txtPassword.Text = User!Password Then
                        MsgBox "密码登录成功!", vbInformation, "提示"
                        Unload Me
                Else
                        ' 提示错误
                        MsgBox "密码错误,请重新输入!", vbCritical, "系统提示"
                        txtPassword.Text = ""
                        txtPassword.SetFocus
                End If

        User.Close
End Sub

Private Sub cmdCancel_Click()
        End
End Sub

'Londing
Private Sub cmdReg_Click()
        MsgBox "感谢使用『VB网』程序", vbInformation, "系统提示"
End Sub