rs.movenext不起作用-运行时错误3021

时间:2019-02-01 13:20:41

标签: vba loops ms-access recordset

我尝试对表“ dbo.Bauteilspezifikation”的所有条目进行爬网,并使用标识符在Do-Loop中调用另一个函数。

你好, 我使用Access 2010年从SQL Server 2012中获取数据在这里,我尝试检查一个表的所有条目,并对其进行修改,如果在某个日期过期。 想法是将标识符“ BauteilID”用作函数“ Check_Freigabe”的变量。 但是在第一次循环之后,记录集似乎丢失了。这可能是因为在“ Check_Freigabe”函数中创建了一个新的记录集。

我试图创建另一个记录(除RS RS2),但后来,我得到一个错误,当访问告诉我,该记录被关闭。

有什么想法吗?

Public Sub Update_Freigabe()
Dim stmt As String
Dim bt_id As Long

    stmt = " Select BauteilID from dbo.Bauteilspezifikation " & _
            " where gesperrt = '0' order by BauteilID asc; "
    'Debug.Print (stmt)
    getSelect (stmt)

    rs.MoveFirst
    Do Until rs.EOF Or rs.BOF
        bt_id = rs(0)
        Call Check_Freigabe(bt_id)
        rs.MoveNext
    Loop

End Sub

Function getSelect(stmt As String)
    'Debug.Print stmt
    getConnection
    getRecordset
    Set rs = conn.Execute(stmt)
End Function
Public Function Check_Freigabe(id As Long)
Dim stmt, test, NewestDate As String

 stmt = " SELECT MBS.Stahlwerk, MBS.Freigabe, V.BauteilID, MAX(V.Datum) as NewestDate " & _
        " FROM dbo.MapBauteilStahlwerk as MBS INNER JOIN dbo.Verwendungsfreigabe as V ON MBS.BauteilID = V.BauteilID " & _
        " INNER JOIN dbo.Stahlwerkzeugnis AS SZ ON SZ.Stahlwerk = MBS.Stahlwerk AND V.StahlwerkID = SZ.StahlwerkID " & _
        " WHERE MBS.BauteilID = " & id & "  " & _
        " GROUP BY MBS.Stahlwerk, MBS.Freigabe, V.BauteilID "
    'Debug.Print (stmt)
    getSelect (stmt)

    If rs.EOF Or rs.BOF Then
        'MsgBox ("test")

        Exit Function
    Else
        If DateDiff("d", rs!NewestDate, Now) >= 365 And rs!Freigabe = "Frei" Then
            stmt_new = " INSERT into dbo.MapBauteilStahlwerk (BauteilID, Stahlwerk, Freigabe)" & _
                        " SELECT  BauteilID, Stahlwerk, 'Frei (> 12 Monate)'" & _
                        " FROM dbo.MapBauteilStahlwerk WHERE bauteilID = '" & id & "' and Stahlwerk = '" & rs!Stahlwerk & "';"
            'Debug.Print (stmt_new)
            insert (stmt_new)
        End If
    End If
End Function

在每个循环中,我希望获得下一个bt_id,我可以用它来调用“Check_Freigabe”

1 个答案:

答案 0 :(得分:0)

rs.MoveFirst { this makes it so that rs.BOF=True }
    Do Until rs.EOF Or rs.BOF { condition met, loop never runs }

正确的循环:

rs.MoveFirst
Do While Not rs.EOF
    [Code]
    rs.MoveNext
  Loop