|
小布点把这个小程序做一下。
在Access建立数据库D:\db1.mdb
建立表student(ID,Name)
建立成绩表Score(id,Subject,Score,SturentID)
起中字段Subject是科目,只有s1,s2,s3,s4,s5等5门课
SturentID是学生ID,来自student表
在VB工程中引用ADO
代码如下:
Dim Conn as new ADODB.CONNECTION
sub Form_load()
'建立数据库连接
Conn.open "rovider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\db1.mdbersist Security Info=False"
End sub
Sub Commond1_OnClick()
'显示查找符合奖励的学生
Dim rs as new ADODB.RECORDSET
dim sSQL as string
Dim sStudentID as string
'查询得到总分>=450的学生
sSQL="Select studentid From score Where (s1+s2+s3+s4+s5)>=450 Order by ScoreSum"
sStudentID=""
rs.open sSQL,Conn,3,1
if not rs.eof
sStudentID=rs.getstring(adClipString, -1, ",", ",", "0")
end if
rs.close
'查询得到门课分>=88的学生,但不包括上面的得奖学生
sSQL="Select studentid From score Where s1>=88 and s2>=88 and s3>=88 and s4>=88 and s5>=88 and studentid Not in(" & sStudentID & ")"
Dim sStudentID1 as string
rs.open sSQL,conn,3,1
if not rs.eof then
sStudentID1 =rs.getstring(adClipString, -1, ",", ",", "0")
end if
rs.close
dim sTemp as string
Dim sTudentID3 as string
if right(sStudentID,1)="," then sStudentID=mid(sStudentID,1,len(sStudentID)-1)
if left(sStudentID1,1)<>"," then sStudentID1="," & sStudentID1
stemp=sStudentID & sStudentID1
'查询得到门课分>=88的学生,但不包括上面的得奖学生
sSQL="Select studentid From score Where s1>=95 and s2>=95 and s3>=95 and s4>=80 and s5>=80 and studentid Not in(" & stemp & ")"
rs.open sSQL,conn,3,1
if not rs.eof then
sStudentID3 =rs.getstring(adClipString, -1, ",", ",", "0")
end if
rs.close
if right(stemp,1)="," then stemp=mid(stemp,1,len(stemp)-1)
if left(sStudentID3,1)<>"," then sStudentID3="," & sStudentID3
stemp=stemp & sStudentID3 '得到了符合三个条件之一的学生的ID
'下面可以得到学生的姓名及各课的分数
sSQL="Select a.Name,b.s1,b.s2.b.s3,b.s4,b.s5 from student a,score b Where a.id=b.studentid and a.id in(" & stemp & ")
rs.open sSQL,Conn,3,1
do until rs.eof
debug.print rs("name") & "," & rs("s1") & "," & rs("s2") & "," & rs("s3") & "," & rs("s4") & "," & rs("s5")
rs.movenext
loop
rs.close
End sub
|
|