对象无效或不再设置

时间:2018-05-21 03:10:15

标签: vba ms-access access-vba

我遇到错误,请帮忙,调试器一直指向SaveToFile行,有人知道我的代码有什么问题吗?

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 ScreenShot

1 个答案:

答案 0 :(得分:0)

移入rst后,您的rsA不再有效。

您需要在循环内设置rsA

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