In my main table, I have 10 variables with datatype "Yes/No".. each with a corresponding label "lblVar(i)" where i = 1-10. The same main table has a field called "Area". There is another table "tbQuestions" with two columns: "Question" and "Area".
On the main form, depending on which area is present in the recordset the 10 variables get assigned "questions" from the "tbQuestions"... and everything works flawlessly. This dynamic assignment of variables is made possible by the follow code.
\\START OF CODE\'Dynamically assign names to the variable for display and hide unused variables
Dim i As Integer
Dim strSQL As String
Dim rs As DAO.Recordset
'SQL String to pull the Questions
strSQL = "SELECT tbAuditQuestions.Question FROM tbAuditQuestions WHERE tbAuditQuestions.Area = " & Me.AreaID.Value & ";"
'Loop through records till you find the last record
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
With rs
rs.MoveFirst
i = 0
Do While Not rs.EOF
Me.Controls("lblVar" & i + 1).Caption = rs.Fields(0)
Me.Controls("Var" & i + 1).Visible = True
Me.Controls("lblVar" & i + 1).Visible = True
rs.MoveNext
i = i + 1
Loop
rs.Close
Set rs = Nothing
End With
\\END OF CODE\
My issues is when I try using this code in a report. I need each record to display a different set of questions (lblVars) depending on the area in that record but the report shows same questions for each record, taking the question values from the first record.
Which event should I put this code in so that each record runs the code and displays the corresponding questions for that record.
I know this is a bit tricky as I have extensively searched online without much help.. Hopefully you guys here have a solution.
Thank You!
On the main form, depending on which area is present in the recordset the 10 variables get assigned "questions" from the "tbQuestions"... and everything works flawlessly. This dynamic assignment of variables is made possible by the follow code.
\\START OF CODE\'Dynamically assign names to the variable for display and hide unused variables
Dim i As Integer
Dim strSQL As String
Dim rs As DAO.Recordset
'SQL String to pull the Questions
strSQL = "SELECT tbAuditQuestions.Question FROM tbAuditQuestions WHERE tbAuditQuestions.Area = " & Me.AreaID.Value & ";"
'Loop through records till you find the last record
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
With rs
rs.MoveFirst
i = 0
Do While Not rs.EOF
Me.Controls("lblVar" & i + 1).Caption = rs.Fields(0)
Me.Controls("Var" & i + 1).Visible = True
Me.Controls("lblVar" & i + 1).Visible = True
rs.MoveNext
i = i + 1
Loop
rs.Close
Set rs = Nothing
End With
\\END OF CODE\
My issues is when I try using this code in a report. I need each record to display a different set of questions (lblVars) depending on the area in that record but the report shows same questions for each record, taking the question values from the first record.
Which event should I put this code in so that each record runs the code and displays the corresponding questions for that record.
I know this is a bit tricky as I have extensively searched online without much help.. Hopefully you guys here have a solution.
Thank You!