我正在使用MS Access 2010中的销售CRM数据库。我正在尝试以各种形式自动填充用户名。
数据库加载到登录页面,询问用户名和密码。用户名在Employee表中设置,而不是基于Windows登录。 employee表具有以下字段(EmpID PK,名字,姓氏,电话,电子邮件,备注,标题,用户名TEXT,密码)。
我有两张表(客户联系人和潜在客户),要求员工取得所有权'活动。我希望员工字段根据登录的人自动填充。
以下是登录页面上“确定”按钮背后的代码:
Private Sub Command1_Click()
If IsNull(Me.txtUserID) Then
MsgBox "Please enter user name.", vbInformation, "User Name Required"
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter password.", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'process the job
If (IsNull(DLookup("[UserName]", "tblEmployee", "[UserName] ='" & Me.txtUserID.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect user name or password."
Else
Me.Visible = False
DoCmd.OpenForm "frmHome"
End If
End If
End Sub
答案 0 :(得分:0)
您需要将用户名存储在全局变量中,或将其写入临时表。
要将其存储在全局变量中,只需在模块中声明它
即可Dim strUser as String
然后像这样修改您的登录表单代码
If (IsNull(DLookup("[UserName]", "tblEmployee", "[UserName] ='" & Me.txtUserID.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect user name or password."
Else
Me.Visible = False
strUser=Me.txtUserID.Value
DoCmd.OpenForm "frmHome"
End If
这有一个缺点,就是在任何未处理的调试/错误发生时都会降低其值。它具有快速且易于使用的优点。
编辑:
我个人会去当地的一张桌子。确保您在应用程序关闭/注销时以及启动时DELETE * FROM tblLocalTableName
。如果您需要任何实施方面的帮助,请与我们联系。
编辑: 对于本地表,请在前端文件的Access中创建一个表。让我们称之为' tblLocalVars'。主键文本字段,让它命名为“键”,另一个文本字段,让我们称之为“Val'”。在此表中添加一行,Key =' LoggedUser'并将Val留空。
然后在您的登录表单上,代码看起来像这样:
Private Sub Command1_Click()
If IsNull(Me.txtUserID) Then
MsgBox "Please enter user name.", vbInformation, "User Name Required"
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter password.", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'process the job
If (IsNull(DLookup("[UserName]", "tblEmployee", "[UserName] ='" & Me.txtUserID.Value & "' And password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect user name or password."
Else
CurrentDB.Execute "UPDATE tblLocalVars SET Val='" & Me.txtUserID & "' WHERE Key='LoggedUser'"
Me.Visible = False
DoCmd.OpenForm "frmHome"
End If
End If
End Sub
现在,只要您想获取当前用户的用户名,就可以使用:
Dlookup("Val","tblLocalVars", "Key='LoggedUser'")
现在您只需要将其添加到注销码:
CurrentDB.Execute "UPDATE tblLocalVars SET Val='' WHERE Key='LoggedUser'"
确保将此代码添加到登录表单上的Unload事件中。
答案 1 :(得分:0)
使用Environ(“username”)将返回当前用户用户名的值。使用environ(“userdomain”)将返回当前用户域的值。
在您的即时窗口中,执行?Environ(“用户名”)来测试并查看您获得的内容
在您希望用户获得所有权的表单上,您可以使用用户单击以获取所有权的命令按钮,您可以从Environ获取用户的用户名(“用户名”)并在表中更新用户作为所有者
无需登录页面(除非您需要额外的安全性),您的应用程序将始终知道该用户是谁。
此外,在您提供表单的查询中,您可以将= Environ(“username”)作为用户名字段中的from参数,以向用户显示他们拥有的记录。
HTH