21

Possible Duplicate:
Is there a way for MS Access to grab the current Active Directory user?

I want to get the name of the logged-in User via in . What is the best way to do this?


Duplicates of this question:

0

3 Answers 3

47

Try this:

Function UserNameWindows() As String
     UserName = Environ("USERNAME")
End Function
4
  • 2
    Bear in mind that this can be changed by users/applications... Mar 24, 2009 at 12:22
  • 10
    It might not be 100% secure every time, but let's be honest, we're talking about VBA. I for one, choose the easy way :)
    – Eric
    Jan 27, 2012 at 23:58
  • 2
    @Eric +1 Here. If you're that concerned, you shouldn't be using Access in the first place.
    – Christine
    Sep 27, 2016 at 22:52
  • 1
    Perfect. This works great for me; I don't give a crap if it's not 9001% secure. Thanks man!
    – clamum
    Sep 25, 2017 at 17:49
24
Public Declare Function GetUserName Lib "advapi32.dll" 
    Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

....

Dim strLen As Long
Dim strtmp As String * 256
Dim strUserName As String

strLen = 255
GetUserName strtmp, strLen
strUserName = Trim$(TrimNull(strtmp))

Turns out question has been asked before: How can I get the currently logged-in windows user in Access VBA?

4
  • I didn't see that the question has been asked before. Thanks, anyways Mar 24, 2009 at 13:51
  • Be aware that Declare is not available in 64-bit Access. Dec 28, 2015 at 22:13
  • Can you show your code for TrimNull? Mar 28, 2018 at 14:57
  • Would the downvoter please leave a comment. May 31, 2018 at 1:11
-3

In a Form, Create a text box, with in text box properties select data tab

Default value =CurrentUser()

Current source "select table field name"

It will display current user log on name in text box / label as well as saves the user name in the table field

1
  • 6
    This does not display the current user's name; it returns the "name of the current user of the database". This defaults to "Admin" unless you've established a security-enabled workgroup.
    – Paul
    Jan 21, 2013 at 18:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.