Access 2003 VBA and Windows Username question

Can I use the windows username in a vba function to compare to a value in an access database and then return the security level based on that, I really only need help getting the Windows username from the system to the vba module, help I am constrained to VBA/Access 2003 only and I know that this would take about 5 minutes in C# and SQL Server. Any ideas would be appreciated.
Who is Participating?
mbizupConnect With a Mentor Commented:
Except use quotes:

strUserName = environ("username")
Use the following VBA:

strUserName = environ(UserName)
rockiroadsConnect With a Mentor Commented:
U can use the api function, something I prefer cos environment variables can easily be spoofed

add this to a module

Private Declare Function GetUserName Lib "advapi32" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetMyUserID() As String

    Dim sBuffer As String
    Dim lSize As Long
    sBuffer = Space$(255)
    lSize = Len(sBuffer)
    If GetUserName(sBuffer, lSize) = 1 Then
        GetMyUserID = Left$(sBuffer, lstrlenW(StrPtr(sBuffer)))
        GetMyUserID = ""
    End If

End Function

Now just call GetMyUserID to get your windows logon id
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.