Solved

Getting windows login name

Posted on 2007-04-07
4
156 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]
Comment Utility
what do you mean with "is not matching" ?
can you explain that.
0
 

Author Comment

by:sgs1970
Comment Utility

 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

762 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

9 Experts available now in Live!

Get 1:1 Help Now