Results 1 to 3 of 3
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!
2013-11-08, 11:25 #2
- Join Date
- Mar 2004
- Manning, South Carolina
- Thanked 1,451 Times in 1,320 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 31 Times in 30 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."