VB程序运行界面如图2所示,在文本框Text1中输入查询的等级,单击“查询”按钮Command1,在列表框List1中显示所有该等级的学生学号和姓名,并按照学号从小到大排序,并在标签Label2处显示学生的人数,如果人数为0,则在列表框中显示“没有该等级的学生”。按此要求编写程序如下,但加框处代码有错误,请改正。
Private Sub Command1_Click()
Dim stuna(1 To 100) As String '存放学生姓名的数组定义为stuna
Dim stunum(1 To 100) As String '存放学生学号的数组定义为stunum
Dim i As Integer, j As Integer, n As Integer
Dim t As String
'连接数据库
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL As String
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + App.Path + "\stugrade.accdb"
cn.Open
strSQL = "select * from Chinese where 语文等级='" + Text1.Text + "'"
Set rs.ActiveConnection = cn
rs.Open strSQL
n = 0
Do While Not rs.EOF
n = n + 1
stuna(n) = rs.Fields("姓名").Value
stunum(n) = rs.Fields("学号").Value
rs.NextMove '①
Loop
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
List1.Clear '清除列表框
If n = 0 Then
List1.AddItem "没有该等级的学生"
Else
For i = 1 To n - 1 '按姓名排序
For j = n To i + 1 Step -1
Ifstuna(j) < stuna(j - 1)Then '②
t = stunum(j): stunum(j) = stunum(j - 1): stunum(j - 1) = t
t = stuna(j): stuna(j) = stuna(j - 1): stuna(j - 1) = t
End If
Next j
Next i
For i = 1 To n
List1.AddItem stunum(i) + " " + stuna(i)
Next i
Label2.Caption = "该等级的学生共有" + Str(n) + "名"
End If
End Sub