How to connect the option/radio button to listview control using ADODB connection?
That is,right after when you click a radio button as the letter of your choice..then it will automatically shown to the the listview control as the answer sheet..for the respective question..and numbering..? Using the VB 6.0..

Example:
1. What is the color of Ramon's red coat? <Answer Sheet>---- Listview1
a)blue b)red c)yellow d)rainbow -------> 1. a)blue
.
.
option button


I really don't have an idea how am i gonna do it! ..I don't know how to construct the code..

Dani AI

Generated

As asked for — clicking an option should immediately appear on an answer sheet ListView and be saved — and as hinted, a simple, reliable pattern is: show one question at a time with a small OptionButton control array for the choices; keep the current question ID/number in a variable; on option click add or update the ListView row keyed by that question; then persist the choice with ADODB. The code below demonstrates the ListView add/update logic and an outline for saving the answer. Use the ListView Key to guarantee one row per question (update if it exists, add if it doesn't).

' OptionChoice is a control array (indexes 0.. for a,b,c,d)
Private Sub OptionChoice_Click(Index As Integer)
    Dim choiceLetter As String
    Dim key As String
    Dim li As ListItem

    choiceLetter = Chr$(Asc("a") + Index)    ' 0->"a", 1->"b", ...
    key = "Q" & CStr(CurrentQID)             ' unique key for question

    On Error Resume Next
    Set li = ListView1.ListItems(key)        ' will error if not found
    On Error GoTo 0

    If li Is Nothing Then
        Set li = ListView1.ListItems.Add(Key:=key, Text:=CStr(CurrentQNum))
        li.SubItems(1) = choiceLetter
    Else
        li.SubItems(1) = choiceLetter
    End If

    SaveAnswer CurrentQID, choiceLetter
End Sub

' Minimal outline for saving with ADODB; set reference to ADO in VB6 (Project->References)
Private Sub SaveAnswer(qID As Long, chosen As String)
    Dim cn As ADODB.Connection
    Dim cmd As ADODB.Command

    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Exam.mdb;"
    Set cmd = New ADODB.Command
    With cmd
        .ActiveConnection = cn
        .CommandText = "UPDATE StudentAnswers SET Choice = ? WHERE StudentID = ? AND QuestionID = ?"
        .CommandType = adCmdText
        .Parameters.Append .CreateParameter("pChoice", adVarChar, adParamInput, 2, chosen)
        .Parameters.Append .CreateParameter("pStudent", adInteger, adParamInput, , CurrentStudentID)
        .Parameters.Append .CreateParameter("pQID", adInteger, adParamInput, , qID)
        .Execute
    End With
    cn.Close
End Sub

Notes and quick tips: set the ListView to report mode and add two columns (question number, chosen answer). Use the ListView Key string (e.g. "Q123") so finding/updating is fast. If you display multiple questions at once, store the question ID in each option button’s Tag property and read that in the Click handler instead of a global CurrentQID. Always add a reference to “Microsoft ActiveX Data Objects x.x Library” before using ADODB. For production, avoid raw string SQL — use parameterized commands (as sketched above), handle errors instead of blanket On Error Resume Next, and reuse a single open connection or wrap DB calls in transactions for performance and safety.

I don't think a listview control is what you want. I think it would look more like a normal question screen if you had an array of radio buttons. On your form, place one radio button, then copy and paste it, VB will ask if you want to create a control array, answer yes. then keep pasting until you have the desired number of radio buttons.

When you retrieve a record from the database populate the captions of the radio buttons.

Place some code under the click event of the control array to check if the user has selected the correct answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.