Results 1 to 4 of 4
  1. #1
    stildawn is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Oct 2013
    Posts
    185

    Simple LastLogin Now() VBA code not working

    Hi All

    I have this simple code:

    Code:
    Set rs = CurrentDb.OpenRecordset("dbo_Users_Table", dbOpenDynaset, dbSeeChanges)
    rs.Edit
    rs!LastLogin = Now()
    rs.Update
    This is just a simple line to record when I user logs in.



    So sometimes this works, and sometimes it doesnt (no errors), sometimes it just doesnt seem to change to the new Now() date / time?

    Any ideas?

    I do use rs previously above this code, so maybe that might cause it? Full code as below:

    Code:
    Private Sub cmdLogin_Click()
         Dim rs As Recordset
        
        Set rs = CurrentDb.OpenRecordset("dbo_Users_Table", dbOpenSnapshot, dbReadOnly)
        
        rs.FindFirst "UserName='" & Me.txtUserName & "'"
        
        If rs.NoMatch = True Then
            IncorrectUserNameStyle
            Exit Sub
        End If
        
        If rs.NoMatch = True Then
            IncorrectUserNameStyle
            Exit Sub
        Else
            Me.lblIncorrectUserName.Visible = False
            Me.txtUserName.BorderColor = RGB(0, 0, 0)
        End If
        
        If rs("UserPassword") <> Nz(Me.txtPassword, "") Then
            IncorrectPasswordStyle
            Exit Sub
        Else
            Me.lblIncorrectPassword.Visible = False
            Me.txtPassword.BorderColor = RGB(0, 0, 0)
        End If
        
        MsgBox "Login Successful" & vbNewLine & vbNewLine & "Program will now load and update, this will take some time.", vbInformation, "Logged In As " & Me.txtUserName
        strSecLevel = rs("SecLevel")
        
        Set rs = CurrentDb.OpenRecordset("dbo_Users_Table", dbOpenDynaset, dbSeeChanges)
        rs.Edit
        rs!LastLogin = Now()
        rs.Update
        
    
    
        Call UpdateFromSQL
        
        DoCmd.OpenForm "Test Form"
        
        DoCmd.Close acForm, Me.Name
        
    End Sub

    EDIT: Sorry I think I figured this out, because I reset the RS, it doesnt keep the correct login row as the previous use, so I need to do the FindFirst again.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    you dont need a lot of code.
    you could have just run an append query to add the date at login.

    you dont need to load 2 recordsets nor run findFirst, both slow.
    use Dlookup instead of findfirst , since Dlookup doesn't need to load a recordset.

  3. #3
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    Why not open the RS with just the single user's data, then you are already on that record, so no need to find it?

    Code:
    Dim strSQL as String
    
    strSQL = "SELECT * FROM dbo_Users_Table WHERE UserName='" & Me.txtUserName & "'"
    
    
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    
    If rs.EOF or rs.BOF Then      ' No Records so no match
    
    etc etc
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    If this is to check one user's credentials then loading a whole table of records makes no sense to me. If that table only held one record, then loading a recordset makes no sense either. One or two DLookups should be enough. If it returns the password that matches what's been entered then do A. If not, do B. Don't see why there are two NoMatch tests in a row - the second one will likely never run.

    I stopped doing password checks long ago, preferring to have a table of user data (EmplNum, Fname, Lname, UserID, email, etc.) UserID being the Windows user login ID because you can't share or spoof that value. If the person who opens the db is in the table they get in. If not, they don't.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Why is this simple query not working?
    By doobybug in forum Queries
    Replies: 19
    Last Post: 05-24-2017, 06:56 PM
  2. Replies: 2
    Last Post: 05-14-2017, 10:07 AM
  3. Simple datasheet view not working
    By dcol in forum Forms
    Replies: 2
    Last Post: 12-23-2015, 04:34 AM
  4. Replies: 1
    Last Post: 07-30-2011, 07:58 AM
  5. Simple Nav Form Code Not Working
    By alsoto in forum Forms
    Replies: 10
    Last Post: 04-10-2009, 09:30 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
  •  
Other Forums: Microsoft Office Forums