Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

environ("UserName") is not working on new computer

Status
Not open for further replies.

hengsin

Technical User
Mar 26, 2003
128
0
0
MY
environ("UserName") is working on my computer. However, when my friend buy a new computer and copy the MS Access file i developed but it's not working in his computer. I go to Tools->Reference to check maybe his computer is missing any dll or ocx files. But he have all the files in his computer.

THe error message is stated that Environ("UserName") is having some problem.

Any idea on this problem?
 
The Environ variables are in actual fact batch file SET variables. To view them all, open a command/DOS window and type SET followed by return. This will list them all. In the same DOS window, you can create your own by typing SET VariableName=Value.

The UserName variable is created when you log into your computer/network. If you don't log in, it won't get created (or will be blank). I can't remember if Windows 9x created the value at all and haven't any machines left to try it out on. I hope this helps.
 
hen,
Use the below declare and function instead, it's more accurate, since the SET can be so easily overridden by a user not wanting to be identified.

Code:
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetUser() As String
Dim l As Long, sUser As String
On Error Resume Next 'don't let this stop things up
sUser = Space$(255)
l = GetUserName(sUser, 255) 'call the api

If l <> 0 Then
'strip null terminator
sUser = Left(sUser, InStr(sUser, Chr(0)) - 1)
If Len(sUser) > 50 Then
sUser = Left$(sUser, 50)
End If
GetUser = sUser
Else
GetUser = &quot;<Not Found>&quot;
End If
End Function

-jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top