我在多用户环境中开发MS Access工具。基于登录,不同的用户拥有不同的视图并获得不同的信息。
所以我的问题是我在模块中声明的全局变量没有保存并将信息传输到表单,我希望根据条件进行说明。
以下是全局变量(模块级别)设置的代码,它完美地运行。
Option Compare Database
Global GBL_Username as String
Public Function Init_Globals() As String
GBL_Username = Forms!frm_Login.txtUsername
DoCmd.OpenForm "frm_Login"
frm_Login.Visible = False
Forms!frm_Login.txtUsername.Value.Enabled = False
End Function
在应该填充的形式中,我调用该函数并将其设置在过滤器字段(txtfilterfield
)中。
然而,经过多次尝试后,我仍然会遇到一些错误,例如"变量未定义"或者它只显示变量是" "
。
我将在此感谢您的帮助。
答案 0 :(得分:1)
尝试Public GBL_Username As String
答案 1 :(得分:1)
我不确定这是否是一个答案,但是对于评论来说太长了,因此发布了答案。
您的功能不会返回任何值,因此在您想要实现的目标方面没有任何意义。
让我们逐行尝试:
Public Function Init_Globals() As String
'Receive the username from a textbox called txtusername? if yes you should use txtUsername.value. if txtusername is a db field your should use frmlogin.[txtusername] << strongly not recommended
GBL_Username = Forms!frm_Login.txtUsername
'Open the login form thereafter? how can you receive the username in previous code without having the form opened?
DoCmd.OpenForm "frm_Login"
'since you have saved/taken your username and saved on a public variable why keeping the login form open and visible?
frm_Login.Visible = False
'I have no clue what this code supposed to do, disable the textbox? if yes you should use txtUsername.enabled = false
Forms!frm_Login.txtUsername.Value.Enabled = False
End Function
也许你想这样做:
将是
Public UserName as String
Public Function GET_USER_NAME() as String
CHECK_LOGIN ' checks if username is already there
get_user_name = userName ' return the username
End Function
Public Function CHECK_LOGIN()
if nz(username,"") = "" then ' check if username is empty
LOGIN ' if empty force relogin
End Id
End function
Public Function LOG_OUT()
userName = ""
end Function
Public Function LOGIN()
docmd.OpenForm "frm_Test", WindowMode:=acDialog
' in your login form after successful login save the username into the userName variable
'something like this. userNAme = me.txt_username.value
End Function
要获取登录的用户名,您可以调用函数“get_user_name”,如
dim crr_user as string
crr_user = get_user_name
注意:我没有检查过错误,我只是写了一篇文章。
这里有一些逻辑思路如何在Access中使用用户登录:
也许查看此帖子有人上传的示例数据库: Microsoft Access - Show name of logged in user from UserID