我在Access中有一个用户名,名称为BAF_User,包含2个字段(BAFUser,BRID)。
我使用以下代码获取Windows用户名,然后使用DLookup进行比较,并获取已登录Access文件的用户的全名。
Option Compare Database
Public Function GetUserName() As String
Dim wshNet As Object
Dim As String
Set wshNet = CreateObject("WScript.Network")
GetUserName = wshNet.UserName
Set wshNet = Nothing
MyName = DLookup("[BAFUser]", "BAF_User", "[BRID] = '" & GetUserName & " '")
End Function
Private Sub Form_Load()
MsgBox "Welcome" + MyName
End Sub
但这并没有显示价值,我无法理解我做错了什么。
感谢您的帮助。
答案 0 :(得分:0)
为什么要使用全局变量?只需调用该函数并使其返回值。
Public Function GetUserName() As String
Dim wshNet As Object
Set wshNet = CreateObject("WScript.Network")
GetUserName = DLookup("[BAFUser]", "BAF_User", "[BRID] = '" & wshNet.UserName & "'")
Set wshNet = Nothing
End Function
Private Sub Form_Load()
MsgBox "Welcome " & GetUserName
End
替代方案:
MsgBox "Welcome " & DLookup("[BAFUser]", "BAF_User", "[BRID] = '" & Environ("USERNAME") & "'"