[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Get User Name

Posted on 2003-11-14
10
Medium Priority
?
455 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
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.

 
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 60 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

656 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