Username =Environ("username") (1 Viewer)

Haynesey

Registered User.
Local time
Today, 23:20
Joined
Dec 19, 2001
Messages
190
Hi,

On our helpdesk database, I have the following code in a control source to automatically pick up the username of the person logged on to the PC.

=Environ("username")

However, this worked fine on Windows 2000 and Office 2000, but I have now upgraded to Windows XP and Office 2003.

Pleas help as this is stopping us from working.

Thanks in advance.

Lee
 

Mile-O

Back once again...
Local time
Today, 23:20
Joined
Dec 10, 2002
Messages
11,316
Haynesey said:
I have the following code in a control source to automatically pick up the username of the person logged on to the PC.

=Environ("username")

The Control Source should be a field name and not a function.
 

Haynesey

Registered User.
Local time
Today, 23:20
Joined
Dec 19, 2001
Messages
190
Still not working

Hi,

It is in a field name. It worked fine until I upgraded to Office 2003.

Any more ideas?

Thanks

Lee
 

Mile-O

Back once again...
Local time
Today, 23:20
Joined
Dec 10, 2002
Messages
11,316
Sorry, I don't as I haven't seen A2003 yet. Have you tried using the specific number instead of "username"? i.e. =Environ(7) - don't think the actual number is 7 though.
 

ColinEssex

Old registered user
Local time
Today, 23:20
Joined
Feb 22, 2002
Messages
9,116
This answer was given by GHudson to another post - credit here goes to Ghudson

The Environ() function works great unless you have the Sandbox mode turned on in
Access 2003. You will need to add this public function to your Access 2003 db's if you
want to continue using the Environ() funciton...
Code:
'This function is used to allow the use of the Environ function in Access 2003
    
Public Function Environ(Expression)
On Error GoTo Err_Environ
        
    Environ = VBA.Environ(Expression)
    
Exit_Environ:
    Exit Function
    
Err_Environ:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_Environ
    
End Function
 

namliam

The Mailman - AWF VIP
Local time
Tomorrow, 00:20
Joined
Aug 11, 2003
Messages
11,695
Sandbox mode??? WTFH is that?

I know i send my nephew of 1,5 yrs there but what is it doing in Access?

Regardz
 

ghudson

Registered User.
Local time
Today, 18:20
Joined
Jun 8, 2002
Messages
6,195
Thanks for the plug ColinEssex.

This is straight from the Access 2003 offline help file...

About Microsoft Jet Expression Service sandbox mode
Microsoft Office Access 2003 uses the Microsoft Jet Expression Service to evaluate expressions. The Jet Expression Service can be configured to run in sandbox mode. Sandbox mode is a mode of operaton where only safe expressions can be evaluated.

About unsafe expressions
Unsafe expressions contain functions that could be exploited by malicious users to access drives, files, or other resources for which they do not have authorization. If malicious users have access to these resources, then they may be able to perform actions such as deleting all of the files from a folder, tying up a network resource, or modifying a file.

To make your Access databases (MDBs) and projects (ADPs) more resistant to malicious attacks, you should enable sandbox mode. Sandbox mode allows Access to be fully functional while blocking unsafe expressions.


Here is a must read for those using Access 2003:
Frequently asked questions about Access security warnings
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:20
Joined
Jul 9, 2003
Messages
16,273
Test Code for Environ

Excellent information posted here chaps thank you! I have been doing some experiments and thought other new users of this function may find my test code helpful:

Code:
Function fTest()
MsgBox "User Name: 40 >>> " & Environ(40)
MsgBox "User Name: 40 >>> " & Environ("UserName")
Debug.Print "User Name: 40 >>> " & Environ(40)
Debug.Print "User Name: 40 >>> " & Environ("UserName")
End Function

Function fFindEnvironCodes()
Dim x As Integer

'For x = 40 To 40
For x = 1 To 100
If x >= 255 Then Exit For 'Causes an error if you exceed 255
Debug.Print x & " >>> " & Environ(x)
Next x
'Highest Code found on my Machine is: 45 >>> windir=C:\WINDOWS
End Function

'To test these codes open the immediate window: >>> control G or "CTRL G" paste in the function
'name either fTest or fFindEnvironCodes

'if you use the function "fTest" notice the results,
'Environ(40)Produces = >>> USERNAME=Tony Hine
'Environ("UserName") = >>> Tony Hine
'as you can see both cases are self documenting!  And if you need only the "username" without
'any added text you can get it quite easily.
:
 

Adeptus

What's this button do?
Local time
Tomorrow, 07:50
Joined
Aug 2, 2006
Messages
300
If you want to keep your code as "safe" as possible, and only allow Environ("Username"), not Environ(anything)...
Code:
function GetUserName()
    GetUserName = Environ("UserName")
end function
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:20
Joined
Jul 9, 2003
Messages
16,273
Hi Adeptus,
Your post hints at some danger with the environ function, do you have any more information?

Cheers Tony
 

Adeptus

What's this button do?
Local time
Tomorrow, 07:50
Joined
Aug 2, 2006
Messages
300
Only that it is restricted by Sandbox mode, therefore it is considered potentially unsafe by Microsoft.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:20
Joined
Jul 9, 2003
Messages
16,273
Hi Adeptus,

Thanks for that, I understand now...

Cheers Tony
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:20
Joined
Jul 9, 2003
Messages
16,273
Excellent advice from Adeptus

Adeptus said:
If you want to keep your code as "safe" as possible, and only allow Environ("Username"), not Environ(anything)...
Code:
function GetUserName()
    GetUserName = Environ("UserName")
end function

In the above code I show the use of Environ(40) to return the username. However on another PC, Environ(40) does not return anything! Using the above code to extract the names the numbers relate to, I found that the list did not go above 30 items. And to extract the windows username, you needed a code .... 28 >>> USERNAME=windows user.... So I would strongly recommend Adeptus' recommendation of only allowing particular uses of the environ function and just like he has shown, pre Define them in your software.

I would suggest a slight change to the code above:


Code:
function fGetUserName()
    fGetUserName = VBA.Environ("UserName")
end function      ' fGetUserName

Or something along the lines of GetWinUserName may be more appropriate.

And I should add, never use the "number" variety as I think you will get unpredictable results on different machines, and possibly the same machine! However I don't know for sure without exploring this further. Will let you know if I discover anything else.

Cheers Tony
 
Last edited:

ghudson

Registered User.
Local time
Today, 18:20
Joined
Jun 8, 2002
Messages
6,195
As I quoted in this old thread... Retrieve computer name
Environ is a pretty resourceful function for obtaining user system settings. Using the numeric arguments can produce different results depending on the users version of Windows. I prefer to use the "text" of the argument like Environ("computername") instead of Environ(8).
The "environvariables.zip" sample I posted in that thread will allow you test the different results you can get with other versions of Windows when you use numeric arguments. Stick with the text argument to ensure you get what you want. Judging by the low number of downloads for my sample I doubt that many folks have seen that thread.
 

nathanhobbs

New member
Local time
Today, 23:20
Joined
Jan 11, 2007
Messages
1
Environ uses standard environment variables!

What is Environ?

Environ uses standard environment variables! This are the ye olde style dos variables you can see using the "set" command.

To see these click start, run and type in "cmd" and press enter. This will open a black MS-DOS stylie command window, with a C:\> prompt (or similar!)

If you type "SET" you will then get a list of Environment variables including the old "PATH" variable.

What options?
In the list you will probably see the familiar USERNAME and COMPUTERNAME, along with ones like:

Code:
ALLUSERSPROFILE=C:\Documents and Settings\All Users
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=MYBIGFATTOWER
ComSpec=C:\WINNT\system32\cmd.exe
HOMEDRIVE=H:
HOMEPATH=\
HOMESHARE=\\MyServer\home_22$\MyUserName
NUMBER_OF_PROCESSORS=1
OS=Windows_NT
Path=C:\WINNT\system32;C:\WINNT;C:\WINNT\system32
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 13 Stepping 6, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0d06
ProgramFiles=C:\Program Files
PROMPT=$P$G
SystemDrive=C:
SystemRoot=C:\WINNT
TEMP=C:\DOCUME~1\MyUser~1\LOCALS~1\Temp
TMP=C:\DOCUME~1\MyUser~1\LOCALS~1\Temp
USERDNSDOMAIN=ME.FRED.COM
USERDOMAIN=ME
USERNAME=MYUSERNAME
USERPROFILE=C:\Documents and Settings\MyUserName
windir=C:\WINNT

These are variables which are accessible by Name or by Number order.

Where do they come from?

Most of these values are set at startup by the windows login scrips etc, but you can set you own by using the SET command...
e.g.
Code:
SET USERNAME=BINGO

Sandbox Mode?
Why they arn't always safe: You could embed rather nasty commands into an environment variable, for example someone could change their USERNAME variable so that if you used it in an SQL statement it would delete all the users or change the admin password to something of their choice. (Basically the standard SQL injection techniques!)
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:20
Joined
Jul 9, 2003
Messages
16,273
Nice Post Nathan,

I didn't realize some of them could be set Programmatically, and I can now see the danger.

I would mention for clarity that this comment:

>>>These are variables which are accessible by Name or by Number order<<<

If you use the number order be aware that this can be different on different machines. Probably safe on your own machine, (I don't know for sure) but you may have problems if your code is transferred to a different machine.
 

Users who are viewing this thread

Top Bottom