Results 1 to 4 of 4

Thread: Cant get username

  1. #1

    Thread Starter
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250

    Cant get username

    Hi, am trying to fill a text box with the current windows username in Access 2000, but im having trouble.

    So far im using this..
    Code:
    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Private Function CurrentUser() As String
        Dim strBuff As String * 255
        Dim x As Long
        CurrentUser = ""
        x = GetUserName(strBuff, Len(strBuff) - 1)
        If x > 0 Then
            x = InStr(strBuff, vbNullChar)
            If x > 0 Then
                CurrentUser = UCase(Left$(strBuff, x - 1))
            End If
        End If
    End Function
    And on my form, I have a text box with 'control source' set to =CurrentUser.

    Problem is this returns 'Admin', which I assume is what im logged into the DB as. But i want the windows username, not this.

    Anyone help please?

    lee
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Cant get username

    Call the function with a msgbox. I just did and got the windows user name.

  3. #3
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Cant get username

    Quote Originally Posted by Lee_S
    Problem is this returns 'Admin', which I assume is what im logged into the DB as. But i want the windows username, not this.

    Anyone help please?

    lee

    That does return the Current Windows Username. For me it Returns Ryan Jones as that is my Windows Loggin name and, my Office Name if RJones so it does work.

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Cant get username

    I wrote that code and it does return the current logged user username . The problem is that the
    function call is conflicting with the Access Property "CurrentUser" which returns the Access logged
    in user - "Admin" usually.

    Change the function call to something other then CurrentUser.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width