Solved API GetUserName (1 Viewer)

dgreen

Member
Local time
Today, 11:47
Joined
Sep 30, 2018
Messages
397
Just had a new person attempt to get into the database and the code failed when it tried to obtain the value from FOSUserName(). If it matters, I noticed is the name appeared as "Owner"

It's possible their computer didn't have a value populated when it was issued out, so where and how does the computer store the strUserName value? How would you change / update it?

Code:
Option Compare Database
Option Explicit

Private Declare Function apiGetUserName _
        Lib "advapi32.dll" Alias "GetUserNameA" _
        (ByVal lpBuffer As String, nSize As Long) As Long

'-------------------------------------------------
' This code is used to retrieve the network user
' name by accessing the API apiGetUserName.
' Created by: Unknown (Found on Dev Ashish
' web site www.mvps.org/access/)
' This code has not been altered in anyway.
' Added to database: 27 dec 1999
' Added by: Richard Rensel
'-------------------------------------------------

Function fOSUserName() As String
    On Error GoTo fOSUserName_Err
    
    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
    
fOSUserName_Exit:
    Exit Function
    
fOSUserName_Err:
    MsgBox Error$
    Resume fOSUserName_Exit
End Function
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:47
Joined
Oct 29, 2018
Messages
21,468
Hi. I think that API pulls the "network" username. If it doesn't exist, then it probably defaults to the user's profile. What do you get if you tried?

Environ("Username")
 

Micron

AWF VIP
Local time
Today, 12:47
Joined
Oct 20, 2018
Messages
3,478
I use that function pretty much exclusively and I believe it gets the windows user login name as the code comments say. If it's possible to have an account with a login name of "owner" then I guess that's what you'll get. You're not using this in a 64 bit Windows environment by chance? Maybe you should modify that code to deal with accounts that are either 32 or 64 bit.
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:47
Joined
Sep 21, 2011
Messages
14,270
Is this the same user who did not get the shortcut created correctly?
 

dgreen

Member
Local time
Today, 11:47
Joined
Sep 30, 2018
Messages
397
Environ("Username")

Same result "OWNER"

Is this the same user who did not get the shortcut created correctly?
Different user.

We looked at the User's Account and it shows their First and Last Name populated connected to the company's email account (just like mine).

Where else might this value be stored?
 

dgreen

Member
Local time
Today, 11:47
Joined
Sep 30, 2018
Messages
397
How would I check to see what environment their computer is operating on?

Press and hold the Windows Key and the Pause key. In the System window, next to System type it will list 32-bit Operating System for a 32-bit version of Windows, and 64-bit Operating System if you're running the 64-bit version.

Mine is a 64-bit operating system with Access operating on 32-bit.
I have a due out from the user.

You're not using this in a 64 bit Windows environment by chance?
 

sonic8

AWF VIP
Local time
Today, 18:47
Joined
Oct 27, 2015
Messages
998
We looked at the User's Account and it shows their First and Last Name populated connected to the company's email account (just like mine).
Where did you look at the user's account?
From the info you provided I think it is without doubt the user is logged in to that computer with the username "Owner".
Maybe the user is logging in locally and not into your domain?
 

dgreen

Member
Local time
Today, 11:47
Joined
Sep 30, 2018
Messages
397
Maybe some additional information on how users access the database?

User logs into their laptop.
User opens Access database front end.
User click a log-in button that goes to a SharePoint site where all the linked SharePoint Lists are stored.
User types in their SharePoint password. {The user has access to the site and can log-in, since it's also the same location, I store the most current front end)
All linked files are connected and they can open tables, run queries, open forms and populate, etc...

-----------------
I was thinking that the Owner value was coming from the User's Account (https://www.computerhope.com/issues/ch000767.htm). We opened Control Panel > User Accounts > User Accounts > Manage Accounts > Change An Account to see what values were there but it didn't show Owner.

-------
Below is what the log in process is capturing. The UserName value is the same whether I use fosUserName or Environ("UserName")
1585678707515.png
 

dgreen

Member
Local time
Today, 11:47
Joined
Sep 30, 2018
Messages
397
Maybe a little closer.... We had the user open their c:\Users\. On my computer it shows my name, but not on this person's computer.
1585678931987.png
 
Last edited:

sonic8

AWF VIP
Local time
Today, 18:47
Joined
Oct 27, 2015
Messages
998
I was thinking that the Owner value was coming from the User's Account (https://www.computerhope.com/issues/ch000767.htm). We opened Control Panel > User Accounts > User Accounts > Manage Accounts > Change An Account to see what values were there but it didn't show Owner.
It is coming from the user's account. - "Manage Accounts" only shows the pretty display name of the user account but not the actual user name.
You need to go to: Control Panel - Administrative Tools - Computer Management - Local Users and Groups - Users
There you'll find the "Owner".
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:47
Joined
Sep 21, 2011
Messages
14,270
Open a command window.
Type in 'set' (without the quotes)

What does USERNAME= show ?
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:47
Joined
Sep 21, 2011
Messages
14,270
Where did you look at the user's account?
From the info you provided I think it is without doubt the user is logged in to that computer with the username "Owner".
Maybe the user is logging in locally and not into your domain?
That sounds like what is happening
Have you actually watched them logon.?.
 

Micron

AWF VIP
Local time
Today, 12:47
Joined
Oct 20, 2018
Messages
3,478
BTW, I mis-spoke (typed?). IIRC, what matters with respect to the bitness is the Office/Access version, not the operating system. Sorry.
 

HiTechCoach

Well-known member
Local time
Today, 11:47
Joined
Mar 6, 2006
Messages
4,357
I have seen lots of laptops and desktops that were set up by en- users, not IT people, have the default account name, owner.

Since not all my client use Active directory, I store both the OS user name and the OS Computer name.

I use the same system lib (advapi32.dll) API and have never had it fail. If with the user account name "owner".

What indicated thre was a failure in your code? Was thre an error message?
 

dgreen

Member
Local time
Today, 11:47
Joined
Sep 30, 2018
Messages
397
@sonic8 See below. The Full Name populates with the user's actual name (blue text). Where is that value stored and how might it be pulled from the computer?

Control Panel - Administrative Tools - Computer Management - Local Users and Groups - Users
There you'll find the "Owner".

1585750247971.png


@HiTechCoach I am able to pull the Computer name out so if push comes to shove, I am tracking your technique. This user got a new hard drive for their laptop and did the install themselves as opposed to others in the company that received a standardized process.

I have seen lots of laptops and desktops that were set up by en- users, not IT people, have the default account name, owner.
Since not all my client use Active directory, I store both the OS user name and the OS Computer name.
I use the same system lib (advapi32.dll) API and have never had it fail. If with the user account name "owner".

@HiTechCoach This request for support string was triggered when I had a new user try and log into the database. The vba code welcomes the user by their first name to the database and tells them to log-in. The code stopped on the FOSUserName portion of the code. It's good that this happened because now I realize that I can't just depend on the UserName field to be unique.

Code:
Zira.Speak "" & GetGreeting() & Mid(fOSUserName(), 1, InStr(1, fOSUserName(), " ") - 1)
 

Micron

AWF VIP
Local time
Today, 12:47
Joined
Oct 20, 2018
Messages
3,478
I can't just depend on the UserName field to be unique.
Ahh, so each person gets to pick their own? Ours were assigned because of the scope of their use. It became part of your company email address, network login, and user profile name for all company wide apps. There were no two the same out of thousands.
 

bastanu

AWF VIP
Local time
Today, 09:47
Joined
Apr 13, 2010
Messages
1,402
See it the attached module works for you, it has a function that should return the full name.

Cheer,
Vlad
 

Attachments

  • modUserName.zip
    1.9 KB · Views: 408

Gasman

Enthusiastic Amateur
Local time
Today, 17:47
Joined
Sep 21, 2011
Messages
14,270
Everywhere I have ever worked the login name was always unique.? In LBG it was even just a set of numbers.?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:47
Joined
Oct 29, 2018
Messages
21,468
Everywhere I have ever worked the login name was always unique.? In LBG it was even just a set of numbers.?
Just a wild thought, but could this be a case of a peer-to-peer network?
 

Users who are viewing this thread

Top Bottom