Results 1 to 15 of 15
  1. #1
    Join Date
    Nov 2002
    Location
    Oakland, CA
    Posts
    126

    Unanswered: Windows user name in Access

    Is there a way to use current Windows user name or a computer name (or both) in my access. I have people logging into my db so I can just use their login in multiple reports. I could avoid them loging in if I could just use the Windows user name. Any thoughts? Thanks

  2. #2
    Join Date
    Apr 2003
    Location
    Evansville, Indiana
    Posts
    76

    Re: Windows user name in Access

    Hi.

    I've had the need to do this in the past for a variety of reasons...however, not recently. The code I have for this worked for Windows NT 4.0 WS, and I don't know if it still works on newer OSs. But see if this works for you:
    Code:
    Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
        "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Function fOSUserName() As String ' Returns the network login name
        Dim lngLen As Long, lngX As Long
        Dim strUserName As String
        
        strUserName = String$(254, 0)
        lngLen = 255
        lngX = apiGetUserName(strUserName, lngLen)
        If lngX <> 0 Then
            fOSUserName = Left$(strUserName, lngLen - 1)
        Else
            fOSUserName = ""
        End If
    End Function
    You can put this code in a VBA module in Access, and then check the returned value from the fOSUserName() Function.

    HTH!

    Kael MCSD, A+

  3. #3
    Join Date
    Jun 2003
    Location
    UK
    Posts
    68
    You could also try Environ(UserName)

    Paul

  4. #4
    Join Date
    Nov 2002
    Location
    Oakland, CA
    Posts
    126
    Thanks both of you but neither works. the Environ(UserName) gives me an error of undeclared variable for UserName in other words that option doesn't even exist. Or am I wrong?

  5. #5
    Join Date
    Feb 2004
    Posts
    10
    Here is a function I have used in the past:

    Option Compare Database
    Option Explicit

    Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Public Function GetUser() As String

    On Error GoTo ErrorRoutineErr

    Dim st As String
    Dim slCnt As Long
    Dim slDL As Long
    Dim slPos As Single
    Dim slUserName As String
    Dim txtUsername As String

    'Get the users name
    slCnt = 199
    st = String(200, 0)
    slDL = GetUserName(st, slCnt)
    slUserName = Left(st, slCnt) & slCnt
    slPos = InStr(1, slUserName, Chr(0))
    If slPos > 0 Then
    txtUsername = Left(slUserName, slPos - 1)
    GetUser = txtUsername
    Else
    GetUser = ""
    End If

    ErrorRoutineResume:
    Exit Function
    ErrorRoutineErr:
    MsgBox "Stats.Form1.UserName" & Err & Error()
    GetUser = Str(Err)
    Resume ErrorRoutineResume
    End Function


    Then on a form I set the Default value for the table field to:
    =GetUser()

    Works greate for me in Access 97 & 2000 gettin the users account ID.

    As I do not display this, the users do not see that I am capturing this.

    HTH!

  6. #6
    Join Date
    Nov 2002
    Location
    Oakland, CA
    Posts
    126
    BEAUTY, WORKS GREAT, THANKS

  7. #7
    Join Date
    Oct 2010
    Posts
    6

    Public Function

    This looks like exactly what I am looking for but I am having difficulty putting this function to work. How do I define a public function?

    Thanks for the help

  8. #8
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    put the code into a code module
    declare the function as public

    theres other functions to discover the computer ID.. do a google for "dev ashish api" if you are interested
    I'd rather be riding on the Tiger 800 or the Norton

  9. #9
    Join Date
    Feb 2004
    Posts
    10

    Create a new Module

    To create the public function, you go to Modules & then create a new one, copy & paste the code.

    Save & compile the module.

    You can then use that function just like you would any other Access functions.

  10. #10
    Join Date
    Sep 2010
    Posts
    31
    its environ("USERNAME") with quotes. that why you got the variable error

    or environ("COMPUTERNAME")

  11. #11
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Quote Originally Posted by ronosh View Post
    its environ("USERNAME") with quotes. that why you got the variable error

    or environ("COMPUTERNAME")
    don't use environ
    its not dependable
    its not relaible
    its easy to spoof

    use the network API calls.
    I'd rather be riding on the Tiger 800 or the Norton

  12. #12
    Join Date
    Sep 2010
    Posts
    31
    Quote Originally Posted by healdem View Post
    don't use environ
    its not dependable
    its not relaible
    its easy to spoof

    use the network API calls.
    Can you elaborate on the dependability/reliability?

  13. #13
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    environ is allocated as required
    if you are short of resources it isn't set or set paritally
    if you turn the computer on without authenticating against the network it isn't correct
    it can be spoofed, ie malicious code can set the environ values.
    some environ values re always set, some depend on free resources.

    so as their is a reliable dependable mechanism.. the network API's use 'em.
    if you want to waste hours trying to track down errant bugs then feel free to use environ
    I'd rather be riding on the Tiger 800 or the Norton

  14. #14
    Join Date
    Sep 2010
    Posts
    31
    Okay, I'm getting on board here.

    So I can see how to use Getuser(). Can I use this to see where my applications dirrectory without using the Environ("APPDATA")

    Can this code be modified to get the COMPUTERNAME?

    Also, can I assume, and I suspect so that "advapi32.dll" exists on all machines running access, including runtime?

    So many questions, so little time

  15. #15
    Join Date
    Sep 2010
    Posts
    31
    Found the COMPUTERNAME API

    modComputerName

    Option Compare Database


    ' This code was originally written by Dev Ashish.
    ' It is not to be altered or distributed,
    ' except as part of an application.
    ' You are free to use it in any application,
    ' provided the copyright notice is left unchanged.
    '
    ' Code Courtesy of Dev Ashish


    Private Declare Function apiGetComputerName Lib "kernel32" Alias _
    "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Function fOSMachineName() As String

    'Returns the computername
    Dim lngLen As Long, lngX As Long
    Dim strCompName As String
    lngLen = 16
    strCompName = String$(lngLen, 0)
    lngX = apiGetComputerName(strCompName, lngLen)
    If lngX <> 0 Then
    fOSMachineName = Left$(strCompName, lngLen)
    Else
    fOSMachineName = ""
    End If
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •