Avatar of developingprogrammer

asked on 

textbox control source = environ("username") not working

hey guys!

i know how to get the environ("username") in VBA no problem but recently i read this website (http://www.access-programmers.co.uk/forums/showthread.php?t=140708) and i wanted to try out the same but failed (attached is my empty database with just two textboxs and a form).

could yall share with me why it can't work? i don't think it's cause of sandboxing.

thanks guys!! = ))
Microsoft Access

Avatar of undefined
Last Comment
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Just use this
so you don't have to worry about.

Time to move past Environ ()  :-)

Avatar of developingprogrammer


cool!! thanks mx!! = ))

ok i'm reading up now on the Declares statement to understand a bit more on this topic.

hrmm mx,

1) why is the windows API better than environ() statement?
2) and also if you don't mind just for my learning purpose, why is my database not working when i put Environ("username") in the control source? thanks!! = ))
Avatar of developingprogrammer


oops and mx, and did i use the term windows API wrongly here?

why is it better to use a call to a DLL rather than then environ which belongs to the interaction class in the vba library? i suppose the environ function is also calling to the dll? = )

thanks mx!! = ))
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of developingprogrammer


cool! that makes perfect sense = )

but how does one fake a username? i thought environ would get the windows ID direct.

thanks mx! = )
You can Fake it from the Command Line in Windows ...

Something like

> Environ("UserName") = "BillyBobJo"

I've actually done in the past, just cannot remember the exact syntax
The bottom line is ... you can Set most (if not all) Environ variables.

It's  just not the 'pro' way to do things.

You can probably Google 'Spoof the Environ variable' ... :-)
Avatar of developingprogrammer


GULP - not the PRO way of doing it.


developingPROprogrammer in action!! haha = ))

thanks so much mx once again as always!! = ))
lol ...
You're alright bro ...
take care. ...
zzzzzzzzzzzzzzzzTime ... in CA

Oh ... get This Book   now.

That ... is an order.
Don't ask questions ... just-do-it :-)

Avatar of developingprogrammer


Whao!! YES SIR!!

Haha okie dokes! Have a good rest and sweet dreams!! = ))
Avatar of Bitsqueezer
Flag of Germany image


the "Environ" function does not call any Dll. It is very primitive what it does: It uses the "environment variables".

Open a command line window (formerly called "DOS") and enter "Set" and enter key. You will see a more or less long list of variables where you also can see "Username" or "Computername". These are the variables which "Environ" reads out and gives back to you, nothing more.

The Windows API gets it's information directly from the Windows system, so there is no way to change the result, it is coming from the approved Windows login (of course some hackers or viral code could change that, but that's not important here).

You can use the environment variables on your own, for example, the Java runtime uses the PATH variable in Windows to let the system know where to find the Java executables. You can invent your own variables simply by using "SET MYVARIABLE=My Value" in a command window. You can do the same with the Username variable - but it would not change the Environ result as it is only valid in the running command window. If you want to set that permanently you must only go into the Windows system settings, there you'll find a dialog to change or add environment variables, separated by system and user environment variables where the first kind of variables is used for any user.

But it's not a secure way as you see it can be changed by anyone. Instead of using environment variables it is better to use registry settings (can of course also be changed, but the chance is not so high that someone changes something here).


thanks Christian for your very comprehensive explanation!!

now i understand it so much better!!

yup i'm DEFINITELY going to change all my environ("username") code to the windows API code = ))

thanks Christian!! = ))
guys all code changed already! thanks so much for your help!! = ))
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo