current user name display on form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cariboybgi
    New Member
    • Jan 2019
    • 3

    current user name display on form

    Good day
    I created a database in Access 2010 and now after opening it in Office 365 i found some of the functions previously done not working properly. I would be happy if I can get some help sorting out my problems.
    1. I would like to display the current user of the database based on the person's User Login and not the Environ (computer's name). The fields in my table are User Name, User Login, Password, User Security. Below is the code i currently have

    Code:
    Private Sub Form_Load()
    Dim Security As String
    
    
    Me.TxtUser = Environ("UserName")
    
    If IsNull(DLookup("UserSecurity", "tblUser", "[UserLogin] = '" & Me.TxtUser & "'")) Then
       MsgBox "No User Security set up for this user. Please Contact Admin", vbOKOnly, "login info"
       Me.CmdAdmin.Enabled = False
    Else
        Security = DLookup("UserSecurity", "tblUser", "[UserLogin] = '" & Me.TxtUser & "'")
        If Security = "Admin" Then
        Me.CmdAdmin.Enabled = True
        
        Else
        Me.CmdAdmin.Enabled = False
      End If
    2. I am interested in having a field on the data form / datasheet auto populate and save with the name of the person inputting the information.
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3653

    #2
    First, I would highly recommend against you saving any password information in your tables.

    Second, exactly what is it about using Environ(“UserNa me”) that does not give you your desired result?

    Also, you may want to take a look at this article on setting User Permissions. It is a bit involved but if you use it as a template, it is very flexible.

    Finally, to streamline what you already have, without changing anything functionally:
    Code:
    Private Sub Form_Load()
        Dim Security As String
    
        Me.TxtUser = Environ("UserName")
    
        Security = Nz(DLookup("UserSecurity", _
                              "tblUser", _
                              "[UserLogin] = '" & Me.TxtUser & "'"), "")
        If Security = "" Then
            MsgBox "No User Security set up for this user. Please Contact Admin", vbOKOnly, "login info"
            Me.CmdAdmin.Enabled = False
        Else
            If Security = "Admin" Then
                Me.CmdAdmin.Enabled = True
            Else
                Me.CmdAdmin.Enabled = False
            End If
        End If
    Hope this hepps.

    Comment

    • cariboybgi
      New Member
      • Jan 2019
      • 3

      #3
      Thanks for your reply. My issue / problem with using Environ["UserName"] is that it returns the computer login name (it's a shared system with one user profile, so no matter who logs into the database Environ returns the name of the computer and not the user) rather than the name of the person who is currently using the database. How about the second item on my post, can you help in that regard?

      Comment

      • PhilOfWalton
        Recognized Expert Top Contributor
        • Mar 2016
        • 1430

        #4
        Hi Carl

        Try this
        Code:
        Option Compare Database
        Option Explicit
        
            Private Declare Function apiGetComputerName Lib "kernel32" Alias _
            "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
            
            Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias _
            "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
        
        Function fOSUserName() As String
            ' Returns the network login name
        
            Dim lngLen As Long, lngX As Long
            Dim strUserName As String
            strUserName = String$(254, 0)
            lngLen = 255
            lngX = apiGetUserName(strUserName, lngLen)
            If (lngX > 0) Then
                fOSUserName = Left$(strUserName, lngLen - 1)
            Else
                fOSUserName = vbNullString
            End If
            
        End Function
        Then on any form or report add a TextBox called "TxtUserNam e" with the ContolSourse "= fOSUserName()".

        Note the "=" sign is required in the ContreoSource.

        No guarantees, but try it. If it doesn't work, I have a plan B.

        Phil

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3653

          #5
          @ Phil:
          That will return the same computer user name variable.

          @ cariboybgi:
          If you take a look at the link I directed you to, it will address the issue of User Name vs. User’s Actual Name—although not in those same words. The key is to create a table that includes the computer user name, the person’s name, and any other pertinent information you may want to keep on hand concerning your users.

          Concerning your second issue. We have a policy of one distinct question per thread. You are free to create a new thread for a solution to that question.

          Hope this hepps.

          Comment

          Working...