randomly generate 40 questions in vb6? please help me
i've been trying to create a vb6 code that will randomize 10 questions but it's not working. i use sql as my database
here's my code:
Private Sub cmdNext_Click()
Dim real_ans As String
Dim nCnt As Integer
'nCnt = nCnt + 2
'Label3.Caption = nCnt
real_ans = Adodc1.Recordset.Fields("answer")
With Adodc2.Recordset
Dim grade As String
If (real_ans = "A" And Option1.Value = True) Or (real_ans = "B" And Option2.Value = True) Or (real_ans = "C" And Option3.Value = True) Or (real_ans = "D" And Option4.Value = True) Then
.Fields("english_score").Value = .Fields("english_score").Value + 1
nEnglish_score = Adodc2.Recordset.Fields("english_score").Value
Select Case nEnglish_score
Case Is >= 7 'If score above 80...
grade = "genius" 'Give an A
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 6 'If score above 70...
grade = "Superior " 'Give a B
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 5 'If score above 60...
grade = "High Average" 'Give a C
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 4 'If score above 50...
grade = "Average" 'Give a D
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 3 'If score above 40...
grade = "Low Average " 'Give a E
With Adodc2.Recordset
开发者_StackOverflow.Fields("IQ").Value = grade
End With
Case Is >= 2 'If score above 30...
grade = "Moron" 'Give a F
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Is >= 1 'If score above 20...
grade = "Idiot " 'Give a G
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
Case Else 'Else
grade = "xxxx" 'Give a G
With Adodc2.Recordset
.Fields("IQ").Value = grade
.Update
End With
End Select
End If
Adodc1.Recordset.MoveNext
End With
End Sub
Private Sub Command1_Click()
frmExam2.Show
frmExam2.SetFocus
End Sub
Private Sub Form_Initialize()
Command1.Enabled = False
With Adodc2.Recordset
.AddNew
.Fields("name").Value = cStudent_Name
.Fields("section").Value = cStudent_Section
.Fields("english_score").Value = "0"
.Fields("math_score").Value = "0"
.Fields("abstract").Value = "0"
.Fields("total_average").Value = "0"
.Fields("IQ").Value = "0"
.Update
End With
End Sub
Private Sub Text1_Change()
If Text1.Text <> "" Then
cmdNext.Enabled = True
Else
Command1.Enabled = True
cmdNext.Enabled = False
End If
End Sub
Please help me i cant figure out whats wrong.
yep what's the error message?
- you don't save any space with this 'with':
With Adodc2.Recordset
.Fields("IQ").Value = grade
End With
- you can remove that block of code to the end (after End Select), since its in every Case statement anyway. So compress it down to:
Adodc2.Recordset.Fields("IQ").Value = grade
- why not have your option values and answers stored in the same way so you can just do a single comparison, like: if real_ans = OptionValue then ...
for random numbers, try this:
Function Rand(max As Long, Optional Min As Long) As Long
Dim s As Single
s = Rnd(1) * (max - Min + 1) + Min - 0.5
Rand = CLng(Round(s, 0))
End Function
and put RANDOMIZE in your form_load
精彩评论