Solved

Q&A datareader

Posted on 2006-11-09
9
192 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 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

22 Experts available now in Live!

Get 1:1 Help Now