循环遍历表并更改值时,访问崩溃

时间:2015-04-19 09:22:09

标签: ms-access crash recordset calculated-columns ms-jet-ace

当我循环遍历表并使用以下代码更改其中一个字段的值时,访问保持崩溃:

Set db = CurrentDb
Set rs = db.OpenRecordset(prProdTblName)
'PrevDept , PrevOpNo, PrevPartNo
rs.MoveFirst
rs.Edit
rs![Op No] = 10
rs.Update
Do Until rs.EOF
    With rs
        PrevOpNo = ![Op No]
        PrevDept = ![Department Code]
        PrevPartNo = ![Part No]
        .MoveNext
        .Edit
        If (![Department Code] = "SMD" And PrevDept <> "SMD") Then
            ![Op No] = 500
        ElseIf (![Department Code] <> "SMD" And PrevDept = "SMD" And ![Part No] = PrevPartNo) Then
            ![Op No] = 1000
        ElseIf (rs![Part No] <> PrevPartNo) Then
            ![Op No] = 10
        Else
            ![Op No] = PrevOpNo + 10
        End If
        .Update
    End With
Loop
rs.Close
Set rs = Nothing

此外,当我尝试使用替代SQL代码时,它似乎是一个无休止的查询:

SQLcommandStr = "SELECT T1.[ID], (Select IIF((T1.[Department Code]='SMD') AND (T2.[Department Code]<>'SMD'),500,IIF((T1.[Department Code]<>'SMD') AND (T2.[Department Code]='SMD') AND (T1.[Part No] = T2.[Part No]),1000,IIF((T1.[Part No] <> T2.[Part No] OR (T1.[ID] = (SELECT MIN(T4.[ID]) FROM " & prProdTblName & " T4))),10,T2.[Op No] + 10))) From " & prProdTblName & " T2 WHERE T2.[ID] = (SELECT MAX(T3.[ID]) FROM " & prProdTblName & " T3 WHERE T1.[ID] > T3.[ID])) INTO TblTemp FROM " & prProdTblName & " T1"

DoCmd.RunSQL SQLcommandStr

1 个答案:

答案 0 :(得分:0)

你可能会在EOF上反弹:

Set db = CurrentDb
Set rs = db.OpenRecordset(prProdTblName)
rs.MoveFirst
rs.Edit
rs![Op No] = 10
rs.Update
Do Until rs.EOF
    With rs
        PrevOpNo = ![Op No]
        PrevDept = ![Department Code]
        PrevPartNo = ![Part No]
        .MoveNext
        If .EOF = False Then
            .Edit
            If (![Department Code] = "SMD" And PrevDept <> "SMD") Then
                ![Op No] = 500
            ElseIf (![Department Code] <> "SMD" And PrevDept = "SMD" And ![Part No] = PrevPartNo) Then
                ![Op No] = 1000
            ElseIf (rs![Part No] <> PrevPartNo) Then
                ![Op No] = 10
            Else
                ![Op No] = PrevOpNo + 10
            End If
            .Update
        End If
    End With
Loop
rs.Close
Set rs = Nothing