Solved

Get User Name

Posted on 2003-11-14
10
445 Views
Last Modified: 2010-05-01
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
Comment
Question by:JFrye
10 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9751566
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
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9751591

Private Sub Form_Load()
MsgBox Environ("UserName")
End Sub
0
 

Author Comment

by:JFrye
ID: 9751618
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9751771
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
 

Author Comment

by:JFrye
ID: 9751855
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9751889
=)

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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9752856
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 20 total points
ID: 9753188
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
 

Author Comment

by:JFrye
ID: 9753864
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
 

Author Comment

by:JFrye
ID: 9753910
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now