Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    1
    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.

    Code:
    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
    Location
    Manning, South Carolina
    Posts
    6,378
    Thanks
    207
    Thanked 829 Times in 762 Posts
    Nanalou,

    This worked for me:
    Code:
    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
    Don't forget to change the information in RED.

    HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  3. #3
    2 Star Lounger HiTechCoach's Avatar
    Join Date
    Sep 2011
    Location
    Oklahoma City, OK
    Posts
    162
    Thanks
    0
    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 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
  •