某校学生会选举需要从学校数据库中随机抽取若干名学生作为监票人。该数据库文件名为school.mdb,其中数据表student存储有关学生学号(xuehao)、姓名(xingming)相关信息,括号内的内容为对应字段名。该程序编辑界面如图所示,相关对象名可参考标识图。
当主持人点击按钮“生成抽号”后,下方的标签会显示可抽取的学号姓名,一定时间后显示被抽取作为监票人的学号姓名。
'xxxss:学校学生数,kcq:可抽取
'xhxm:学号姓名,kcq:可抽取
Dim xxxss As Integer
Dim xhxm(3000) As String
Dim kcq(3000) As Boolean
'cq_Click:启用两个定时器
Private Sub cq_Click()
cqxhxm.Enabled = True
xskcqxhxm.Enabled = True
End Sub
Private Sub cz_Click() '初始化数组kcq,使每个元素数据都处于可显示状态
For i =" 0" To xxxss - 1
kcq(i) = True '①
Next i
End Sub
Private Sub xskcqxhxm_Timer() '若数组kcq第x个元素处于可抽取状态,则显示数组xhxm第x个元素
x =" Int(Rnd" * xxxss)
If kcq(x) Then xhxmbq.text = kcq(x) '②
End Sub
Private Sub Form_Load() '从数据库中提取需要的学号姓名相关数据并初始化数组kcq
Randomize
xxxss = 0
Dim conn As New ADODB.Connection,rs As New ADODB.Recordset
Dim str_conn as String,str_sql As String
str_conn = "driver="Microsoft" access driver(*.mdb);DBQ="&app.path&"\school.mdb";
conn.open str_conn
str_sql = "select * from students"
rs.open str_sql
Do While Not rs.eof
xxxss =" xxxss" + 1
xhxm(xxxss) = rs.fields("xuehao")&rs.fields("xingming")
rs.movenext
Loop
For i =" 0" To xxxss - 1
kcq(i) = True
Next i
End Sub
Private Sub cqxhxm_Timer() '决定抽取的学号姓名作为监票人
xskcqxhxm.Enabled = False
For i =" 0" To xxxss - 1
If xhxmbq.Caption =" xhxm(i)" Then kcq(i) = False
Next i
cqxhxm.Enabled = False
End Sub