Inability to Capture Username

H

Henry Stockbridge

Hi,

I have created an install routine using a Userform that runs code
conditional to the Username. However, the procedure is not recognizing
the value of the strName variable.

================

Sub cmdNext_Click()
Dim strName as String
strName = Environ("Username")

If strName = "doej" Or strName = "doex" Then GoTo X_Only Else GoTo
Y_Only

X_Only:
' Code here.....

Y_Only:
' Code here...

End Sub

=================

Any help you can lend would be appreciated.

Henry
 
L

Leith Ross

Hello Henry,

The way you have have written your SUB the Y_Only routine will always
exceute. Try this and see if your problem goes away...

Sub cmdNext_Click()
Dim strName as String
strName = Environ("Username")

If strName = "doej" Or strName = "doex" Then
GoTo X_Only
Else
GoTo Y_Only
End If

'Any other code goes here...
Exit Sub

X_Only:
' Code here.....
Exit Sub

Y_Only:
' Code here...
Exit Sub

End Sub

Sincerely,
Leith Ross
 
H

Henry Stockbridge

Hi, Again.

Well, it appears the Environ() function is returning a zero-length
string, so I was going to use this code, instead. I am unsure how to
incorproate this into my Userform, though.

===============

'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If ( lngX > 0 ) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
'******************** Code End **************************

Any help you can offer would be appreciated.

Henry
 
D

Dave Peterson

Sub cmdNext_Click()
Dim strName as String
strName = fosusername

'then you may want to use select case here--instead of a bunch of if/then/elses.

select case lcase(strname)
case is = "doej", "doex"
'do something
case is = "somethingelse"
'do something else
case else
'do something completely different
end select

end sub

====
Put this stuff in a General module:

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If ( lngX > 0 ) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top