How to retrieve the username

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I am still learning Access. I used to used a Excel as a database but I was told that Access is a database. So I designed a form, and on that form I have two textboxes 1st textbox is labeled LastModified and the second one is labeled ModifiedBy. When I login into my work computer, I have to enter my employeeID and password. So I know in Excel VBA in the Immediate window section, when I type in the following code
VBA Code:
?application.username
and hit return, it actually displays my name (first and last). However, this code does not work in Access. So when I web searched this issue, it stated that I should use the following code to retrieve the username
VBA Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.Dirty Then
       Me.ModifiedByUser = Environ$("username")
    End If
End Sub
But this code only displays my employeeID, which is what I used to login with. How is it that Excel can pull the correct information and Access can't? Thank you.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I'm pretty sure this has to do with the user name that is entered into the Office app, and not the workstation environment variable. Go into Access | Info and take a look at the user name there and compare it to the same screen in Excel. I'd bet that they are different. Looks like you're using Excel 2010 and 365...so in 365 it may be tied to your user ID...so make sure you're logged into the Access app the same way you would with Excel.
 
Upvote 0
While I am by no means an expert, I've recently worked on a system that needed to log the username and from what I have read the best way to do this is with a Windows API call. The Environ function can be spoofed easily.
I use the method found here: API: Get Login name
This retrieves the name the of the user that is currently logged into the machine and that value can be passed to other controls with the custom function =fosusername()

I hope this helps. :)
 
Upvote 0
@Candyman8019 Thank you for the response. Both Excel and Access are within Microsoft 365. I followed the steps that you stated, in Access | File | Info. When I looked at the top right corner by a smiley face and frown, I see my username and not my EmployeeID. Thank you.
 
Upvote 0
@TonyD1016 I clicked that link you sent and copied and pasted the code into my VBA editor and the following code is red:
VBA Code:
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

I receive a compile error: The code in this project must be updated for use on 64-but systems. Please review and update Declare statements and then mark them with the PtrSafe attribute. How can I fix this issue. Thank you.
 
Last edited:
Upvote 0
This the code that is associated with the textbox.
VBA Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    strUserName = fOSUserName
    Me.Modified_By.Text = fOSUserName
End Sub
 
Upvote 0
There's a gazillion posts about ptrsafe in general and specifically this function. Google is my friend and he gave me this

You could run it just once and then assign the returned value to your own Environ variable.
 
Upvote 0
? application.username does indeed get the name in Excel options on my laptop.
That property is not visible to Access.

I used to have a table that held company login ID (6 digits) and real names.
 
Upvote 0

Forum statistics

Threads
1,224,766
Messages
6,180,846
Members
453,001
Latest member
coulombevin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top