Results 1 to 14 of 14
  1. #1
    rayted is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2016
    Posts
    34

    Detect windows username in a form

    Hi guys,



    I have a form I am using and a 'Field' which reads 'Requested by'. In this box, I want whoever to be logged into and using this form to have their name populated by their default windows login name. How can I make this happen? Do I need a Macro?

    Thank you!

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    To get the windows login name use

    Code:
    Me.RequestedBy=Environ("UserName")
    where RequestedBy is the name of the form control

    Add this line to your Form_Load event code
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    rayted is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2016
    Posts
    34
    Hi ridders52,

    Thanks for that. Where do I input the code? on the embedded macro in get focus or somewhere else?

    Thank you

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    As previously stated:
    Add this line to your Form_Load event code
    i.e. use a VBA event procedure

    Code:
    Private Sub Form_Load()
    
         Me.RequestedBy=Environ("UserName")
    
         'Then any other existing form load code goes here ....
    End Sub
    I never use embedded macros so can't advise on their use
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Gicu's Avatar
    Gicu is online now VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hi there,

    I would put Colin's code in the OnCurrent Event of the form rather than OnLoad or even better add the following code to a standard module and then use the Default Value of the RequestedBy text box to call the custom function
    Code:
    Public Function getWinUser() As String    getWinUser = Environ("UserName") End Function
    
    See the following thread for more info:https://access-programmers.co.uk/for...d.php?t=221559

    Cheers,
    Vlad

  6. #6
    Gicu's Avatar
    Gicu is online now VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    There seems to be a problem with the code tags, you need to function on separate lines like this

    Public Function getWinUser() As String
    getWinUser = Environ("UserName")
    End Function

    Cheers,
    Vlad

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Quote Originally Posted by Gicu View Post
    Hi there,

    I would put Colin's code in the OnCurrent Event of the form rather than OnLoad ..
    I haven't checked the link but would you like to explain why?
    Form current will run each time the record is changed but for this purpose you don't need it to be repeatedly updated.

    However, I agree that using a function is obviously a good idea if the code will be used elsewhere
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  8. #8
    Gicu's Avatar
    Gicu is online now VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hi Colin,

    In the original post the OP mentions a "field" so I am assuming that it is a table field to which they want to assign a "default" value of Environ("UserName"). So I took it not as a simple "visual" unbound text box to show on the form who is the current Windows user, but more of a data question being related to a specific record. But again I am only speculating, maybe rayted can clear this for us.

    Cheers,
    Vlad

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Do you want to save the Windows username or another value? I have a Users table with a unique record for each user. Each record has user name, user ID, and their Windows login username. I use Environ("USERNAME") to do a lookup on the table to retrieve UserID and save UserID to record.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    rayted is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2016
    Posts
    34

    Image for reference

    Quote Originally Posted by Gicu View Post
    Hi Colin,

    In the original post the OP mentions a "field" so I am assuming that it is a table field to which they want to assign a "default" value of Environ("UserName"). So I took it not as a simple "visual" unbound text box to show on the form who is the current Windows user, but more of a data question being related to a specific record. But again I am only speculating, maybe rayted can clear this for us.

    Cheers,
    Vlad
    See image - the requested by field - I want this to be populated by using my windows logon name so I don't have to select someone else's nameClick image for larger version. 

Name:	name.png 
Views:	25 
Size:	42.0 KB 
ID:	32928

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Do you want to allow user to edit the Requestor?

    Access objects do not recognize the Environ() function, only in VBA. So would have to create VBA code to pull the USERNAME. This code could be a custom function in a general code module that can be called from anywhere, perhaps even from textbox or combobox DefaultValue property.

    If that statement makes no sense to you, I suggest you spend a solid week studying how to program in Access. Do you have ANY programming knowledge?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    @rayted
    So you now have plenty of advice on how to do this - in each case using Environ("UserName")

    Method 1 - Form_Load event - see my post 4

    Method 2 - create a function as suggested by Gicu then use that either in the Form_Load event or the control's default value - see post 6

    Method 3 - as post 6 plus a lookup table as suggested by June7 - see post 9

    There are other more complicated methods - nothing else is as simple as these
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  13. #13
    Gicu's Avatar
    Gicu is online now VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hi Colin,

    What would happen in the method 1 scenario (unig the OnLoad event) if he decides to add two records. The second one will not get the right value. Also, would you run the risk to overwrite somebody else's records if the db is used in a multiuser environment?

    I believe the options will be to create a custom function and attach that to the default value property of the control or to add code to the OnCurrent event like this:

    If Me.NewRecord then Me.ReguestedBy =Environ("UserName") 'where RequestedBy is the name of the combo box

    Cheers,
    Vlad

  14. #14
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Looking at the screenshot, there is a form & subform with the user name textbox in the main form
    So whilst your point is valid, I don't think its necessarily relevant in this case

    however, the OP knows what his database contains and can choose whichever he thinks is best .....
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Detect Windows shutdown or logoff
    By jcharrow in forum Access
    Replies: 4
    Last Post: 12-04-2017, 03:50 PM
  2. Replies: 10
    Last Post: 09-23-2015, 11:26 AM
  3. Windows Username Authentication
    By james28 in forum Security
    Replies: 2
    Last Post: 04-30-2014, 02:55 PM
  4. Windows Log In "Username" in an append query
    By jlgray0127 in forum Queries
    Replies: 1
    Last Post: 02-26-2013, 09:46 AM
  5. Detect which version of Windows is running
    By GraeagleBill in forum Programming
    Replies: 2
    Last Post: 12-31-2012, 05:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums