Count All except last record

gmatriix

Registered User.
Local time
Today, 14:24
Joined
Mar 19, 2007
Messages
365
Hello All,

I am trying to count all records except for the last one. I have attempted unsuccessfully. Here is what I have.

BlankCount = DCount("*", "Keyencetbl", "IsNull([RawData] And [Recorded On] Is Not Last([id])")

I have the wrong syntax.....any ideas?

Thanks
 
gmatrix, what is that you are trying to do you said? Just trying to count the number of records in the table "Keyencetbl"? If so why is the Criteria included?
 
Hey,

I have some records that get deleted if RawData and RecordedOn is blank. I am counting them because it is putting the amount of records deleted in a table.

However, the last record has to have RawData and RecordedOn blank. I just want to only count all records except for the last one
 
At what point are you performing this count? Immediate After the deletion? If so how are you deleting the records?
 
Here is the code. The code works....just trying to tweak it alittle

Code:
Dim KeyDel As dao.Recordset, Dbscurrent As dao.Database
  

    Dim LessDays As Date
    Dim DelCount As Variant
    Dim DelCount1 As Variant
    Dim BlankCount As Variant
    Dim BlankCount1 As Variant
    Dim Blankttl As Variant
    Dim DelSum As Variant
    Dim DelSum1 As Variant
    Dim Delttl As Variant
    Dim Delttl1 As Variant
   


           
'If it errors out end the code and contact IT department

On Error GoTo ErrorBox


' Opens Alert Box and lock menu buttons

DoCmd.OpenForm "AlertMsg"
    DoCmd.RepaintObject
    Me.Command0.Enabled = False
        Me.Command1.Enabled = False
        Me.Command4.Enabled = False
  
DoCmd.SetWarnings False

'Set Recordset

    Set Dbscurrent = CurrentDb()
    Set KeyDel = Dbscurrent.OpenRecordset("Keyencetbl")
    Set KeyDel1 = Dbscurrent.OpenRecordset("Keyencetbl1")
    
    

' Counting Current Records/Counting all blank records

DelCount = DCount("*", "Keyencetbl")
DelCount1 = DCount("*", "Keyencetbl1")
BlankCount = DCount("*", "Keyencetbl", "IsNull([RawData] And [Recorded On])")
BlankCount1 = DCount("*", "Keyencetbl1", "IsNull([RawData] And [Recorded On])")


' Will leave 7 days of data in record

    LessDays = Date - 6
    

'Will delete empty records

DoCmd.OpenQuery "KeyNullDelete"
DoCmd.OpenQuery "KeyNullDelete1"
                    
                       
    ' [Keyencetbl]
    ' Moving through record and deleting if the current record is less than
    ' the hishest ID on the server and less than 7 days prior to today
    ' The Keytbl1 is a subform located on the Menu form.  It is attached to
    ' a stored procedure.
    
If KeyDel.RecordCount <> 0 Then
        KeyDel.MoveFirst
        Do Until KeyDel.EOF
            If KeyDel!ID < Me.Keytbl1 And KeyDel![Recorded On] < LessDays Then
                KeyDel.Delete
                
        End If
            KeyDel.MoveNext
          
        Loop
End If
    Set Dbscurrent = Nothing
        Set KeyDel = Nothing
    
    
    ' [Keyencetbl1]
    ' Moving through record and deleting if the current record is less than
    ' the hishest ID on the server and less than 7 days prior to today
    ' The Keytbl2 is a subform located on the Menu form.  It is attached to
    ' a stored procedure.

If KeyDel1.RecordCount <> 0 Then
        KeyDel1.MoveFirst
        Do Until KeyDel1.EOF
            If KeyDel1!ID < Me.Keytbl2 And KeyDel1![Recorded On] < LessDays Then
                KeyDel1.Delete
        End If
            KeyDel1.MoveNext
        Loop
End If

    Set Dbscurrent = Nothing
        Set KeyDel1 = Nothing
    
DoCmd.SetWarnings True
              
' Count Records after delete and Calculating

DelSum = DCount("*", "Keyencetbl")
DelSum1 = DCount("*", "Keyencetbl1")
Delttl = DelCount + DelCount1 - DelSum - DelSum1
Blankttl = BlankCount + BlankCount1

' Set KeyStats Recordset

Dim rs1 As dao.Recordset, Dbscurrent1 As dao.Database
Set Dbscurrent1 = CurrentDb()
Set rs1 = Dbscurrent1.OpenRecordset("KeyStats")


' Open up KeyStats and enter in the amount deleted, Machine and Time and Date

If Delttl <> 0 Or Blankttl <> 0 Then
        rs1.AddNew
            rs1.Fields("RecDeleted") = Delttl
                rs1.Fields("Machine") = "Key7601"
                    rs1.Fields("RecordedOn") = Now()
                        rs1.Fields("BlankRowsDeleted") = Blankttl
                rs1.Update
            rs1.Close
        Dbscurrent1.Close
    Set rs1 = Nothing
Set Dbscurrent1 = Nothing
End If


' Close Alert Box and unlocked menu buttons

    DoCmd.Close acForm, "AlertMsg", acSaveNo
    Me.Command0.Enabled = True
    Me.Command1.Enabled = True
    Me.Command4.Enabled = True

ExitHandler:
 DoCmd.Close acForm, "AlertMsg", acSaveNo
 DoCmd.OpenForm "Menu"
    Me.Command0.Enabled = True
    Me.Command1.Enabled = True
    Me.Command4.Enabled = True
Exit Sub


ErrorBox:
  Select Case Err
    Case Is > 0
        MsgBox "[Error #100] Network Connection Failed! Please contact IT Department. Hit 'OK' and report [Error #100] to continue", vbExclamation, ""
        Resume ExitHandler
    Case Else
        MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
        Resume ExitHandler
    End Select



End Sub
 
Last item in a table is arbitrary - if you sort/order differently, the results may change every time.

If you have an extra item meeting your criteria, why not just subtract 1 from total?
Or if your id is an autoID, you could use Max() (or Not Dmax(...id))-- assuming you don't start renumbering.
 
Hey,

I have some records that get deleted if RawData and RecordedOn is blank. I am counting them because it is putting the amount of records deleted in a table.

However, the last record has to have RawData and RecordedOn blank. I just want to only count all records except for the last one

hi,

probably really dumb but cant you just count the records and minus one in lieu of the last record?

Dcount = whatever - 1

that or call a query and count that instead

thanks
 
Hello,

Thank is exactly what I did. After posting this I though about alittle more and tried the -1 and it worked. Im glad to know I wasn't way of base....Thanks again!
 
The simplest way is usually the best lol. There is no 'right or wrong' way. Its whatever works and delivers the result

Thanks
 

Users who are viewing this thread

Back
Top Bottom