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?
tbl_Masterwith an inner join totbl_ACTIVEwould suffice, no? Just the same, what isRetrieveValdoing?tbl_MASTERthat needs updating. Doing this relationally instead of looping would make more sense.Dima numeric variable, iterate it by 1 every time, and then every few loops (say 20) useIf CountLoops Mod 20 = 0 then Doevents. That will stop it from 'not responding' but will also slow down the Macro - however, if you're running a loop on 145000 records, you should be expecting a slow macro already!