Results 1 to 14 of 14
  1. #1
    joym is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2017
    Posts
    57

    Question capture name of user logged in everytime time i create a new record

    i created a login page with 2 fields txtLoginID and txtpassword



    When i press ok i push the username for that particular ID to the field me.txtusername on form [TT Form]

    Code:
    Private Sub btn_Ok_Click()
    
    Dim ID As Integer
    
    ID = DLookup("LoginID2", "Login", "Username = '" & Me.txtLoginID.Value & "'")
    
    DoCmd.OpenForm "TT Form", , , , acFormAdd
    Forms![TT Form]![txtUser] = Name
    
    End Sub
    This works fine intially how ever the form [TT Form] is used to create new records and has a save button which saves the record if all fields have been filled.

    Since it is used to add records after executing the save command. it clears all fields including the me.txtusername.

    I want the username of the person logged in to still be displayed until the user logs out.

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    Code:
    Private Sub btn_Ok_Click()
    
    Dim ID As Integer
    
    ID = DLookup("LoginID2", "Login", "Username = '" & Me.txtLoginID.Value & "'")
    Tempvars!tvUserID = ID
    DoCmd.OpenForm "TT Form", , , , acFormAdd
    Forms![TT Form]![txtUser] = Name
    End Sub

    Add the line in red. After the save button code:

    Code:
    Docmd.close acform,me.name
    docmd.open "TT Form",,,"ID=" & Tempvars!tvUserID
    ID is whatever the textbox is named holding LoginID2
    The reason for the Tempvar is scope. You need to hold the value of the ID so it can be used in the second procedure (which I assume is the save button).

  3. #3
    joym is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2017
    Posts
    57
    Quote Originally Posted by davegri View Post
    Code:
    Private Sub btn_Ok_Click()
    
    Dim ID As Integer
    
    ID = DLookup("LoginID2", "Login", "Username = '" & Me.txtLoginID.Value & "'")
    Tempvars!tvUserID = ID
    DoCmd.OpenForm "TT Form", , , , acFormAdd
    Forms![TT Form]![txtUser] = Name
    End Sub

    Add the line in red. After the save button code:

    Code:
    Docmd.close acform,me.name
    docmd.open "TT Form",,,"ID=" & Tempvars!tvUserID
    ID is whatever the textbox is named holding LoginID2
    The reason for the Tempvar is scope. You need to hold the value of the ID so it can be used in the second procedure (which I assume is the save button).
    Sorry for the late reply. I did test this out however i have got this error
    Click image for larger version. 

Name:	01.jpg 
Views:	18 
Size:	39.3 KB 
ID:	28858

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    The tempvar is initialized in btn_Ok_click.
    If that code has not been run yet, the tempvar will not exist.
    I can't tell from your code fragments what the sequence of events has been.

  5. #5
    joym is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2017
    Posts
    57
    Quote Originally Posted by davegri View Post
    The tempvar is initialized in btn_Ok_click.
    If that code has not been run yet, the tempvar will not exist.
    I can't tell from your code fragments what the sequence of events has been.
    I did initialize it as you instructedClick image for larger version. 

Name:	02.jpg 
Views:	15 
Size:	204.9 KB 
ID:	28862

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    Try reversing these two lines:

    Code:
    docmd.open "TT Form",,,"ID=" & Tempvars!tvUserID
    Docmd.close acform,me.name

  7. #7
    joym is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2017
    Posts
    57
    Quote Originally Posted by davegri View Post
    Try reversing these two lines:

    Code:
    docmd.open "TT Form",,,"ID=" & Tempvars!tvUserID
    Docmd.close acform,me.name
    Same error

  8. #8
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    Are you using Access 2007 or higher? Tempvars were not supported until ac2007.

  9. #9
    joym is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2017
    Posts
    57
    Ms access 2013

    are we pulling the temporary value from the wrong place. After the initial login which sends the user name to the TTform the login form closes so when i click the save button on the TTform it isnt able to pull form the login page as it is closed.

    in that case i would have to pull the temp value from the textbox which had the user's name on TTform ???

  10. #10
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    Well, again I don't know what your program flow is as a user logs in. Wherever the tempvar is set, it obviously needs to be where the ID is currently available.
    You can go to the immediate window and type:
    ?tempvars!tvUserID
    It will either respond with 'null', a blank which is a zero length string, or the ID.
    You also can always debug.print "ID=" & tempvars!tvUserID in your code just before using the tempvar to see its value at that point (displayed in the immediate window).

  11. #11
    joym is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2017
    Posts
    57
    You can go to the immediate window and type:
    ?tempvars!tvUserID
    did not get what you meant by that

    how can you upload a copy of the db here. I did it once but forgot

  12. #12
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    go here for info on attachments:
    https://www.accessforums.net/faq.php...b3_attachments

    and you can google "access immediate window". This is an immense debugging aid.

  13. #13
    Mayer is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    4
    Hello,,
    This is my first post in this great forums, glad to be here
    why not to use "Me.visible = False" command instead of "docmd.close" for login form, so all fields in login form will remain available all the time
    Visible just hide the login window but it still available in background
    hope that can help

  14. #14
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716

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

Similar Threads

  1. Replies: 0
    Last Post: 02-07-2017, 04:20 AM
  2. Replies: 4
    Last Post: 10-29-2016, 12:20 AM
  3. Logged In user Names
    By Parminder in forum Access
    Replies: 5
    Last Post: 07-23-2015, 02:17 PM
  4. Replies: 8
    Last Post: 01-01-2015, 07:34 PM
  5. Getusername of currently logged in user
    By nkuebelbeck in forum Access
    Replies: 8
    Last Post: 06-29-2011, 04:06 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