Question

I currently working on VBA code that can loop through my access table by using DAO recordset, i have to find any data is 90 days before the current time will have copy and insert into other table as second recordset, after insert into the second recordset, then delete the data and move next to next record of data. my code is below and I currently got a runtime error "3265" Item not found in this collection.

    Private Sub Command22_Click()

        Dim dbs As DAO.Database
        Dim rsDatalog1 As DAO.Recordset
        Dim rsDatalog2 As DAO.Recordset
        Dim time As Date
        time = Now() - 90
        DoCmd.SetWarnings False
        DoCmd.Echo False
        DoCmd.Hourglass True
        Set dbs = CurrentDb()
        Set rsDatalog1 = dbs.OpenRecordset("SELECT DateStamp, LocationID, DataType, LogValue FROM DataLog")
        Do Until rsDatalog1.EOF


                If Not rsDatalog1.EOF Then
                rsDatalog1.MoveFirst
                If Not rsDatalog1.EOF Then
                    rsDatalog1.MoveNext
                    Do Until rsDatalog1.EOF

                        If rsDatalog1.Fields(Datastamp) >= Now() - 90 Then
                        Set reDatalog2 = dbs.OpenRecordset("INSERT INTO Archive VALUE ('" & rsDatalog1("DataStamp") & "', '" & rsDatalog1("LocationID") & "','" & rsDatalog1("DataType") & "','" & rsDatalog1("LogValue") & "'")
                        Debug.Print redatalog1.Field(DateStamp, LocationID, DataType, LogValue)
                        rsDatalog1.Delete
                        rsDatalog1.MoveNext
                        End If
                    Loop
                End If
            End If
            rsDatalog1.Close
            rsDatalog2.Close
        Loop
        DoCmd.Hourglass False
        MsgBox "Finish"
End Sub
Was it helpful?

Solution

There are a few possible causes to this issue. The first is when you have If rsDatalog1.Fields(Datastamp) >= Now() - 90 Then DataStamp should be in quotes. You can also reference fields with an ! such as rsDatalog1!Datastamp.

You shouldn't have your rsDatalog1.close inside your loop. This will cause it to fail on its next pass since it won't be able to check for end of file.

You only need one loop instead of two.

Your insert statement needs to list the fields that you are inserting before you list out the values that are being inserted.

Private Sub Command22_Click()    
    Dim dbs As DAO.Database
    Dim rsDatalog1 As DAO.Recordset
    Dim time As Date
    time = Now() - 90
    DoCmd.SetWarnings False
    DoCmd.Echo False
    DoCmd.Hourglass True
    Set dbs = CurrentDb()
    Set rsDatalog1 = dbs.OpenRecordset("SELECT DateStamp, LocationID, DataType, LogValue FROM DataLog")
    Do Until rsDatalog1.EOF
        If rsDatalog1.Fields(Datastamp) >= Now() - 90 Then
            dbs.Execute "INSERT INTO Archive (DataStamp, LocationID, DataType, LogValue) VALUE ('" & rsDatalog1!DataStamp & "', '" & rsDatalog1!LocationID & "','" & rsDatalog1!DataType & "','" & rsDatalog1!LogValue & "')", dbFailOnError
            rsDatalog1.Delete
        End If

        if rsDatalog1.recordCount <> 0 then
            rsDatalog1.MoveNext
        end if
    Loop

    rsDatalog1.close
    set rsDatalog1 = nothing
    DoCmd.Hourglass False
    MsgBox "Finish"
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top