• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

Get User Name

I want to have a log file when a particular user accesses a file. I know how to make the log, but how can I get the current user name? All PCs here are using Win2K.

(This same question is in the archives, but I don't feel like paying for premium services just to look at one PAQ... hehe)

Thanks!
0
JFrye
Asked:
JFrye
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private Sub Form_Load()
    Dim strUserName As String

    strUserName = String(100, Chr$(0))
    GetUserName strUserName, 100
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    Debug.Print strUserName
End Sub
0
 
vinnyd79Commented:

Private Sub Form_Load()
MsgBox Environ("UserName")
End Sub
0
 
JFryeAuthor Commented:
Idle_Mind

I tried that, and just to make sure before I use it in the log, I made a MsgBox on Form_Load():

MsgBox "Welcome, " & strUserName & "!"

However, it never shows the user name... It only shows "Welcome, !"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Works great on my system.  I'm using Win ME.  Can you post the code so I can exactly how you used it?

Idle_Mind
0
 
JFryeAuthor Commented:
I copied everything exactly like you wrote it... The only thing I added was the MsgBox directly below the code.

Private Sub Form_Load()
    Dim strUserName As String

    strUserName = String(100, Chr$(0))
    GetUserName strUserName, 100
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    Debug.Print strUserName

    MsgBox "Welcome, " & strUserName & "!"
End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
=)

Did you put the API declaraion on the form?

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

Not to take away from your excellent code, but may I suggest a couple of changes:

1. Check the success or failure of the API.
2. Use the nSize parameter to get the length of the string returned, instead of using InStr.

Something like this:
Dim iBufSize As Long
Dim strUserName As String

iBufSize = 255
strUserName = Space$(iBufSize)
If GetUserName(strUserName , iBufSize) = 1 Then
   strUserName = Left$(strUserName, iBufSize - 1)
Else
   strUserName = "??????"
End If

Anthony
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Fair enough.  Tis sloppy code.  If you want to get REALLY correct however, the function returns zero if it fails, and a nonzero value for success.  Here are my suggested changes:

Dim iBufSize As Long
Dim strUserName As String

iBufSize = 255
strUserName = Space$(iBufSize)
If GetUserName(strUserName , iBufSize) <> 0 Then
   strUserName = Left$(strUserName, iBufSize - 1)
Else
   strUserName = "??????"
End If

=)
Idle_Mind
0
 
JFryeAuthor Commented:
Ok, I partially found out what went wrong... I placed the MsgBox in it's own function... this function was a template for the Log File function. I had:

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

Priavte Sub Form_Load()
    Dim iBufSize As Long
    Dim strUserName As String
   
iBufSize = 255
    strUserName = Space$(iBufSize)
    If GetUserName(strUserName, iBufSize) <> 0 Then
       strUserName = Left$(strUserName, iBufSize - 1)
    Else
       strUserName = "??????"
    End If
    fnLogUser
End Sub

Private Function fnLogUser()
    MsgBox "Welcome, " & strUserName & "!"
End Function

Why did the MsgBox popup "Welcome, !" when in it's own function, but when it was placed directly in the Form_Load it welcomed the correct user?
0
 
JFryeAuthor Commented:
Ok, I defined the variables as long/string globally, and it worked.

Public iBufSize As Long
Public strUserName As String

'Then the rest of the stuff...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now