2013-05-19 222 views
4

我想做一个登录表单,但我不知道如何获得或确认我在我的用户名和密码文本框中键入的内容是在我的表中使用DLookup在我的登录按钮。代码为DLookup登录用户名和密码

这里是我当前的代码:

Dim u As Variant 
Dim p As Variant 
Dim inu As String 
Dim inp As String 

u = DLookup("cusername", "tbl_users", "inuser.Value") 
p = DLookup("cpassword", "tbl_users", "inpass.Value") 

inu = inuser.Value 
inp = inpass.Value 

If inu = u And inp = p Then 
DoCmd.OpenForm "frm_userlog" 
MsgBox "Welcome " & tuser & "!" 

ElseIf IsNull(Me.inuser.Value) And inpass.Value = 1 Then 
MsgBox "You must input a username" 

ElseIf IsNull(Me.inpass.Value) And inuser.Value = 1 Then 
MsgBox "you must input a password" 

ElseIf IsNull(Me.inuser.Value) And IsNull(Me.inpass.Value) Then 
MsgBox "you must input a username and password" 

Else 
MsgBox "The username or password you entered is invalid" 
    End If 
End Sub 
+0

您需要在密码查找中使用where条件通过正确的用户名来选择。我有工作的代码,包括我可以在明天发布的基本密码加密 –

回答

3

第三DLookup说法,标准,是一个可选的字符串表达式,类似于“WHERE子句中的SQL表达式,无字WHERE”

在你的身上,你似乎试图给它一个名为inuser的控件的值。但是,您实际上正在传递一个字符串,其中包含文字“inuser.Value”

DLookup("cusername", "tbl_users", "inuser.Value") 

但即使删除引号,也不会给你想要的。

如果你想从那里tbl_users一些领域(也许user_id)匹配inuser.Value查找cusername ...

DLookup("cusername", "tbl_users", "user_id = " & inuser.Value) 

如果该字段,user_id,是文本而不是数字数据类型,建立报价进入标准串...

DLookup("cusername", "tbl_users", "user_id = '" & inuser.Value & "'") 

它看起来像你对我有同一类型的问题0,所以如果我得到了第一个权利,在那里做一个类似的改变。

你可以在Description of DLookup() usage, examples, and troubleshooting in Access 2000找到更多关于DLookup的信息。虽然这是一篇旧文章,但所有内容仍适用于最近的Access版本AFAICT。

0

按我的评论我用这个:

Private Sub cmdlogin_Click() 
    If IsNull(Me.cb_user) Then 
     MsgBox "You didn't select a user", vbCritical, "Error" 
     Me.cb_user.SetFocus 
     Exit Sub 
    Else 
     If IsNull(Me.txtpass) Then 
      MsgBox "You didn't enter a password", vbCritical, "Error" 
      Me.txtpass.SetFocus 
      Exit Sub 
     Else 
      If encrypt(Me.txtpass.Value) = DLookup("password", "users", "user_id = " & Me.cb_user.Value) Then 
       DoCmd.OpenForm ("home") 
       Me.Visible = False 
      Else 
       MsgBox "Incorrect password. Please try again", vbCritical, "Incorrect password" 
       Me.txtpass = Null 
       Me.txtpass.SetFocus 
      End If 
     End If 
    End If 
End Sub 

哪里cb_user是用户名的组合框。

Encrypt是基本ROT 13加密,其余的模块中放置:

Public Function encrypt(strInput As String) 
    Dim n As Integer, i As Integer 
    n = 13 
    For i = 1 To Len(strInput) 
     Mid(strInput, i, 1) = Chr(Asc(Mid(strInput, i, 1)) + n) 
    Next i 
    encrypt = strInput 
End Function 

如果这不是必要省略周围的密码查找使得encrpyt涡卷:

If encrypt(Me.txtpass.Value) = DLookup("password", "users", "user_id = " & Me.cb_user.Value) Then 

变得

If Me.txtpass.Value = DLookup("password", "users", "user_id = " & Me.cb_user.Value) Then 
相关问题