简体   繁体   English

使用VBA在Access上创建登录表单

[英]Creating Login form on Access using VBA

I am trying to create a login for my Access db, but I can't get it to work. 我正在尝试为我的Access数据库创建一个登录,但我无法让它工作。 Here is my code (keep in mind "Preparer" is the name of the table holding username and password info: 这是我的代码(请记住“Preparer”是包含用户名和密码信息的表的名称:

Private Sub Command1_Click()

    If IsNull(Me.txtLogin) Then

        MsgBox "Please Enter Login", vbInformation, "Need ID"
        Me.txtLogin.SetFocus

    ElseIf IsNull(Me.txtPassword) Then

        MsgBox "Please Enter Password",vbInformation, "Need Password"    
        Me.txtPassword.SetFocus

    Else

        If ((IsNull(DLookup("Login","Preparer","Login='& Me.txtLogin.Value &'"))) or _
        (IsNull(DLookup("Password","Preparer","Password='& Me.txtPassword.Value &'")))) Then

            MsgBox "Incorrect Login or Password"

        Else

            DoCmd.Close    
            MsgBox "Success"

            DoCmd.OpenForm "Master"

        End If

    End If

End Sub

The first two parts work so if login or password is blank it gives the correct error message, but whenever I put a login and password, even if they are correct (ie they exist in the preparer table) it gives me the "Incorrect Login or Password" 前两个部分工作,所以如果登录或密码为空,它会给出正确的错误信息,但每当我输入登录名和密码时,即使它们是正确的(即它们存在于编制者表中),它也会给我“不正确的登录或密码”

I'm assuming the problem is in the code here: 我假设问题出现在这里的代码中:

If ((IsNull(DLookup("Login","Preparer","Login='& Me.txtLogin.Value &'"))) or _
    (IsNull(DLookup("Password","Preparer","Password='& Me.txtPassword.Value &'")))) Then

    MsgBox "Incorrect Login or Password"

Anyone understand why I can't get to the bottom else statement? 任何人都明白为什么我不能触及其他声明?

You are not comparing the right information. 您没有比较正确的信息。 You should be checking if the Password entered for that Username exists in the table. 您应该检查表中是否存在为该Username输入的Password So your code should be changed to just one single DLookup, as. 因此,您的代码应该只更改为一个单独的DLookup。

Private Sub Command1_Click()
    If IsNull(Me.txtLogin) Then
        MsgBox "Please Enter Login", vbInformation, "Need ID"
        Me.txtLogin.SetFocus
    ElseIf IsNull(Me.txtPassword) Then
        MsgBox "Please Enter Password",vbInformation, "Need Password"    
        Me.txtPassword.SetFocus
    Else
        If Nz(DLookup("Password", "Preparer", "Login = '" & Me.txtLogin.Value & "'")), "GarbageEntry") = Me.txtPassword Then
            DoCmd.Close    
            MsgBox "Success"
            DoCmd.OpenForm "Master"
        Else
            MsgBox "Incorrect Login or Password"
        End If
    End If
End Sub

In the above code, you first use the DLookup to retrieve the Password for the username. 在上面的代码中,您首先使用DLookup来检索用户名的密码。 IF the username matches, a Password will be returned, ELSE a default value of " GarbageEntry " is returned. 如果用户名匹配,将返回密码,ELSE返回默认值“ GarbageEntry ”。 So the Right password (or) GarbageEntry is compared with the actual password entered. 因此,将正确的密码(或)GarbageEntry与输入的实际密码进行比较。 If they are the same then you give them access. 如果它们是相同的,那么你给他们访问权限。 Else a message. 还有一条消息。

Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM