如何防止 Access VBA 中的连续循环

How do I prevent a continuous loop in Access VBA

我正在 运行ning 一个简单的过程来检查 Table 1 是否有 Yes/No 并用是、否或 [= 更新我的 table 19=] 如果记录不存在。

Public Function getAnswer() As Integer
Dim db As Database
Dim rst_MASTER As Recordset
Dim strSQL As String
Dim strSQL_Active as String
Dim strSQL_MASTER As String

'error handeling
On Error GoTo Err_Error

    DoCmd.SetWarnings False
    
    'updates all has bene flags if record with 401K type exists on bene file source
    strSQL = "UPDATE tbl_ACTIVE INNER JOIN tbl_FILE_SOURCE ON " & _
                "tbl_ACTIVE.Key= tbl_FILE_SOURCE.Key SET tbl_ACTIVE.isTrue= True " & _
                "WHERE tbl_FILE_SOURCE.PlanType='A';"
            
    DoCmd.RunSQL strSQL, True
    
    Set db = CurrentDb
    Set rst_MASTER = db.OpenRecordset("tbl_MASTER")
     
    If rst_MASTER.RecordCount > 0 Then
        rst_MASTER.MoveFirst
        
        Do Until rst_MASTER.EOF
            strSQL_Active = "SELECT tbl_ACTIVE.isTrue FROM tbl_ACTIVE WHERE tbl_ACTIVE.Key = " & rst_MASTER!Key

            rst_MASTER.Edit
            Select Case RetrieveVal(strSQL_Active, True)
                Case True
                    rst_MASTER![IsTrue] = "No"
                Case False
                    rst_MASTER![IsTrue] = "Yes"
                Case 0
                    rst_MASTER![IsTrue] = "N/A"
                Case Else
                    rst_MASTER![IsTrue] = "Err"
                    
            End Select
            
            rst_MASTER.Update
            rst_MASTER.MoveNext
        Loop
    End If

    getAnswer = 1

Err_Error:
     MsgBox Err.Number & Err.Description
     getAnswer = -1
       
End Function

当我 运行 上面的代码时,它似乎得到了东西并继续旋转,直到我不得不停止程序。我检查了 table 并且只有大约一半的字段被填充,其余的仍然空白。每次都在不同的时间间隔但永远不会结束。

如何防止它陷入连续循环?

根据您的 table 设计和关键字段的有限描述,这应该可以使用 3 个预设更新查询。我相信您可以编辑它们以适合您的特定 tables 和期望的结果。

查询 1:

UPDATE tbl_Master
INNER JOIN tbl_ACTIVE
ON tbl_ACTIVE.Key = tbl_Master!Key
SET tbl_Master.isTrue = "No"
WHERE tbl_ACTIVE![IsTrue] = True

查询 2:

UPDATE tbl_Master
INNER JOIN tbl_ACTIVE
ON tbl_ACTIVE.Key = tbl_Master!Key
SET tbl_Master.isTrue = "Yes"
WHERE tbl_ACTIVE![IsTrue] = False

查询 3:

UPDATE tbl_Master
INNER JOIN tbl_ACTIVE
ON tbl_ACTIVE.Key = tbl_Master!Key
SET tbl_Master.isTrue = "Err"
WHERE tbl_ACTIVE![IsTrue] Not In (True, False)

Note: You'll have to edit your 'RetrieveVal function to return something other than 0 for an EOF condition - your False condition evaluates to 0 already.