我如何防止访问VBA的连续循环
我正在运行一个简单的过程,该过程将表1选中“是/否”,并使用yes,否或N/A更新我的表,如果记录不存在。
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
当我运行上述代码时,它似乎会得到东西并继续旋转,直到我不得不停止程序为止。我检查了桌子,只有大约一半的字段被填满了,其余的仍然是空白的。每次以不同的间隔,但从未完成。
如何防止这种情况陷入连续循环中?
I'm running a simple procedure that checks Table 1 for a Yes/No and updates my table with either a Yes, No, or N/A if the record does not exist.
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
When I run the above code, it seems to get stuff and continues to spin until I have to stop the program. I checked the table and only about half the fields are filled the rest remained blank. Each time it's at different intervals but never finishes.
How do I prevent this from getting caught in a that continuous loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于对表设计和关键字段的有限描述,这应该使用3个预设更新查询来工作。我敢肯定,您可以编辑它们以适合您的特定表格和所需的结果。
查询1:
查询2:
查询3:
Based on the limited description of your table design and the key fields, this should work using 3 preset update queries. I'm sure you can edit them to fit your specific tables and desired results.
QUERY 1:
QUERY 2:
QUERY 3: