I have a userform in Access with name BAF_User with 2 fields (BAFUser, BRID).
I am using the below code to get windows username and then compare it using DLookup and get the Full Name of the user who has logged into the Access file.
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
But this is not showing the value, I am unable to catch what I have done wrong.
Thanks for help.
Why use global variable? Just call the function and have it return value.
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
An alternative:
MsgBox "Welcome " & DLookup("[BAFUser]", "BAF_User", "[BRID] = '" & Environ("USERNAME") & "'"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments