Solved

Getting windows login name

Posted on 2007-04-07
4
158 Views
Last Modified: 2010-04-30
Hi,
  For getting the windows login user name as a string , what is to be done ?
 I actually used GetEnvironmentVariable, but the value coming when compared to a string variable is not matching

    Thanks,
      Sam
0
Comment
Question by:sgs1970
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18869335
what do you mean with "is not matching" ?
can you explain that.
0
 

Author Comment

by:sgs1970
ID: 18869336

 Hi,
  See the subroutine I used

 
Function GetUserName() As String
    Dim strUserName As String * 255
    strUserName = ""
    'Create a string buffer of 255 chars in length
    Dim X As Integer
    X = GetEnvironmentVariable("USERNAME", strUserName, Len(strUserName))
    If X > 0 Then        'Look for Null Character, usually included
     X = InStr(strUserName, vbNullChar)
    'Trim off buffered spaces too
        If X > 0 Then
   
         Else
            ''  MsgBox (Left$(strUserName, x))
        End If
    End If

   GetUserName = strUserName
End Function
0
 

Author Comment

by:sgs1970
ID: 18869338
The username is not read as a  proper string , it seems
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 18869341
yes, the API returns a null-terminated string (comes from C programming).
you have to do a left() on the returned string, if the vbNullchar is found:

Function GetUserName() As String
    Dim strUserName As String * 255
    strUserName = ""
    'Create a string buffer of 255 chars in length
    Dim X As Integer
    X = GetEnvironmentVariable("USERNAME", strUserName, Len(strUserName))
    If X > 0 Then        'Look for Null Character, usually included
     X = InStr(strUserName, vbNullChar)
    'Trim off buffered spaces too
        If X > 0 Then
            strUserName = left( strUsername, x-1)
         Else
            ''  MsgBox (Left$(strUserName, x))
        End If
    End If

   GetUserName = strUserName
End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

896 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