回复 2楼 三断笛
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public SQL As String
Public db As Database
Public temp As Integer
Private Sub Form_Load()
Dim sum As Double
Dim textfirst1 As Double
Dim textsecond1 As Double
Dim textthird1 As Double
Dim textforth1 As Double
sum = 0
I = 0
cover.Show
Set db = Workspaces(0).OpenDatabase(App.Path & "\db2.mdb", False, False, "MS Access;PWD=ZXDK")
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\db2.mdb;Persist Security Info=False;Jet OLEDB:Database Password=ZXDK" '设置数据库路径
conn.CursorLocation = adUseClient
conn.Open
SQL = "select * from 学生信息表"
rs.Open SQL, conn, 3, 3
If rs.BOF Or rs.EOF Then
MsgBox "数据库没有记录!", vbInformation, "提示"
Exit Sub
Else
Set DataGrid1.DataSource = rs
display
Do While Not rs.EOF
Textfirst = Val(rs!第一次划款 & "")
Textsecond = Val(rs!第二次划款 & "")
Textthird = Val(rs!第三次划款 & "")
Textforth = Val(rs!第四次划款 & "")
textfirst1 = textfirst1 + Val(Textfirst)
textsecond1 = textsecond1 + Val(Textsecond)
textthird1 = textthird1 + Val(Textthird)
textforth1 = textforth1 + Val(Textforth)
total = total + 1
cover.ProgressBar1.Value = Int((I / rs.RecordCount) * 100)
I = I + 1
rs.MoveNext
Loop
sum = textfirst1 + textsecond1 + textthird1 + textforth1
Labeltotal.Caption = "数据库共有记录:" & total & "条"
Labelsum.Caption = "总贷款金额:" & sum & "元"
Labfirst.Caption = "第一次:" & textfirst1 & "元"
Labsecond.Caption = "第二次:" & textsecond1 & "元"
Labthird.Caption = "第三次:" & textthird1 & "元"
Labforth.Caption = "第四次:" & textforth1 & "元"
End If
Unload cover
Labsit.Caption = "状态:正常"
End Sub
Private Sub cmdclear_Click()
Textname.Text = ""
Textname.Text = ""
Textsex.Text = ""
Textxuehao.Text = ""
Textsf.Text = ""
Textzy.Text = ""
Textaddr.Text = ""
Textyb.Text = ""
Textht.Text = ""
Textbiye.Text = ""
Textfirst.Text = ""
Textfirsttime.Text = ""
Textsecond.Text = ""
Textsecondtime.Text = ""
Textthird.Text = ""
Textthirdtime.Text = ""
Textforth.Text = ""
Textforthtime.Text = ""
End Sub
Private Sub cmddelall_Click()
If MsgBox("( 慎!) 该操作将删除全部记录且不可恢复!确认删除吗?", vbYesNo + vbExclamation, "确认删除") = vbYes Then
rs.Close
SQL = "delete * from 学生信息表"
rs.Open SQL, conn, 3, 3
Else
Exit Sub
End If
MsgBox "记录已全部删除,请重新运行系统。", vbInformation, "提示"
Unload Me
End Sub
Private Sub cmdend_Click()
rs.MoveLast
display
Labsit.Caption = "状态:已经是末记录"
End Sub
Private Sub cmdexit_Click()
Unload Me
End Sub
Private Sub cmdhead_Click()
rs.MoveFirst
display
Labsit.Caption = "状态:已经是首记录"
End Sub
Private Sub cmdinput_Click()
addrecords.Show
End Sub
Private Sub cmdnext_Click()
If Not rs.EOF Then
rs.MoveNext
If Not rs.EOF Then
display
Else
MsgBox "已到最后一条记录", vbInformation, "提示"
End If
Else
MsgBox "已到最后一条记录", vbInformation, "提示"
End If
End Sub
Private Sub cmdpre_Click()
If Not rs.BOF Then
rs.MovePrevious
If Not rs.BOF Then
display
Else
MsgBox "已到最前一条记录", vbInformation, "提示"
End If
Else
MsgBox "已到最前一条记录", vbInformation, "提示"
End If
End Sub
Private Sub cmdrefresh_Click()
cmdrefresh.Enabled = False
SQL = "select * from 学生信息表 order by ID"
rs.Close
rs.Open SQL, conn, 3, 3
display
sum
Labsit.Caption = "状态:正常"
cmdrefresh.Enabled = True
Set DataGrid1.DataSource = rs
End Sub
Private Sub funrefresh()
SQL = "select * from 学生信息表"
rs.Close
rs.Open SQL, conn, 3, 3
Set DataGrid1.DataSource = rs
display
End Sub
Private Sub display()
Textid.Text = rs("ID")
Textname.Text = rs!姓名 & ""
Textsex.Text = rs!性别 & ""
Textxuehao.Text = rs!学号 & ""
Textsf.Text = rs!身份证号 & ""
Textzy.Text = rs!专业 & ""
Textaddr.Text = rs!住址 & ""
Textyb.Text = rs!邮编 & ""
Textht.Text = rs!合同号 & ""
Textbiye.Text = rs!毕业 & ""
Textfirst.Text = rs!第一次划款 & ""
Textfirsttime.Text = rs!第一次划款时间 & ""
Textsecond.Text = rs!第二次划款 & ""
Textsecondtime.Text = rs!第二次划款时间 & ""
Textthird.Text = rs!第三次划款 & ""
Textthirdtime.Text = rs!第三次划款时间 & ""
Textforth.Text = rs!第四次划款 & ""
Textforthtime.Text = rs!第四次划款时间 & ""
temp = Textid.Text
End Sub
Private Sub sum()
Dim total As Double
Dim sum As Double
Dim textfirst1 As Double
Dim textsecond1 As Double
Dim textthird1 As Double
Dim textforth1 As Double
Dim conn_sum As New ADODB.Connection
Dim rs_sum As New ADODB.Recordset
Dim SQL_sum As String
conn_sum.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\db2.mdb;Persist Security Info=False;Jet OLEDB:Database Password=ZXDK"
conn_sum.CursorLocation = adUseClient
conn_sum.Open
SQL_sum = "select * from 学生信息表"
rs_sum.Open SQL_sum, conn, 3, 3
Do While Not rs_sum.EOF
Textfirst = Val(rs_sum!第一次划款 & "")
Textsecond = Val(rs_sum!第二次划款 & "")
Textthird = Val(rs_sum!第三次划款 & "")
Textforth = Val(rs_sum!第四次划款 & "")
textfirst1 = textfirst1 + Val(Textfirst)
textsecond1 = textsecond1 + Val(Textsecond)
textthird1 = textthird1 + Val(Textthird)
textforth1 = textforth1 + Val(Textforth)
total = total + 1
rs_sum.MoveNext
Loop
sum = textfirst1 + textsecond1 + textthird1 + textforth1
Labeltotal.Caption = "数据库共有记录:" & total & "条"
Labelsum.Caption = "总贷款金额:" & sum & "元"
Labfirst.Caption = "第一次:" & textfirst1 & "元"
Labsecond.Caption = "第二次:" & textsecond1 & "元"
Labthird.Caption = "第三次:" & textthird1 & "元"
Labforth.Caption = "第四次:" & textforth1 & "元"
End Sub
把一些无用的代码精简了,版主看看这是怎么回事:
昨天仔细研究发现:每次点击“刷新”后,在display()函数中,“毕业”字段之前的几个字段的值是第一条记录的值,是正确的。而“毕业”字段之后的那几个字段赋给text框的却是最后一条记录的值,而且,最后一条记录中那些字段有的无数值,运行后却发现自动添加上了“0”,并赋给了text。只有手动点击“移动”按钮,把指针向后移动再回到第一条记录,text框中才是显示正确的值(也就是均为第一条记录的值)。
附有截图和数据库,数据库密码为ZXDK
因为涉及隐私,有些值临时删去了,请原谅!