Solved

Q&A datareader

Posted on 2006-11-09
9
178 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
  • 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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17915859
SirBounty,

Still need a hand with this one?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now