rs.movenext 不工作 - 运行 时间错误 3021

rs.movenext does not work - run time error 3021

我尝试遍历 table "dbo.Bauteilspezifikation" 的所有条目,并使用标识符在 Do-Loop 中调用另一个函数。

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

我尝试创建另一个记录集(除 rs 之外的 rs2),但随后出现错误,Access 告诉我记录集已关闭。

有什么想法吗?

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"

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