Consulting

Results 1 to 7 of 7

Thread: Display Username in unbound text box on a form

  1. #1
    VBAX Regular
    Joined
    Mar 2014
    Posts
    11
    Location

    Display Username in unbound text box on a form

    OK, I have a database that works great so far. I have about 80 users all with thier own Usernames and Passwords. Issue is, I want to be able to catch the Username they log in with, and then display it in a textbox on the form they go to after login. Not to good with VBA, but I know it can be done. Please let me know if anyone has any ideas.... Keep in mind, I'm not to good with VBA yet so please explain THANKS.

    In case you need it, here is my login code.

    Private Sub Command8_Click()
    Username.SetFocus
    
    'This will check usernames and passwords in the system
    If Username = "Admin" And Password = "123" Then
    
    DoCmd.Close
    
    DoCmd.OpenForm "WrittenStatsForm"
    
    Else
    MsgBox "Incorrect Username and or Password"
    
    End If
    End Sub

  2. #2
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    I'll just assume you're loading usernames and passwords from a table since you didn't specify.
    Once the user has logged in you can store their username in a public variable in a module.
    Then you just need to create a public function to read that variable.

    Public LoggedInUser As String
    
    Public Function GetUserName() As String
        GetUserName = LoggedInUser
        'or to get windows login use
        'GetUserName = CreateObject("WScript.Network").UserName
    End Function
    You can use public functions pretty much anywhere, macros, queries, etc.
    For a textbox just set the control source to =GetUserName()

    I'll also assume you're asking for help with the data loading...

    This example requires two text boxes, txtUser and txtPW.
    When the form opens, keypreview is set to true, which means that whenever the user presses a button the form gets to process the event before the control. I've used it here to trap the Return key which has a keycode of 13.
    Whenever the user presses the return key, CheckPW checks that neither text box is empty then opens a recordset to get the password from a table called 'user'.
    It then does a case sensitive check against both passwords. If they match, CheckPW is set to true, the user is logged in and the public username variable is set.

    Private Sub Form_Load()
        Me.KeyPreview = True
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        Select Case KeyCode
        Case 13
            If CheckPW(Trim("" & txtUser), Trim("" & txtPW)) Then
                LoggedInUser = Trim("" & txtUser)
                DoCmd.OpenForm "WrittenStatsForm"
                DoCmd.Close acForm, Me.Name
            End If
        End Select
    End Sub
    
    Private Function CheckPW(usr As String, pw As String) As Boolean
        If usr = "" Then Exit Function
        If pw = "" Then Exit Function
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim retval As String
        Set db = CurrentDb
        Set rs = db.OpenRecordset("select pw from user where username='" & usr & "'")
        If Not rs.EOF Then retval = rs("pw").Value
        rs.Close
        Set rs = Nothing
        Set db = Nothing
        CheckPW = StrComp(retval, pw, vbBinaryCompare) = 0
    End Function

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use

    Environ("Username")
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Quote Originally Posted by xld View Post
    Use

    Environ("Username")

    Yeah that should also return the windows logon username.
    But I've had problems with that in the past so I prefer to use the WScript.Network UserName instead (commented out option in first piece of code).

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Really? I use it all of the time. What problems have you had? I've had problems with WScript on sites where scripting is disabled (not often, but it has happened).
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Mar 2014
    Posts
    11
    Location
    Quote Originally Posted by xld View Post
    Use
    Environ("Username")
    OK, I'm new with VBA so can you explain a little? I have 1 form that is a login form, once they sign in, that form closes and opens another form they will use. Where do I put your code to get the username and how do I get it in the text box on the main form they are using?

  7. #7
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Create a public function as I said in my first post but just use

    Public Function GetUserName() As String 
        GetUserName = environ("UserName")
    End Function
    @xld
    I just used to find that it would sometimes return no value. You're right though it's the best option if it works.

Posting Permissions

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