Manufacturing Information Solutions Forum Index Manufacturing Information Solutions
Your Place for Support and Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Retrieve user name from Windows in Access 2003/XP/2000/97

 
Post new topic   Reply to topic    Manufacturing Information Solutions Forum Index -> Microsoft Access
View previous topic :: View next topic  
Author Message
BIGmiralli
Frequent Poster


Joined: 17 Apr 2007
Posts: 38
Location: Boston, Massachusetts

PostPosted: Fri May 23, 2008 8:54 am    Post subject: Retrieve user name from Windows in Access 2003/XP/2000/97 Reply with quote

Question: In Access 2003/XP/2000/97, how can I retrieve the name of the user logged into Windows?

Answer: To retrieve the user name from Windows, create a new Module in Access.

Paste the following code into the Module:


Code:
Declare Function WNetGetUser Lib "mpr.dll" _
     Alias "WNetGetUserA" (ByVal lpName As String, _
     ByVal lpUserName As String, lpnLength As Long) As Long

Const NoError = 0                    'The Function call was successful

Function GetUserName() As String

     Dim LUserName As String
     Const lpnLength As Integer = 255
     Dim status As Integer
     Dim lpName

     ' Assign the buffer size constant to lpUserName.
     LUserName = Space$(lpnLength + 1)

     ' Get the log-on name of the person using product.
     status = WNetGetUser(lpName, LUserName, lpnLength)

     ' See whether error occurred.
     If status = NoError Then
          ' This line removes the null character. Strings in C are null-
          ' terminated. Strings in Visual Basic are not null-terminated.
          ' The null character must be removed from the C strings to be used
          ' cleanly in Visual Basic.
          LUserName = Left$(LUserName, InStr(LUserName, Chr(0)) - 1)

     Else
          ' An error occurred.
          MsgBox "Unable to get the name."
          End
     End If

     GetUserName = LUserName

End Function

________
HONDA RC113
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Manufacturing Information Solutions Forum Index -> Microsoft Access All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group