Modify environ("USERNAME") code to show First name

mickeyshelley1
mickeyshelley1 used Ask the Experts™
on
I use the following function to obtain the name of the current user. txtAlias =Environ$(“USERNAME”)

Because our users log on as firstName  space  lastName  i.e.  John Smith and the value of txtAlias is FirstName and Last Name. this brings me to my question:

Is it possible to modify the code to get just the first name and then  modify it to retrieve only the last name, depending on the need

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
With split?

split(Environ$(“USERNAME”), " ")(0) 'First name
split(Environ$(“USERNAME”), " ")(1) ' Last name

Chris
Top Expert 2016

Commented:

better to use variables

vUsers=environ("USERNAME")

fName=left(vUsers,instr(vUsers, " ")-1)
lName=mid(vUsers,instr(vUsers, " ")+1)
Top Expert 2006
Commented:
do all your names have spaces in? if not then I would code defensive. Like Chris I would go for using the split approach

    Dim sFirstName As String
    Dim sLastName As String
    Dim sAlias() As String
   
    sAlias = Split(Environ("UserName"), " ")
    sFirstName = sAlias(0)
    If UBound(sAlias) > 0 Then
        sLastName = sAlias(1)
    End If

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems Analyst
Top Expert 2007

Commented:
"Because our users log on as firstName  space  lastName"
Humm, I either forgot or wasn't aware that you can use a Space in the Windows login ?

mx

Author

Commented:
i overlooked one word usernames, how could this be addressed to include this as a third possible value for txtAlias?

Author

Commented:
We are using 2008 Domain Controller and it is very liberal with usernames such as those using spaces.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems Analyst
Top Expert 2007

Commented:
I've never heard of spaces being allowing in usernames anywhere, on the web or elsewhere. Anyway, I'm only addressing the Envrion() issue as a heads up.

mx
Top Expert 2006

Commented:
Whatever you are doing regarding strings, if there is a possibility of one or two words then it would be a simple case of checking for existence otherwise assuming there are two words will fail in only one encountered.

When you use the split command with space as the delimiter, all words in between spaces are placed in an array. ubound represents the number of elements and that can be checked to see if there is more than one. You cannot use your variable txtAlias with a split as it has to be a string array. However, you can use it to set when reading individual elements from the array.
Top Expert 2006

Commented:
proper meaning of ubound - it returns the last subscript of the array - which effectively is like determining the number of elements

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial