Hi zL -
This method works. I have used it in an NT environment, also Win 95/98.
I have received alot of help from members of this forum. It feels good to
be able to contribute something to someone else.
John
just a little program with a simple form & button you can use to display
the user name....
Private Sub Command1_Click()
gblUserID = ClipNull(GetUser())
MsgBox "Your user name is " & gblUserID, vbOKOnly
End Sub
____________________________________________
Put this in a VB MODULE:
Option Explicit
' ---------------------------------------------------------------------------------
' John Bates November 2000
' This module retrieves the Windows User ID for the user currently logged on.
' To call: gblUserID = ClipNull(GetUser())
' The users name/ID will be returned in the global variable gblUserID
' ---------------------------------------------------------------------------------
Global gblUserID As String
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal IpBuffer As String, nSize As Long) As Long
Function ClipNull(InString As String) As String
Dim intpos As Integer
If Len(InString) Then
intpos = InStr(InString, vbNullChar)
If intpos > 0 Then
ClipNull = Left(InString, intpos - 1)
Else
ClipNull = InString
End If
End If
End Function
Function GetUser() As String
Dim IpUserID As String
Dim nBuffer As Long
Dim Ret As Long
IpUserID = String(25, 0)
nBuffer = 25
Ret = GetUserName(IpUserID, nBuffer)
If Ret Then
GetUser$ = IpUserID$
End If
End Function