0
votes

I'm facing an error, please help, the debugger keep pointing at SaveToFile line, anyone know what's wrong with my code?

Private Sub Form_Timer()

Dim rst As DAO.Recordset2
Dim rsA As DAO.Recordset2
Dim fld As DAO.Field2

Dim db As DAO.Database
Set db = CurrentDb

If DCount("FileName", "Query1") > 0 Then
    Set rst = db.OpenRecordset("Query1")
    Set rsA = rst.Fields("Attachments").Value

    If Not (rst.EOF And rst.BOF) Then
        rst.MoveFirst
        Do Until rst.EOF = True
            rsA.Fields("FileData").SaveToFile "D:\" & rst.Fields("FileName").Value & rsA.Fields("FileType")
            rst.Edit
            rst!ExportedFlag = "Yes"
            rst.Update
            rst.MoveNext
        Loop
    Else
        MsgBox "There are no new records in the database."
    End If

    MsgBox "Finished looping through records."

    rst.Close
    Set rst = Nothing
    Set rsA = Nothing
    Set fld = Nothing
    Set db = Nothing
End If

My error message:

My Error ScreenShot

1
you want us to debug your code for you? That's not going to happen! - Mitch Wheat

1 Answers

0
votes

Once you move in rst, your rsA is no longer valid.

You need to set rsA inside the loop.

Do Until rst.EOF = True
    Set rsA = rst.Fields("Attachments").Value
    rsA.Fields("FileData").SaveToFile "D:\" & rst.Fields("FileName").Value & rsA.Fields("FileType")