Solved

Q&A datareader

Posted on 2006-11-09
9
225 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
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!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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