• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1902
  • Last Modified:

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!! = ))
  • 7
  • 5
1 Solution
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Just use this
so you don't have to worry about.

Time to move past Environ ()  :-)

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
developingprogrammerAuthor Commented:
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!! = ))
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

developingprogrammerAuthor Commented:
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!! = ))
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Well, db needs to be in a Trusted Location and Macro Security set to Low.
And I think there is some other Sandbox setting in the Registry that needs to be tweaked - which  may not be feasible on a given user's system at a given Company.

Envrion() can easily be spoofed - to fake a user name.  You cannot spoof the Win API in this context.

I moved away from Environ() 3-4 years ago.

I would not worry about the Declares too much. They are what they are ... and this Function is flawless. It's in all of my apps at work.

Just put the code in a standard VBA module, and then put


in your text box.

Of course, you can rename the Function to something else if you like ... for example

GetUserName() or whatever.
developingprogrammerAuthor Commented:
cool! that makes perfect sense = )

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

thanks mx! = )
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
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' ... :-)
developingprogrammerAuthor Commented:
GULP - not the PRO way of doing it.


developingPROprogrammer in action!! haha = ))

thanks so much mx once again as always!! = ))
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
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 :-)

developingprogrammerAuthor Commented:
Whao!! YES SIR!!

Haha okie dokes! Have a good rest and sweet dreams!! = ))

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).


developingprogrammerAuthor Commented:
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!! = ))
developingprogrammerAuthor Commented:
guys all code changed already! thanks so much for your help!! = ))
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now