MS Access 2003. environ("username") returning empty string

drkness

New member
Local time
Today, 17:40
Joined
Oct 12, 2010
Messages
3
Hi all,

I work at a quite large a company with domain users.
I'm developing access databases that rely on the environ("username") variable.
I got a new computer and on this one, environ("username") returns an empty string!
Could someone please help with this.
This works fine on my 2nd desktop computer. i am confused.
When i run a loop of for i = 1 to say 33 and check each environ(i), and compare the output in the two computers, the environment "USERDOMAIN" and "USERNAME" is totally missing from the 'problem' computer.

Any ideas?

thanks in advance.
 
I'm currently using the environment("username") variable but if there is a better/safer way I would definitely be interested in changing it.

Galaxiom - I have looked at the thread you provided a link to but cannot see any example? I would be grateful if you could give me the example
 
Last edited:
'/API's
Code:
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Function GetComputerName Lib "Kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Code:
Function FindUserName() As String
    ' This procedure uses the Win32API function GetUserName
    ' to return the name of the user currently logged on to
    ' this machine. The Declare statement for the API function
    ' is located in the Declarations section of this module.
   
    Dim strBuffer As String
    Dim lngSize As Long
        
    strBuffer = String(100, " ")
    lngSize = Len(strBuffer)
    
       
    strBuffer = String(100, " ")
    lngSize = Len(strBuffer)
    
    If GetUserName(strBuffer, lngSize) = 1 Then
        FindUserName = Left(strBuffer, lngSize)
    Else
        FindUserName = "User Name not available"
    End If

 End Function

Code:
Public Function FindComputerName()
    Dim strBuffer As String
    Dim lngSize As Long
        
    strBuffer = String(100, " ")
    lngSize = Len(strBuffer)

    If GetComputerName(strBuffer, lngSize) = 1 Then
        FindComputerName = Left(strBuffer, lngSize)
    Else
        FindComputerName = "Computer Name not available"
    End If
    
End Function

WhoAmI = FindUserName()
WhereAmI = FindComputerName()
 
Hi all,

I work at a quite large a company with domain users.
I'm developing access databases that rely on the environ("username") variable.
I got a new computer and on this one, environ("username") returns an empty string!
Could someone please help with this.
This works fine on my 2nd desktop computer. i am confused.
When i run a loop of for i = 1 to say 33 and check each environ(i), and compare the output in the two computers, the environment "USERDOMAIN" and "USERNAME" is totally missing from the 'problem' computer.

Any ideas?

thanks in advance.

that is strange though

my standalone picks up values for those two (environ keys 30 and 31) - but maybe if you try to join a netwrk and fail, it may affect the values. Is that possible?

other points

1) Any other environ settings look strange
2) you havent redefined a function called environ have you?

also check the environ settings on first startup - and then see if they change within your app.


GL
 
Thanks for the reply!

I have no problems logging into the network.

I login using the same credentials into both the computers.

I've checked with everyone else who got the new laptops, they're environ("username") returns their username fine.

I haven't redefined environ function.

That IS strange. the only other solution would be to try n rebuild the pc n hope for the best but that takes an entire day 'round here, n we'll never get to the root of this problem.

Pardon the ignorance, how do u check environment variables after startup? using the SET command at cmd?
 
Just use the set command in a command window with no argument to return the current value of the environment variable.

SET Username

It should return:
USERNAME=value
 
you were correct.

SET in cmd does not show any USERNAME variable. nor USERDOMAIN

Curiously, after setting the USERNAME in the promt by typing

SET USERNAME = username

MS Access still returns it empty on ?environ("USERNAME")

There's a disconnect somewhere.
 
drk,

not sure if this is the computer username, but I ran across this one time:
Code:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Module
Public Function UserName() As String
    Dim cn As String
    Dim ls As Long
    Dim res As Long

    cn = String(1024, 0)
    ls = 1024
    res = GetUserName(cn, ls)
    If res <> 0 Then
        UserName = Mid(cn, 1, InStr(cn, Chr(0)) - 1)
    Else
        UserName = ""
    End If
End Function
it may be a substitute for the environ function. I believe it works the same, but don't quote me. :)
 
The XP gui for overwriting the default environment variables is in System Properties, Advanced, Environment Variables.
 

Users who are viewing this thread

Back
Top Bottom