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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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.

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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