【发布时间】:2014-03-20 02:35:00
【问题描述】:
我有一些在表单加载时运行的代码:
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 错误:
2471 您作为查询参数输入的表达式产生了以下错误: '莫林达夫'
如果我将 Dlookup 语句更改为:
MsgBox DLookup("permissions", "Users", "userName = 'loginID'")
我收到错误“94”,无效使用 Null。
如果我保持与上述相同的语法,但手动输入我正在测试的用户值,如下所示:
MsgBox DLookup("permissions", "Users", "userName = 'MORINDAV'")
该语句有效,VBA 向我显示了一个带有该用户权限级别的消息框。
我确定这是语法问题,但在使用变量时我似乎无法让 DLookup 工作:loginID
【问题讨论】:
标签: ms-access vba ms-access-2010