2013-11-08, 09:57 #1
- Join Date
- Aug 2013
- Thanked 0 Times in 0 Posts
Environ("USERNAME") How do I store the value in a table?
I am a very novice VBA coder. I have a very simpe bit of code I found online that provides me the network login for users.
Public Function GetUserName() As String UserName = Environ("USERNAME") MsgBox (UserName) 'this msgbox line of code is for testing and the message box does correctly display my network login information End Function
I created an update query that updated the TrainerName to GetUserName(). It doesn't work. It doesn't error out though either.
The goal is for users to click a button and see just their survey data based on their login name (which would be captured in the table). Clicking the button would run a query that uses the login name. Hope that makes sense.
What am I doing wrong? Is there an easier way to store this data into a table? Your help is appreciated!
Subscribe to our Windows Secrets Newsletter - It's Free!
Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!
+ Get this BONUS — free!
Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!
2013-11-08, 11:25 #2
- Join Date
- Mar 2004
- Manning, South Carolina
- Thanked 766 Times in 700 Posts
This worked for me:
Sub InsertUserNameIntoTable() Dim dbs As Database Set dbs = OpenDatabase("G:\BEKDocs\Access\Code to Test\Testing.mdb") dbs.Execute " INSERT INTO tblTempTrainerNameForSurveys " _ & "(TrainerName) VALUES " _ & "(" & Chr(34) & Environ("USERNAME") & Chr(34) & ");" dbs.Close End Sub
2013-11-09, 12:01 #3
- Join Date
- Sep 2011
- Oklahoma City, OK
- Thanked 24 Times in 24 Posts
If you are using Access 2007 or later you can take advantage of the new TempVars collection. It works get for doing what you need. TempVars can be used to as criteria in a Query. Unlike VBA variables that require writing a a function to make the available in queries.
What version of Access are you using?
Warning: Environ("USERNAME") can be spoofed. It is easy to change the user name returned by Environ("USERNAME"). It is best to use a Windows API that can't be spoofed.
Warning 2: Be sure to lock data the front end. If the users have access tot he tables they could change the username store int he table.
Last edited by HiTechCoach; 2013-11-09 at 12:03.Boyd Trimmell aka HiTechCoach (Access Information here)
Microsoft MVP - Access Expert
"If technology doesn't work for people, then it doesn't work."