General discussion


User name VB

By Ravenhawk ·
I have a user who is attempting to put together a vb script that collects the name of the user logged into a system when they open up a specific file located on a network drive. Currently he is scraping the name based on the name entered in the application (Office), which doesn't always reflect the truth or match the username known by the system. He was thinking of getting information from the registry, but may not be able to access it directly. Any thoughts?

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

by Miss Kitty In reply to User name VB

Here's what I'm using to get the username in my VB apps:

Public strUserName As String

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function UserName() As String
Dim lpBuffer As String
Dim j
lpBuffer = Space$(255)
GetUserName lpBuffer, Len(lpBuffer)
j = InStr(lpBuffer, Chr$(0))
If j > 0 Then UserName = Left$(lpBuffer, j - 1)
End Function

Private Sub Form_Load()
Dim strString As String
strString = UserName
tblName = Replace(strString, ".", "", 1)
GetFileName = tblName
End Sub

When I need to show the username I have it stored in GetFileName.


Collapse -

by Ravenhawk In reply to

Poster rated this answer.

Collapse -

by bschaettle In reply to User name VB

Here's a solution that uses Windows Script Host.

Public Function GetNetworkUserName() As String
Dim sNetworkUsername As String
Dim wshNetwork As Object
On Error GoTo LeaveFunction
sNetworkUsername = "" ' default
Set wshNetwork = CreateObject("WScript.Network")
sNetworkUsername = wshNetwork.UserName
Set wshNetwork = Nothing
GetNetworkUserName = sNetworkUsername
End Function ' GetNetworkUserName

Collapse -

by Ravenhawk In reply to

Poster rated this answer.

Collapse -

by Ravenhawk In reply to User name VB

This question was closed by the author

Related Discussions

Related Forums