Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the NT UserName with VB 1

Status
Not open for further replies.

davo

Technical User
Aug 7, 2000
29
US
What is the code for a VBA macro that gets the users name when the Access DB is on Win NT4.0?<br><br>This has been answered before I have lost the thread.<br><br>Sorry.
 
Here's the code.&nbsp;&nbsp;I got it from MS KB article #161394<br><br><A HREF=" TARGET="_new"> Code****<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Declare for call to mpr.dll.<br>&nbsp;&nbsp;&nbsp;Declare Function WNetGetUser Lib &quot;mpr.dll&quot; _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Alias &quot;WNetGetUserA&quot; (ByVal lpName As String, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ByVal lpUserName As String, lpnLength As Long) As Long<br><br>&nbsp;&nbsp;&nbsp;Const NoError = 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'The Function call was successful<br><br>&nbsp;&nbsp;&nbsp;Sub GetUserName()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Buffer size for the return string.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Const lpnLength As Integer = 255<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Get return buffer space.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim status As Integer<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' For getting user information.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim lpName, lpUserName As String<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Assign the buffer size constant to lpUserName.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpUserName = Space$(lpnLength + 1)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Get the log-on name of the person using product.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = WNetGetUser(lpName, lpUserName, lpnLength)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' See whether error occurred.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If status = NoError Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' This line removes the null character. Strings in C are null-<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' terminated. Strings in Visual Basic are not null-terminated.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' The null character must be removed from the C strings to be used<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' cleanly in Visual Basic.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Else<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' An error occurred.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Unable to get the name.&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Display the name of the person logged on to the machine.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;The person logged on this machine is: &quot; & lpUserName<br><br>&nbsp;&nbsp;&nbsp;End Sub <br><br>*****End Code*****<br><br>I know that I have used variations on this successfully.&nbsp;&nbsp;I think you will probably need to do further string manipulations of the result; as I remember the result has some funky characters at the end.<br><br>Good luck<br><br>Kathryn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top