Unable to get DLookup value with variable field

shiv chhabra

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.

June7

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

DLookup not working when using a variable?

From Dev

AWK print field by variable value

From Dev

Awk not assigning value of field to variable

From Dev

Unable to set variable value in callback

From Dev

unable to get $_SESSION variable

From Dev

How to get Meteor collection field value using variable as field key

From Dev

Unable to get environment variable

From Dev

Get default value of a field

From Dev

Unable to set value of input field

From Dev

How to get the value of field

From Dev

Unable to get value of subject field of the draft using Webdriver in Yahoo draft mail

From Dev

unable to get span value

From Dev

defining field in z={ field : 'value'} using variable?

From Dev

DLookup Or DMax To Find Value

From Dev

MS Access VBA Dlookup on Yes/No field

From Dev

Unable to get the accurate value of shell variable set inside a pipe

From Dev

XSLT assign field value to variable

From Dev

Unable to get environment variable

From Dev

How to get the value of a field in an sql Table and then use it as a variable?

From Dev

Unable to get hidden form value into javascript variable

From Dev

Using a variable to get resource field

From Dev

DLookup Function - returning wrong value

From Dev

Unable to store value in a variable

From Dev

Unable to get hidden field value in ASP

From Dev

How to get the value of field

From Dev

Unable to get auth email as field value in view (larave5.3)

From Dev

java unable to get value of static variable from the same class

From Dev

Firebase Firestore - unable to get the value of a field in a collection

From Dev

Unable to get the value of variable inside a function

Related Related

HotTag

Archive