我有一个表格上运行负载一些代码:使用变量时DLookup不工作?
Private Sub Form_Load()
'Declare the username variable
Dim loginID As String
'Get the username from the environment array variable
loginID = Environ("USERNAME")
'Pop up a message box stating the obvious and checking the variable is set
MsgBox ("Hello " & loginID")
'Lookup the permissions of the user based on the windows logon
MsgBox DLookup("permissions", "Users", "userName = " & loginID)
当我运行此代码,“测试”的信息框的作品,它会弹出我的Windows登录ID,但是当代码继续和我获取到使用DLookup,我得到一个错误VBA:如果我改变使用DLookup声明本 “MORINDAV”
:
2471您输入的查询参数的表达产生这个错误
MsgBox DLookup("permissions", "Users", "userName = 'loginID'")
我得到错误'94',无效使用空值。
如果我保持同样的语法以上,但手动输入用户价值我测试的是这样的:
MsgBox DLookup("permissions", "Users", "userName = 'MORINDAV'")
声明工程和VBA表明我与该用户的权限级别的消息框。
我敢肯定,这是一个语法问题,但我似乎无法得到使用DLookup同时采用了可变工作:登录ID
通过打开Windows命令提示符,将USERNAME设置为伪造值,然后从该会话中启动Access,可以轻松击败'Environ(“USERNAME”)''。 – HansUp