Results 1 to 9 of 9

Getusername of currently logged in user

  1. #1
    nkuebelbeck is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Mar 2010
    Posts
    91

    Getusername of currently logged in user

    Here is what I came up with. Its really cheesy and I would like something a little more secure

    Code:
    Private Sub Form_Open(Cancel As Integer)
    
    Dim pwd As String
    pwd = InputBox("What is the Password?")
    If pwd = "TC" Then
    MsgBox "Success!!"
    Else
    MsgBox "That was incorrect. Please try again"
    DoCmd.CLOSE
    End If
    
    End Sub
    So I googled and found there was a way to get logged in user.



    So I have this module built as GetUserName

    Code:
    Option Compare Database
    
    Declare Function WNetGetUser Lib "mpr.dll" _
         Alias "WNetGetUserA" (ByVal lpName As String, _
         ByVal lpUserName As String, lpnLength As Long) As Long
    
    Const NoError = 0                    'The Function call was successful
    
    Function GetUserName() As String
    
         Dim LUserName As String
         Const lpnLength As Integer = 255
         Dim status As Integer
         Dim lpName
    
         ' Assign the buffer size constant to lpUserName.
         LUserName = Space$(lpnLength + 1)
    
         ' Get the log-on name of the person using product.
         status = WNetGetUser(lpName, LUserName, lpnLength)
    
         ' See whether error occurred.
         If status = NoError Then
              ' This line removes the null character. Strings in C are null-
              ' terminated. Strings in Visual Basic are not null-terminated.
              ' The null character must be removed from the C strings to be used
              ' cleanly in Visual Basic.
              LUserName = Left$(LUserName, InStr(LUserName, Chr(0)) - 1)
    
         Else
              ' An error occurred.
              MsgBox "Unable to get the name."
              End
         End If
    
         GetUserName = LUserName
    
    End Function

    Now, How do I call that function and return the string value on the form load event or form open event?

    I tried this.

    dim stname as string
    stname = GetUserName()

    Msbbox stname

    This doesn't work. Very new to VBA.

    Thanks in advance

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,739
    Using this:

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

    I do this:

    Me.txtUserID = fOSUserName
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    nkuebelbeck is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Mar 2010
    Posts
    91

    will this work on all platforms

    where do you get this file
    advapi32.dll

    also, does this work on xp 32 and 64 and windows 32 and 64?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,739
    I assume it comes with Windows. I've never done anything but copy that function into a standard module. It works "out of the box".

    I don't believe I've tested on a 64 machine.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    It will work on a 64 bit machine (32 bit or 64 bit Windows - I have a 64 bit Windows 7 and that API works fine on it). The only change you would have to do is if you had 64 bit Access installed would be to declare the API function as PTRSAFE.

  6. #6
    pkstormy's Avatar
    pkstormy is offline Access/SQL Server Expert
    Windows XP Access 2003
    Join Date
    Mar 2010
    Location
    Madison
    Posts
    655
    This code was deemed unhackable by MS:

    https://www.accessforums.net/code-re...thod-7675.html

    For an example using a security type table:
    https://www.accessforums.net/code-re...mple-7538.html

  7. #7
    nkuebelbeck is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Mar 2010
    Posts
    91
    Quote Originally Posted by pbaldy View Post
    Using this:

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

    I do this:

    Me.txtUserID = fOSUserName
    What If I want to query that value on form open to secure it with a certain username?

    if fOSuserName = "UsernameOFmyChoosing" then
    msgbox "success"
    else
    docmd Close

    ?

    EDIT - So I tried it and when I try to call the function like this me.text98 = fosusername. I get this error message

    Compile Error:
    Expected variable or procedure, not module?

    If I remove that line of code it works.

  8. #8
    nkuebelbeck is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Mar 2010
    Posts
    91

    nevermind

    My module and function had the same names. Must have been confusing access

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    20,739
    It would; a function can not have the same name as a module. Glad you got it sorted out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. user id
    By chazcoral2 in forum Forms
    Replies: 9
    Last Post: 08-19-2010, 01:37 PM
  2. Replies: 8
    Last Post: 06-30-2010, 10:57 PM
  3. Look Up User.
    By emccalment in forum Forms
    Replies: 2
    Last Post: 03-02-2010, 11:09 AM
  4. new user needs help
    By dougb in forum Reports
    Replies: 1
    Last Post: 07-15-2009, 07:41 PM
  5. Currently logged in user into text box
    By ne16428 in forum Security
    Replies: 5
    Last Post: 06-10-2009, 11:46 AM

Posting Permissions

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