我正在使用Access 2007。 我的登录表单有用户名和密码,我想将用户名的值从登录表单传递到菜单表单。我的代码如下
'Login Form'
Private Sub Command1_Click()
If IsNull(Me.txtUserName) Then
MsgBox "Please enter Username", vbInformation, "Username Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'process login'
If (IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Or _
(IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect"
Else
DoCmd.Close
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName
End If
End If
End Sub
我把菜单表格的代码
Me.Label4 = "Hi " & Me.OpenArgs & "!"
我希望菜单表单自动检测用户名是什么。实施例
Hi John!!
错误表示无效参数。问我的代码中有错误吗?
答案 0 :(得分:1)
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName
OpenArgs不是第三个参数,它是第七个参数,它可以解释错误(第三个参数是一个过滤器):
DoCmd.OpenForm "Menu", acNormal, , , , , "Bob"
(或使用命名参数)
然后
MsgBox "Hello " & Me.OpenArgs
会奏效。
答案 1 :(得分:0)
您可以在登录表单中将用户名存储为变量,并在打开菜单后立即设置消息:
'Login Form'
Private Sub Command1_Click()
Dim un As String
If IsNull(Me.txtUserName) Then
MsgBox "Please enter Username", vbInformation, "Username Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
un = Me.txtUserName
'process login'
If (IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Or _
(IsNull(DLookup("[Login_ID]", "Login_Detail", "[Login_ID] ='" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect"
Else
DoCmd.Close
DoCmd.OpenForm "Menu", acNormal, Me.txtUserName
Forms!Menu!Label4 = "Hi " & un & "!"
End If
End If
End Sub
如果它抛出错误,请将DoCmd.Close
移到最后。
答案 2 :(得分:0)
你有两种可能性。
在“菜单”表单中,创建以下子项:
Public Sub Pass_Login_Info(strUserName As String)
Me.Label4 = "Hi " & strUserName & "!"
End Sub
在“登录”表单中,调用菜单表单的子句,如下所示:
DoCmd.OpenForm "Menu", acNormal
Form_Menu.Pass_Login_Info un
这对我来说是更简单和最好的方法,因为用户名是应该在整个应用程序中知道和检索的内容。
只需创建一个模块,并在其中添加一个可在任何地方使用的公共变量
Public USER_NAME as string
在您的登录表单中:
USER_NAME = un
在“菜单”表单的“加载”事件中
Private Sub Form_Load()
Me.Label4 = "Hi " & USER_NAME & "!"
End Sub