Results 1 to 7 of 7
  1. #1
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109

    Windows User Login Information

    Hi Everyone,



    I did a search and found a few websites with code to pull the windows user information. I listed the one below that looks like the easiest to use. Unfortunately, I don't know any VBA so if anyone can give me a little bit better walk through than the website does that would be very helpful. This seems to be the only code necessary for my database and, from what I understand, there isn't a way to do this without VBA. I am creating an event for a form that adds the user information to a table when a new record is inserted. I'll create a macro that sets the value of the field to the function.

    My first question about the website instructions is how do I paste the code into a new module? If so which one. My create panel has module and class module. My second question is, how do I use the function? I tried to test it by creating a text field and making it equal to the function (=fOSUserName()) and it didn't work.

    Thank you very much for your assistance


    http://access.mvps.org/access/api/api0008.htm

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You'd want a standard module, not a class module. Also make sure you don't name the module the same as the function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    This is what I use: VBA.Environ("UserName")

    Yep, that's all of it, exactly as shown (actually, it will work without the VBA. qualifier).

    Access will not recognize the Environ function so can't use in textbox ControlSource or in query or macros. Examples from my db:

    Me.tbxUser = DLookup("UserInitials", "Users", "UserNetworkID='" & VBA.Environ("UserName") & "'")

    and
    Code:
    Private Sub tbxUser_AfterUpdate()
    If Me.tbxUser Like "[A-Z][A-Z][A-Z]" Or Me.tbxUser Like "[A-Z][A-Z]" Then
        CurrentDb.Execute "INSERT INTO Users(UserNetworkID, UserInitials) VALUES('" & VBA.Environ("UserName") & "', '" & UCase(Me.tbxUser) & "')"
        Call UserLogin
    Else
        MsgBox "Not an appropriate entry.", vbApplicationModal, "EntryError"
    End If
    End Sub
    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.

  4. #4
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    Quote Originally Posted by pbaldy View Post
    You'd want a standard module, not a class module. Also make sure you don't name the module the same as the function.
    Thank you for clearing that up. The website said to name it the same but I changed the name of the Module 'UserName'. I listed the code for the module below. If the module was created correctly, how do I use it? Do I create an event for a new record that says.
    Code:
    me.[Path to field to change] = fOSUserName()
    ?


    Code:
    Option Compare Database
    Option Explicit
    
    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

  5. #5
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    Quote Originally Posted by June7 View Post
    This is what I use: VBA.Environ("UserName")

    Yep, that's all of it, exactly as shown (actually, it will work without the VBA. qualifier).

    Access will not recognize the Environ function so can't use in textbox ControlSource or in query or macros. Examples from my db:

    Me.tbxUser = DLookup("UserInitials", "Users", "UserNetworkID='" & VBA.Environ("UserName") & "'")

    and
    Code:
    Private Sub tbxUser_AfterUpdate()
    If Me.tbxUser Like "[A-Z][A-Z][A-Z]" Or Me.tbxUser Like "[A-Z][A-Z]" Then
        CurrentDb.Execute "INSERT INTO Users(UserNetworkID, UserInitials) VALUES('" & VBA.Environ("UserName") & "', '" & UCase(Me.tbxUser) & "')"
        Call UserLogin
    Else
        MsgBox "Not an appropriate entry.", vbApplicationModal, "EntryError"
    End If
    End Sub
    Thank you for your reply. So would I just say on the event something like me.[field] = VBA.Environ("UserName") ? This is the first time touching any of the coding stuff so I apologize if something is written incorrectly.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Yes, that should work. So should the api function.

    BTW, the article did not specify what to name the module, just to 'call' function. Can 'call' from a query or ControlSource or macro or another VBA procedure.

    However, the Environ function can also be wrapped in a custom function and called the same way.
    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.

  7. #7
    MintChipMadness is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    109
    Quote Originally Posted by June7 View Post
    Yes, that should work. So should the api function.

    BTW, the article did not specify what to name the module, just to 'call' function. Can 'call' from a query or ControlSource or macro or another VBA procedure.

    However, the Environ function can also be wrapped in a custom function and called the same way.

    Thank you! The Environ function worked perfectly.

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

Similar Threads

  1. Replies: 3
    Last Post: 05-31-2012, 02:49 PM
  2. User Login
    By winterh in forum Forms
    Replies: 6
    Last Post: 03-14-2012, 06:01 PM
  3. Multi- User Login
    By Tom Lovick in forum Access
    Replies: 1
    Last Post: 02-11-2012, 12:20 PM
  4. Replies: 1
    Last Post: 10-27-2011, 01:31 PM
  5. Replies: 3
    Last Post: 09-22-2011, 03:35 PM

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