Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21857

Creating a Questionnaire/Quiz dynamically at runtime

$
0
0
Hi there clever ones

I am trying to create a questionnaire/test at runtime dynamically. I have a combo box which holds three tests. On the click event I am creating a query and populating a new recordset with one record from the database. I am using an Access database and all this works ok - I have tested the contents of the recordset and it is fine.

I have managed to work out how I can create ten panels at runtime and place them in two vertical rows of 5. I have managed to update their capton to Question 1, Question 2 etc. I have also managed to add a label to each. I have used two for loops in order to position the frames in to vertical rows.

Here is my problem. I have my rocordset object which holds the following data in one record:

QuestionID TestID Question 1 Question 2 Question 3 Question 4 (up to queston 10) and each field is populated with the correct data. The hard part is assigning the fields contents to each label in the dynamically created control label array. I will paste the code that I have so far to show you where I am. Open to suggestions and help... Cheers

Code:

Option Explicit
' If you are adding an ActiveX control at run-time that is not referenced in your project, you need to declare it  as VBControlExtender.
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlText As VB.TextBox
Dim WithEvents ctlCommand As VB.CommandButton
Dim WithEvents ctlLabel As VB.Label
Dim WithEvents ctlOption As VB.OptionButton
Dim WithEvents ctlCheck As VB.CheckBox
Dim WithEvents ctlFrame As VB.Frame

Private Sub cboTest_Click()
Dim intPrimaryKey As Integer
Dim i As Integer
Dim strLblQuestion As String
Dim myField As Field
Dim j As Integer

    'get the primary key of the selection
    intPrimaryKey = cboTest.ItemData(cboTest.ListIndex)
   
    'Now get a recordset of all the questions for this test (ten of them)
   
    'connect to db
    EstablishDatabaseConnection
   
    'create the query
    strSQL = "SELECT * from tblQuestions WHERE TestID = " & intPrimaryKey
   
    'Run the query
    objADOCon.Execute (strSQL)
   
    'open the recordset
    OpenRecordset
    For j = 0 To objADORs.Fields.Count - 1
          'print out a copy of the field names of the recordset
            Debug.Print "Field " & j & " = " & objADORs.Fields(j).Name
    Next
       
              objADORs.MoveFirst
             
              'Create the first set of frames and add a lable to each (1 to 5 )
              For i = 1 To 5
                       
                      Load fraFrame(i)
                     
                      With fraFrame(i)
                          .Caption = "Question: " & i
                          .Visible = True
                          .Top = fraFrame(i - 1).Top + 1500
                      End With
                      Set ctlLabel = Controls.Add("VB.Label", "ctllblQuestion" & i, fraFrame(i))
                      ctlLabel.Visible = True
                      'HELP NEEDED HERE
                      'I want to do this but I know this doesnt work
                      'ctlLabel.caption = objadors! (okay the field name is Question 1 and its the third field in the recordset. How do I dynamically
                      ' add this within the loop obviously starting at the third field and progressing to the end of the record which is Question 10
                      'I need to do this dynamically.  All tests / questionnaires with have the same fields
            Next i
           
            'This for loop will create the frames in another vertical row to the side of the first set of frames
            For i = 6 To 10
             
                      Load fraFrame(i)
                      With fraFrame(i)
                          .Caption = "Question: " & i
                          .Visible = True
                          .Top = fraFrame(i - 6).Top + 1500
                          .Left = 8000
                      End With
            Next i
   
            'Once I figure out how to create and populate each label with the different questions I have another table in the database
            'which will hold data to help me create check boxes for true/ false questions or a number of option controls for
            'multiple choice questions
   
End Sub

Private Sub Command1_Click()


    Dim ctrl As Control
   
    For Each ctrl In Controls
        If TypeOf ctrl Is TextBox Then
            Debug.Print "Textbox: " & ctrl.Name
        ElseIf TypeOf ctrl Is Label Then
            Debug.Print "Label: " & ctrl.Name
        ElseIf TypeOf ctrl Is Frame Then
            Debug.Print "Frame: " & ctrl.Name
        ElseIf TypeOf ctrl Is CommandButton Then
            Debug.Print "CommandButton: " & ctrl.Name
        End If
    Next ctrl
       
End Sub


Private Sub Form_Load()
    'connect to database
    EstablishDatabaseConnection
   
    'Create a query
    strSQL = "Select * FROM tblTests"
   
    'Execute the query
    objADOCon.Execute (strSQL)
   
    'Open the recordset
    OpenRecordset
   
   
    'Populate the combo box
    With cboTest
        Do Until objADORs.EOF
            'Add the rewardsID and RewardDesc
            .AddItem (objADORs!TestDescription)
            .ItemData(.NewIndex) = (objADORs!TestID)
            objADORs.MoveNext
        Loop
    End With
    objADORs.Close
    Set objADORs = Nothing
End Sub


Viewing all articles
Browse latest Browse all 21857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>