根据单元格值致电特定的用户表单

时间:2019-01-01 12:02:47

标签: excel vba excel-vba

我正在创建一个供学生选择大学课程的系统。我已经建立了一个登录表单,该表单从“ StudentInformation”表中读取学生的用户ID和密码。有200名学生属于3个不同的班级,因此我为每个程序(AMForm,FMForm和HRMForm)设置了一个用户表单。

在A列中是其用户ID,B列是其密码。在F列中,每个程序都有术语“ AM”或“ FM”或“ HRM”。登录时,我希望它根据F列中的单元格值打开其特定的用户窗体,例如,如果单元格F2包含“ AM”,则启动AMForm,如果F3包含“ FM”,则启动FMForm,如果单元格F4包含“ HRM” “然后启动HRMForm。

我希望这是有道理的,任何帮助将不胜感激!我在下面输入了当前的登录代码。

Private Sub btnLogin_Click()

    Dim RowNo As Long
    Dim ID As String, PW As String
    Dim WS As Worksheet
    Dim aCell As Range

    On Error GoTo ErrorHandler

'Ensure User ID and password fields are filled

    If Len(Trim(txtUser)) = 0 Then
        txtUser.SetFocus
        MsgBox "Error. UserID cannot be empty."
        Exit Sub
    End If

    If Len(Trim(txtPass)) = 0 Then
        txtPass.SetFocus
        MsgBox "Error. Password cannot be empty."
        Exit Sub
    End If

'Set Range Location

    Application.ScreenUpdating = False

    Set WS = Worksheets("StudentInformation")
    ID = LCase(Me.txtUser)

    Set aCell = WS.Columns(1).Find(What:=ID, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

'If match found
    If Not aCell Is Nothing Then
        RowNo = aCell.Row
        If Me.txtPass = aCell.Offset(, 1) Then
            MsgBox "Login Successful."
            AMForm.Show
            Unload Me
        Else
            MsgBox "Incorrect UserID or Password. Please try again.", vbOKOnly
        End If
 'If not found
    Else
        MsgBox "Incorrect UserID or Password. Please try again.", vbOKOnly
    End If

CleanExit:
    Set WS = Nothing
    Application.ScreenUpdating = True
    Exit Sub
ErrorHandler:
    MsgBox Err.Description
    Resume CleanExit
End Sub

0 个答案:

没有答案