Results 1 to 10 of 10

Thread: GetUserName

  1. #1
    Join Date
    May 2013
    Posts
    56

    Unanswered: GetUserName

    I am trying to implement some sort of security for the database that I have created in Access 2013. I have an .accdb backend and a runtime version for the front end.

    I am working to create user access screens that are different with different functions based on the user access level. I assume that if I can collect the Windows User ID that I can save that and use it to present the "front-end" that the user is approved for.

    I need help in getting, saving and using the Windows User ID. I would also like to use it to record the ID and time of last change to records.

    Does this approach make sense......can someone help me?

    Thanks in advance.

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    google dev ashish api for the network logon
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    496
    Provided Answers: 24
    vUserID = Environ("Username")

    INSERT INTO tLOG ([user],[form],[datestamp]) values ('" & vUserId & "','form',#" & now() & "#)"

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    nope, don't trust environ, used to till I saw how easily it can be subverted. use the network API call
    you will also find other functions which will identify the computer. I have successfully used these for a long time, and in a couple of cases the audit trail is sufficient for even the most fastidious HR department, or most obstreperous union rep in a disciplinary
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    May 2013
    Posts
    56
    OK, I will try the API call. What function identifies the computer? Where can I find a listing of the available functions?

    Thanks for the help. I will let you know how it goes

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Nov 2008
    Posts
    24
    Do you run Access in a networked environment that requires users to log on or Not. If it is a network you can use this along with a reference table to set security levels. I use levels 0,1,2,6 and 9 with different functions. I do a dlookup based on network login and this sets a global vaiable (integer) and then use this number is how the forms are displayed (including colours).
    I also agree to use the network API call.
    I have sometime sused this one:
    Public Function atCNames(UOrC As Integer) As String
    '************************************************* *
    'Purpose: Returns the User LogOn Name or ComputerName
    'Accepts: UorC; 1=User, anything else = computer
    'Returns: The Windows Networking name of the user or computer
    '************************************************* *
    On Error Resume Next
    Dim NBuffer As String, sDump As String
    Dim Buffsize As Long, Wok As Long, lR As Long
    Buffsize = 256
    ' NBuffer = Space(256)
    NBuffer = Space$(Buffsize)
    If UOrC = 1 Then
    Wok = api_GetUserName(NBuffer, Buffsize)
    atCNames = Left$(NBuffer, InStr(NBuffer, Chr(0)) - 1)
    Else
    Wok = api_GetComputerName(NBuffer, Buffsize)
    atCNames = Left$(NBuffer, InStr(NBuffer, Chr(0)) - 1)
    End If

    sDump = String$(255, " ") 'Create a buffer
    lR = api_GetUserName(sDump, 255) 'Fill buffer with ID
    GetUserName1 = Left$(sDump, InStr(1, sDump, vbNullChar) - 1) 'Return just the id
    ' MsgBox (sDump & " " & Wok & lR & " --- " & GetUserName1)

    End Function

    You will need to set
    Private Declare Function api_GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Declare Function api_GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

  8. #8
    Join Date
    May 2013
    Posts
    56
    I split the database and am using a runtime version for the users to access the functionality; the data is in the backend file on a server they all have to log into for access.

    I have a table with the usernames, the First and Last names and a number for the access level. Can I hold the username from GetUserName and use this to do a look up on the table and then display the interface based on the access level? I think you do something like this. But I don't understand the global variable and how you are using it.

  9. #9
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    yes
    you have your own internal usertable, stroe the users network logon ID as part of that. match the network logon to the user table and return whatever user rpivileges as identified

    I also put some code in front of each and everyt form/report which rerquests if a users has permission for the requested operation(s)

    to be honest i tend to allocate users to usergroups and permissions to usergroups.
    effectively the form requests does this user have permission for this form/report for the specified level, that level could be functional IE does user 'dhenderson' have edit permission for form 'abc'
    I'd rather be riding on the Tiger 800 or the Norton

  10. #10
    Join Date
    Nov 2008
    Posts
    24
    Hi dhenderson,
    Sorry for late response. I use a global variable to save having to re-read the table each time a user permission is required. I allocate a permission level to a user (usually between 0 and 10 depending on what I will allow them to do). You could also allocate a group and then set a group position. The table format I use is:
    Login Code (or user name):Full name:Password:Access Level:PositionecurityLeveluspended:Team Nameub-Team:
    I then do a simple lookup of the table to get security number or level and allocate a number to the Global Variable. I also run a background form with a timer to recheck this level every 10 minutes (in case I wish to change security temporarily). I just use on timer event and the same security module.
    The variable User_security_level is a global variable that I use. I use the following function:

    Public Function is_recognisedstaff() As Boolean
    Dim mydb As DAO.Database
    Dim myRS As DAO.Recordset
    is_recognisedstaff = False
    Set mydb = CurrentDb()
    Set myRS = mydb.OpenRecordset("TBL_OfficerLookup", dbOpenDynaset)
    myRS.FindFirst "[Logon Code] LIKE '" & LCase(atCNames(1)) & "' AND [Suspended] = False"
    If myRS.NoMatch Then
    is_recognisedstaff = False
    User_security_level = 0 'cant do anything
    Else
    is_recognisedstaff = True
    User_security_level = myRS![SecurityLevel]
    User_fullname = myRS![Full Name]
    End If
    myRS.Close
    Set myRS = Nothing
    End Function

Posting Permissions

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