Results 1 to 3 of 3
  1. #1
    New Lounger
    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 would like to store the username information into a table. The table is called tblTempTrainerNameForSurveys and there is just one field in it called TrainerName. For the life of me I can't figure out out how to write the UserName value to the table.

    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!

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Manning, South Carolina
    Thanked 1,609 Times in 1,453 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) & ");"
    End Sub
    Don't forget to change the information in RED.

    May the Forces of good computing be with you!


    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    3 Star Lounger HiTechCoach's Avatar
    Join Date
    Sep 2011
    Oklahoma City, OK
    Thanked 37 Times in 36 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 13:03.
    Boyd Trimmell aka HiTechCoach (Access Information here)
    Microsoft MVP - Access Expert
    "If technology doesn't work for people, then it doesn't work."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts