vb+ado+access+批量计算,
有一MDB表:姓名 语文 数学 英语 总分 名次,
,想通过vb+ado+access批量计算出每一个人的总分,总分=语文+数学+英语,并用总分排出名次且将每一个人的总分及名次再写入原表中。
參考答案:这个不难,但是VB程序在这里贴出来很难,
我已编好,并调试成功。
请用站内短消息发个邮箱给我,我好传过来
程序大致如下:
Public ObjFso As New FileSystemObject
Private Sub Command1_Click()
Me.CommonDialog1.ShowOpen
Me.Text1.Text = Me.CommonDialog1.FileName
End Sub
Private Sub Command2_Click()
SystemFileName = Me.Text1.Text
'建立一个数据库连接
Dim ObjConnTmp As ADODB.Connection, ObjRsTmp As ADODB.Recordset
Set ObjConnTmp = FunDB_CONNECT(SystemFileName)
StrTmpSQL = "update " & Text2.Text & " set 总分=语文+ 数学+ 英语"
ObjConnTmp.Execute StrTmpSQL
StrTmpSQL = "Select 姓名, 语文, 数学, 英语, 总分, 名次 from " & Text2.Text & " order by 总分 desc"
Set ObjRsTmp = FunRecordSet(StrTmpSQL, ObjConnTmp)
n = 0
Do Until ObjRsTmp.EOF
n = n + 1
ObjRsTmp("名次") = n
ObjRsTmp.Update
ObjRsTmp.MoveNext
Loop
ObjRsTmp.Close: Set ObjRsTmp = Nothing
ObjConnTmp.Close: Set ObjConnTmp = Nothing
MsgBox "已操作完毕,请查看您的数据表"
End Sub
Private Sub Form_Load()
Me.Text1.Text = App.Path & "\成绩表.mdb"
End Sub
'请注意在进行数据库操作时需要先引用 Microsoft ActiveX Data Objects 2.8 Library
'注意在引用FSO功能时先引用 Microsoft Scripting Runtime
Public Function FunDB_CONNECT(ByVal DataSource As String) As ADODB.Connection
'该子程序用来连接数据库
'输入: 数据库名,包括路径
'返回: Connection 对象
'If ISCONNECT = True Then SubDB_DISCONNECT
Do Until ObjFso.FileExists(DataSource)
MsgBox ("数据库文件不存在" & vbCrLf & "请指定数据库文件路径")
FrmMain.CommonDialog1.ShowOpen
DataSource = FrmMain.CommonDialog1.FileName
If DataSource = "" Then
If MsgBox("数据库加载失败!真的要退出吗?", vbOKCancel) = vbOK Then Exit Function
End If
Loop
Dim ObjConnTmp As New ADODB.Connection
ObjConnTmp.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DataSource & ";"
'打开到数据库的连接
ObjConnTmp.Open
If ObjConnTmp.State <> adStateOpen Then
MsgBox "数据库连接失败"
End
End If
'返回Connection对象给函数
Set FunDB_CONNECT = ObjConnTmp
'*****************引用范例************************
' Dim ObjConnTmp As ADODB.Connection
' StrPath = App.Path & "\Setting\"
' DataSource = StrPath & "Setting.mdb"
' Set ObjConnTmp = FunDB_CONNECT(DataSource)
'*****************引用范例************************
End Function
Public Function FunRecordSet(ByVal StrTmpSQL As String, ByVal ObjConnTmp As ADODB.Connection) As ADODB.Recordset
'本子函数功能: 据SQL字符串和connection对象对数据进行连接
'返回: RECORDSET 对象
Dim ObjTmpRs As New ADODB.Recordset
' SubDB_CONNECT (DataSource)
'
Set ObjTmpRs.ActiveConnection = ObjConnTmp
ObjTmpRs.CursorType = adOpenDynamic
ObjTmpRs.LockType = adLockOptimistic '设置游标类型
ObjTmpRs.Open StrTmpSQL
Set FunRecordSet = ObjTmpRs
'注意不能在这时候断开数据库连接
'**************调用范例***************
' Dim StrTmpSQL As String
' Dim ObjRsTmp As New ADODB.Recordset
'
' StrTmpSQL ="select * from " & TableName '= Me.TxtSelectSQL.Text
' Set ObjRsTmp = FunRecordSet(StrTmpSQL, ObjConnFun)
' ObjRsTmp.Close
' Set ObjRsTmp = Nothing
'****************************************
End Function