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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you capture user ID after user logs in or starts VB application 1

Status
Not open for further replies.

zl

Programmer
Apr 20, 2001
3
US
Hello! I am trying to write a routine to capture user ID as they start up an application. I am programming in a network environment, so I should be able to capture the user's ID using the sessions variable. Is this correct? If so, please provide example of the code. Thanks. ZL

 
If you are referring to the login name, see: faq222-429
 
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
 
hi
it doesn't seem to work in for windows 2000 though,,, any ideas?
 
try debug.print environ("USERNAME")

You'll be amazed.

Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top