Hello all. I need some help building VBA code to loop through two recordsets. I have a local table like this:
Question Response
1 2
2 2
3 1
4 1
I want to evaluate each entry in that table based on a set of rules. The rules are created by my users and as an example, might look like this:
Question 1 <> 3 (cannot equal 3)
Question 2 = 2
Question 4 <> 1
The local rules table has a variable amount of rows. The questions and responses to be evaluated are queried from a table, and that recordset will have approx. 250 rows.
So I would like to loop through the question recordset with each rule and if a rule is violated, note that and at the end of the loop present a msgbox telling a user the rules that were violated, along with the rule(s) so they can make corrections.
I found this code and thought it may work with modifications:
Is this the best approach? If you could help with the missing bits commented above it would be much appreciated.
Question Response
1 2
2 2
3 1
4 1
I want to evaluate each entry in that table based on a set of rules. The rules are created by my users and as an example, might look like this:
Question 1 <> 3 (cannot equal 3)
Question 2 = 2
Question 4 <> 1
The local rules table has a variable amount of rows. The questions and responses to be evaluated are queried from a table, and that recordset will have approx. 250 rows.
So I would like to loop through the question recordset with each rule and if a rule is violated, note that and at the end of the loop present a msgbox telling a user the rules that were violated, along with the rule(s) so they can make corrections.
I found this code and thought it may work with modifications:
Code:
Set rst1 = db.OpenRecordset("qselQuestions")
Set rst2 = db.OpenRecordset("qselRules")
rst2.MoveFirst
lngTotalRowsExist = 0
lngTotalsRowsMatched = 0
Do Until rst2.EOF
lngTotalRowsExist = lngTotalRowsExist + 1
' this bit below will need to be altered to handle the not-equal rules
strCriteria = "[question] = & rst2("question") & _
" AND [response] = rst2("response")
rst1.FindFirst strCriteria
If rst1.NoMatch = False Then
lngTotalRowsMatched = lngTotalRowsMatched + 1
End If
rst1.MoveNext
Loop
If lngTotalsRowsMatched > 0
strMsg = lngTotalRowsMatched & " rules of the " & _
lngTotalRowsExist & " total rules in the rule set have been " & _
"violated and need to be corrected!"
' then added to this I would like to show the rules violated
MsgBox strMsg
else
' proceed with processing
End If
rst1.Close
rst2.Close
Is this the best approach? If you could help with the missing bits commented above it would be much appreciated.