Solved

Get User Name

Posted on 2003-11-14
10
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 86

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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 86

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

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 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 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