Comparing dates in a recordset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tuxalot
    New Member
    • Feb 2009
    • 200

    Comparing dates in a recordset

    I am running this code in a hidden validation form and trying to compare dates (registered, date db was last accessed and the system date of the PC). I can't seem to get the check tampering code working. I've been staring at this for far too long and I guess I'm overlooking something. Any help is appreciated.

    Code:
    strSQL = "SELECT * FROM tblColors WHERE [SerialHDD] = '" & strHDD & " '"
    Set db = CurrentDb
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    
    If rs.EOF = True Then
        MsgBox "It appears that you are not registered. Opening registration form."
        DoCmd.OpenForm "frmRegister", acNormal, , , acFormEdit, acWindowNormal
        DoCmd.Close acForm, "frmValidate"
        Exit Sub        'once user has completed registration, open this form again
    Else
        'check tampering - did the user roll back his PC clock
        If CLng(Date) < CLng(rs!DateLastAccessed) Then
            MsgBox "It appears that the database has been tampered with. Please contact support."
            Call dbTamper
            DoCmd.Close
        End If
    
        'check tampering - did the user roll back date accessed in local table
        If CLng(rs!DateRegistered) > CLng(rs!DateLastAccessed) Then
            MsgBox "It appears that the database has been tampered with. Please contact support."
            Call dbTamper
            DoCmd.Close
        End If
    End If
    Last edited by NeoPa; Dec 16 '11, 04:32 PM. Reason: Cleared indents evenly (and added closing End If)
  • sierra7
    Recognized Expert Contributor
    • Sep 2007
    • 446

    #2
    Hi
    I don't fully understand what is happening here, so humour me!

    You are setting up a recordset and then assuming that if it's EOF there are no records. Fair enough but I usually check if RecordCount =0. You then open two forms, so the focus is going to move from the current form to one of these new forms. Depending upon whether they are 'pop-up' or 'modal', goodness knows where the focus and next line of code will be.

    Else, if the recordset is not empty, you are assuming there is only one record.. OK.. You are then trying to read 'rs!DateLastAcc essed' without a WITH statement. I think this is why the check is going wrong.

    S7

    Comment

    • TheSmileyCoder
      Recognized Expert Moderator Top Contributor
      • Dec 2009
      • 2322

      #3
      You REALLY need to tell us what you mean by "Not working". Not working can mean a lot of things. It could be throwing an error. It could be that it evaluates everything to true, or everything to false. It could be that it fails under specific circumstances.

      Try setting a break point at the lines where you make the date comparison, hower the mouse over the recordsets and see what value the date has.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32653

        #4
        Tux, you should be past these nooby errors by now. Smiley is absolutely right, but you don't show the declarations of your variables either. See When Posting (VBA or SQL) Code. You've copied it directly, which is good, but I'm going to strip out the spare indentation (evenly) for you, to make it more easily viewed on here.

        Declarations are particularly important when dealing with recordsets as they come in both DAO and ADODB flavours, and they are not the same.

        Line #1 includes an extra space after strHDD and before the SQL string quote (in other words would look like WHERE [SerialHDD] = 'nnnnn '). This is probably a mistake.

        I may disagree somewhat with S7 on checking for empty recordsets. Checking EOF is more reliable as RecordCount is not set immediately in all situations (for instance when the data set is large and/or it is not Jet provided).

        The date checking is unnecessarily awkward from my PoV, but I see nothing there which shouldn't work, assuming all the date fields contain valid data. At this point it might be a good idea to remind you that you haven't included that fundamental information in your question either.
        Last edited by NeoPa; Dec 16 '11, 04:33 PM.

        Comment

        Working...