Solved

Q&A datareader

Posted on 2006-11-09
9
217 Views
Last Modified: 2010-08-05
I have an app that pulls in data from an Access db.
There are two tables - Questions & Answers

I need my form load to call a sub, LoadQuestion, which should read a field named [Correct]
If this field has only one character, display x number of radio buttons ([C])
If it's multiple characters (comma seperated), display x number of check boxes ([B,E])

My default instances are radOption and chkOption.
I'm also using:
Dim cn As OleDbConnection, cmd As OleDbCommand, dr As OleDbDataReader
Dim strCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\Quizz.mdb"

and in my form load,
cmd = New OleDbCommand("SELECT QuesID, QuestionText, OptA, OptB, OptC, OptD, OptE, OptF, Correct FROM tblQuestions, tlbAnswers WHERE QuestionID=AnswerID", cn)

Anyone willing to help me get this running? :^)
0
Comment
Question by:sirbounty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 10

Accepted Solution

by:
gangwisch earned 500 total points
ID: 17906097
cmd = New OleDbCommand("SELECT QuesID, QuestionText, OptA, OptB, OptC, OptD, OptE, OptF, Correct FROM tblQuestions, tlbAnswers WHERE QuestionID=AnswerID", cn)
dim dr as oledbdatareader = cmd.executereader()
if dr.read then
if len(dr("Correct"))=1 then
'make radio button
dim a as new radiobutton
a.text=dr("opta")
dim b as new radiobutton
b.text=dr("optb")
dim c as new radiobutton
c.text=dr("optc")
dim d as new radiobutton
d.text=dr("optd")
dim e as new radiobutton
e.text=dr("opte")
dim f as new radiobutton
f.text=dr("optf")
me.controls.add(a)
me.controls.add(b)
me.controls.add(c)
me.controls.add(d)
me.controls.add(e)
me.controls.add(f)
else
'make check box
dim a as new checkbutton
a.text=dr("opta")
dim b as new checkbutton
b.text=dr("optb")
dim c as new checkbutton
c.text=dr("optc")
dim d as new checkbutton
d.text=dr("optd")
dim e as new checkbutton
e.text=dr("opte")
dim f as new checkbutton
f.text=dr("optf")
me.controls.add(a)
me.controls.add(b)
me.controls.add(c)
me.controls.add(d)
me.controls.add(e)
me.controls.add(f)
end if
end if
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17907250
How would I reference the created control?
For instance, if I have a variable/array (CorrectAnswer), how would I validate that the correct radio/checkbox was clicked?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17908800
This is working for me - but I'm not sure how to reference that the correct answer(s) were chosen.  I can't seem to position the controls properly either... :|  They look 'ok', but it seems I've got a straggler at the top, for some reason...
       
        For Each dR In dv.Table.Rows 'ds.Tables(0).Rows
            With dR
                If Len(dR.Item("Correct")) = 1 Then
                    'make radio button
                    a = New RadioButton
                    b = New RadioButton
                    c = New RadioButton
                    d = New RadioButton
                    e = New RadioButton
                    f = New RadioButton
                Else
                    'make check box
                    a = New CheckBox
                    b = New CheckBox
                    c = New CheckBox
                    d = New CheckBox
                    e = New CheckBox
                    f = New CheckBox
                End If
 
               With Me.Controls
                    .Add(a)
                    .Add(b)
                    .Add(c)
                    .Add(d)
                    .Add(e)
                    .Add(f)
                End With

                With a
                    .Text = dR("optA")
                    .Location = New System.Drawing.Point(12, 56)
                    .Width = 400
                End With
                With b
                    .Text = dR("optB")
                    .Location = New System.Drawing.Point(12, 102)
                    .Width = 600
                End With
                With c
                    .Text = dR("optC")
                    .Location = New System.Drawing.Point(12, 148)
                    .Width = 800
                End With
                With d
                    .Text = dR("optD")
                    .Location = New System.Drawing.Point(12, 194)
                    .Width = 800
                End With
                With e
                    If Not IsDBNull(dR("optE")) Then
                        .Text = dR("optE")
                        .Location = New System.Drawing.Point(12, 240)
                        .Width = 800
                    Else
                        e.Visible = False
                    End If
                End With
                With f
                    If Not IsDBNull(dR("optF")) Then
                        .Text = dR("optF")
                        .Location = New System.Drawing.Point(12, 285)
                        .Width = 800
                    End If
                End With
            End With
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17915859
SirBounty,

Still need a hand with this one?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17915871
Yes - I've opened another question related to it.
Part of this has been answered - how to distinguish between the radio/checkbox...
So,perhaps I should close this and work off the other to avoid a duplicate question.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17916199
Fair enough.

Just as a minor note.... you might want to consider refactoring this so that your code is more maintainable...
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17916231
Not sure I follow you there?  Did I get off on the wrong foot anyways? : \
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17916333
Refactoring is rearranging your code so it doesn't hurt to look at it... or make changes :)

So, the above code might look like:

Public Sub MakeControls()
    For Each dr In dv.Table.Rows
      If Len(dR.Item("Correct")) = 1 Then
        MakeRadioButtons(dr)
      Else
        MakeCheckBoxes(dr)
      End If
    Next
  End Sub

  Public Sub MakeCheckBoxes(ByVal dr As DataRow)
    MakeCheckBox("A", dr("optA"), 12, 56, 400)
    MakeCheckBox("B", dr("optB"), 12, 102, 600)
    MakeCheckBox("C", dr("optC"), 12, 158, 800)
    MakeCheckBox("D", dr("optD"), 12, 224, 800)
    MakeCheckBox("E", dr("optE"), 12, 280, 800)
    MakeCheckBox("B", dr("optF"), 12, 336, 800)
  End Sub

  Public Sub MakeCheckBox(ByVal letter As String, ByVal text As Object, ByVal top As Integer, ByVal left As Integer, ByVal width As Integer)
    If Not text Is DBNull.Value Then
      Dim chk As New CheckBox
      chk.Text = letter & ") " & text.ToString
      chk.Top = top
      chk.Left = left
      chk.Width = width
      Me.Controls.Add(chk)
    End If
  End Sub

  Public Sub MakeRadioButtons(ByVal row As DataRow)

  End Sub

  Public Sub MakeRadioButton()

  End Sub
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17916751
Thanx for that - I'll look into adjusting it (and was going to at some point down the line).
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question