Results 1 to 6 of 6
  1. #1
    swagger18 is offline Novice
    Windows Vista Access 2003
    Join Date
    Jan 2011
    Posts
    19

    Name Association

    Good morning all,

    I am just tweaking my database as it is complete and I would like to add some validation.



    I have a userID field which will currently allow the user to enter there name, but I would like it in a certain format.

    Is there anyway in which I could code the form so that on form load the box would be populated with the name of the user that is currently logged on to the computer where the database has been opened from.

    Many thanks,

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,619
    You can use VBA.Environ("USERNAME") to get the user's network login username. Access will not recognize the Environ function but VBA does. Use this data to lookup user's full name from a table. Populate textbox.
    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.

  3. #3
    swagger18 is offline Novice
    Windows Vista Access 2003
    Join Date
    Jan 2011
    Posts
    19
    Thanks for the reply. How would I code that so it would populate the field as I have never come across this before.

    Cheers,

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,619
    You need to build a table of records with userID and name. Then in form Load or Open event validate user, something like:

    strUser = Nz(DLookup("namefield", "tablename", "userID='" & VBA.Environ("USERNAME") & "'"),"")
    If strUser <> "" Then
    Me.textboxname = strUser
    Else
    MsgBox "Not authorized user. Contact Administrator."
    End If
    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.

  5. #5
    swagger18 is offline Novice
    Windows Vista Access 2003
    Join Date
    Jan 2011
    Posts
    19
    I have managed to do it using the following function

    Private Declare 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

    Also changing the Input Mask on the text box to be =fOSUserName()

    Thanks for your help man

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,619
    Yes, I explored that API code and started to implement it then I found the Environ function. So simple. Haven't had any issues with it. So I dumped the API. You put that expression in Input Mask? Or did you mean ControlSource?
    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.

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

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