Get User name from  NT WorkSt. 4.0

Posted on 1998-06-16
Last Modified: 2010-05-03
Using Access97/VBA5 with or without Access security activated, I need (programmatically)the Logon name of each user for a Peek-to-Peer NTWorkStation 4.0 configuation with up to 10 users. The object is to save the user from logging on twice, once to NT and again to Access.
Question by:mrl
  • 8
  • 2

Author Comment

ID: 1463445
Edited text of question

Author Comment

ID: 1463446
Edited text of question

Accepted Solution

tward earned 150 total points
ID: 1463447
The GetUserName API call should be able to get the Logon Name for Windows 95/NT:

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

The GetUserName function retrieves the user name of the current thread. This is the name of the user currently logged onto the system.
BOOL GetUserName(
    LPTSTR lpBuffer,      // address of name buffer
    LPDWORD nSize       // address of size of name buffer
Points to the buffer to receive the null-terminated string containing the user's logon name. If this buffer is not large enough to contain the entire user name, the function fails.
Pointer to a DWORD that, on input, specifies the maximum size, in characters, of the buffer specified by the lpBuffer parameter. If this buffer is not large enough to contain the entire user name, the function fails. If the function succeeds, it will place the number of characters copied to the buffer into the DWORD that nSize points to.
Return Values
If the function succeeds, the return value is TRUE, and the variable pointed to by nSize contains the number of characters copied to the buffer specified by lpBuffer, including the terminating null character.
If the function fails, the return value is FALSE. To get extended error information, call GetLastError.
If the current thread is impersonating another client, the GetUserName function returns the user name of the client that the thread is impersonating.


Author Comment

ID: 1463448
I am an experienced programmer but inexperienced with Win API so please be tutorial in your response. I have no references to NT API documentation so hints as to how to use the recommended functions will be necessary. Thank you in advance. MRL
Email is

Author Comment

ID: 1463449
Whow! I hadn't even finished typing my comments and there was an answer!!!! This forem is fantastic. This is probably a commonly asked question although I did not see it in the PAQ list.  I need a few minutes to review the answer to see if I understand.
Thank you.
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


Expert Comment

ID: 1463450
I do think I have answered this one before, but I hate going through the PAQ unless there is a search through them that I have missed.

Just in case here is an example of using it within VB:

Dim Size As Long
Dim UserName As String
Size = 255

UserName = String(Size, 0)

Call GetUserName(UserName, Size)

UserName = Trim(Left(UserName, Size - 1))

Author Comment

ID: 1463451
To tward:
Have read your answer and have lots of techical questions about how I use the parameters in the call with Access/VBA5.0. I will consult the language references to see how I can get these values in and out  of this language. i.e., how do I define the lpbuffer, nsize etc or is it as simple as
Dim lpbuffer long
Dim nSize double

Author Comment

ID: 1463452
Once again your answer preceded my question...Are you clarvoyant or just a genius? Please Stand by while I look at your answer.

Author Comment

ID: 1463453
As I said, I haven't used API calls before so I'm unclear as to the exact way I declare the function in VB but this is obviously an exercise for the student. I will release this (or do whatever I have to do...this is my very first visit to this site) so you receive your appropriate reward. Thank you so much for such timely information. MRL

Author Comment

ID: 1463454
Anyone reading this thread should understand the time element involved...the answer from the expert arrived on my screen before appended my first comment and susequent clarifications followed just as rapidly...this is the greatest site I have ever found on the net!!! Experts site is not misnamed.

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now