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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

=Environ("Username") returns #Name?

Status
Not open for further replies.

DataEmbryo

Programmer
Jan 21, 2004
12
US
I've tried all the fixes I could find here and on MS, but I still get this error. I checked that the environment variable "Username" is set. I have two computers, both running Windows 2000 and Access 2000. It works on one, but not the other...

I have =Environ("Username") in a forms and reports.

Any ideas?

Thank you.
 
Go to the command prompt on the nonworking computer and type

SET<ENTER>

and see if you can find a USERNAME environment variable defined. If not, then hmm.

Also, on the nonworking computer, you may just be having references problems and you only NOTICE the difference on the Environ() function. Press CTRL-G (on the nonworking computer again), go to Tools->References and see if anything is MISSING.


Also, you can get the username a bunch of different ways. I wrote a FAQ about this:

faq181-3779 Getting all types of Usernames in Access
 
Thank you for your reply.... I'm still struggling. :(

I checked that the environment variable exists. I even changed it on the working computer to 'bob'. I was surprised to see that when I re-opened the db, my username was displayed instead of 'bob'.

I checked for missing references, but there are non. I also verified that they are in the correct order...

I tried the module below and got the same error...

Option Explicit

'source: Public Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long

Const NoError = 0 'The Function call was successful

Public Function apiNetUserName() As String

' Buffer size for the return string.
Const lpnLength As Integer = 255
' Get return buffer space.
Dim status As Integer
' For getting user information.
Dim lpName, lpUserName As String
' Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)
' Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)
' See whether error occurred.
If status = NoError Then
' This line removes the null character. Strings in C are null-
' terminated. Strings in Visual Basic are not null-terminated.
' The null character must be removed from the C strings to be used
' cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
Else
' An error occurred.
lpUserName = ""
End
End If
FindNetUserName = lpUserName
End Function


'-----------------stop here---------------


Thanks again for any help you can offer.
 
Then this is probably a references error. This is a widely-encountered (and rarely-understood) problem. The solution is as I listed above:


---
Also, on the nonworking computer, you may just be having references problems and you only NOTICE the difference on the Environ() function. Press CTRL-G (on the nonworking computer again), go to Tools->References and see if anything is MISSING.
---


It's hard to explain this in any full manner, but similar threads are all over the place (search here or Google). Basically because one DLL is missing on the computer, so ALL of the functions break, whether they're connected to that DLL or not. So do the above thing to fix it--remove any MISSING: references.
 
I am not a big fan of global variables, but this might be a case when to use it.
[tt]
Public MyCurrentUser As String[/tt]

Then on your main menu, load event
[tt]
MyCurrentUser = Environ("Username") [/tt]

If this does not work, then I would have to suspect there is a reference problem.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top