Solved

Get User Name

Posted on 2003-11-14
10
452 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
Industry Leaders: 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!

 
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

Technology Partners: 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!

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

739 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