做下机的时候刚开始是一点思路都没有的,只是知道一个大概的框架,但是里面的细节比如说更新哪个表、删除哪个表,什么时候删除等,这些还是不是太明白。但是把想法体现在流程图上就会清晰很多。
在这个窗体中涉及到的表有basicdata(进行单价对比)、lin表、online表、student表。
代码展示
在下机的过程中,首先判断卡号是否为空,是否已经上机
If txtCardNum.Text = "" Then '判断卡号是否为空
MsgBox "请输入卡号上机", vbOKOnly + vbExclamation, "提示"
Exit Sub
End If
txtSQL = "select * from OnLine_info where cardno='" & txtCardNum.Text & "'"'连接上机表,判断段是否已经上机
Set OnLine = ExecuteSQL(txtSQL, Msgtext)
txtSQL = "select * from BasicData_info "
Set bas = ExecuteSQL(txtSQL, Msgtext)
txtSQL = "select * from student_info "
Set stu = ExecuteSQL(txtSQL, Msgtext)
If OnLine.EOF = True Then '?判断是否上机
MsgBox "此卡号未上机", vbOKOnly + vbExclamation, "提示"
如果以上条件都满足的话,就开始正常下机了,在这个还要判断消费时间是否2分钟以上,在这里需要注意的是,比较时间的时候一定要直接连接数据库,不要直接写数字,如果数据库的值变了,相应的消费时间也会改变
a = MsgBox("确认要下机?", vbOKOnly + vbExclamation, "提示")
If a = vbOK Then
consumtime = DateDiff("n", Trim(OnLine!Date), Now) '计算消费时间
txttime.Text = Val(consumtime)’消费时间的值赋给消费时间文本框
If consumtime <= Val(bas!PrepareTime) Then '?判断有没有达到最低上机时间
consum = 0
consumptionAmount.Text = Val(consum)
MsgBox "没有达到上机准备时间,不收取费用", vbOKOnly + vbExclamation, "提示"
更新数据库(online表、line 表)
txtSQL = "select * from Line_Info "
Set line = ExecuteSQL(txtSQL, Msgtext)
line.AddNew
line.Fields(1) = Trim(txtCardNum.Text)
line.Fields(2) = Trim(stu.Fields(1))
line.Fields(3) = Trim(stu.Fields(2))
line.Fields(4) = Trim(stu.Fields(4))
line.Fields(5) = Trim(stu.Fields(3))
line.Fields(6) = Trim(OnLine.Fields(6))
line.Fields(7) = Trim(OnLine.Fields(7))
line.Fields(8) = Format(Now(), "yyyy-MM-dd")
line.Fields(9) = Format(Now(), "HH:mm:ss")
line.Fields(10) = txttime.Text
line.Fields(11) = consume
line.Fields(12) = txtCash
line.Fields(13) = "正常下机"
line.Fields(14) = Environ("computername")
line.Update
txtSQL = "delete from Online_Info where cardno='" & txtCardNum.Text & "'"
Set OnLine = ExecuteSQL(txtSQL, Msgtext)
txtSQL = "select * from OnLine_info"
Set OnLine = ExecuteSQL(txtSQL, Msgtext)
OnlinePerson.Caption = OnLine.RecordCount '显示上机人数
OffDate.Text = Date
OffTime.Text = Time
MsgBox "下机完成", vbOKOnly + vbExclamation, "提示"
以上所有代码是上机未达到最低消费时间的,那么达到消费时间后需要判断是固定用户还是临时用户,在这里首先还是判断消费时间
If Val(consumtime) Mod Val(bas!unitTime) = 0 Then'如果消费时间为整小时数
t = Val(consumtime) / Val(bas!unitTime) '计算消费时间(单位为小时)
Else
t = Val(consumtime) / Val(bas!unitTime) + 1'如果不满一小时按一小时算
End If
判断是固定用户还是临时用户
txtSQL = "select * from student_info"
Set stu = ExecuteSQL(txtSQL, Msgtext)
If Trim(stu!Type) = Trim("固定用户") Then
consume = t * bas.Fields(0) '计算消费金额
consumptionAmount.Text = Val(consume)
Else
consume = t * bas.Fields(1)
consumptionAmount.Text = Val(consume)
End If
txtCash = Val(stu!cash) - consume '计算剩余金额
txtBalance.Text = Val(txtCash)
这些完成之后就更新数据库就好了,只不过这里需要多更新一个学生的表,其他的(line 表、online表跟上面更新的数据一样)
txtSQL = "update student_Info set cash=" & txtCash & " where cardno='" & txtCardNum.Text & "'"
Set stu1 = ExecuteSQL(txtSQL, Msgtext)
‘把余额更新到学生表中
全部代码展示
Private Sub OffLine_Click() '下机
Dim OnLine As ADODB.Recordset '
Dim stu As ADODB.Recordset
Dim line As ADODB.Recordset
Dim bas As ADODB.Recordset
Dim consum As String
Dim consumtime As String
Dim txtSQL As String
Dim Msgtext As String
Dim txtCash As Integer
Dim stu1 As ADODB.Recordset
If txtCardNum.Text = "" Then '判断卡号是否为空
MsgBox "请输入卡号上机", vbOKOnly + vbExclamation, "提示"
Exit Sub
End If
txtSQL = "select * from OnLine_info where cardno='" & txtCardNum.Text & "'"
Set OnLine = ExecuteSQL(txtSQL, Msgtext)
txtSQL = "select * from BasicData_info "
Set bas = ExecuteSQL(txtSQL, Msgtext)
txtSQL = "select * from student_info "
Set stu = ExecuteSQL(txtSQL, Msgtext)
If OnLine.EOF = True Then '判断是否上机
MsgBox "此卡号未上机", vbOKOnly + vbExclamation, "提示"
Else
a = MsgBox("确认要下机?", vbOKOnly + vbExclamation, "提示")
If a = vbOK Then
consumtime = DateDiff("n", Trim(OnLine!Date), Now) '计算消费时间
txttime.Text = Val(consumtime)
If consumtime <= Val(bas!PrepareTime) Then '判断有没有达到上机时间
consum = 0
consumptionAmount.Text = Val(consum)
MsgBox "没有达到上机准备时间,不收取费用", vbOKOnly + vbExclamation, "提示"
'更新数据库
txtSQL = "select * from Line_Info "
Set line = ExecuteSQL(txtSQL, Msgtext)
line.AddNew
line.Fields(1) = Trim(txtCardNum.Text)
line.Fields(2) = Trim(stu.Fields(1))
line.Fields(3) = Trim(stu.Fields(2))
line.Fields(4) = Trim(stu.Fields(4))
line.Fields(5) = Trim(stu.Fields(3))
line.Fields(6) = Trim(OnLine.Fields(6))
line.Fields(7) = Trim(OnLine.Fields(7))
line.Fields(8) = Format(Now(), "yyyy-MM-dd")
line.Fields(9) = Format(Now(), "HH:mm:ss")
line.Fields(10) = txttime.Text
line.Fields(11) = consume
line.Fields(12) = txtCash
line.Fields(13) = "正常下机"
line.Fields(14) = Environ("computername")
line.Update
txtSQL = "delete from Online_Info where cardno='" & txtCardNum.Text & "'"
Set OnLine = ExecuteSQL(txtSQL, Msgtext)
txtSQL = "select * from OnLine_info"
Set OnLine = ExecuteSQL(txtSQL, Msgtext)
OnlinePerson.Caption = OnLine.RecordCount '显示上机人数
OffDate.Text = Date
OffTime.Text = Time
MsgBox "下机完成", vbOKOnly + vbExclamation, "???"
Else
If Val(consumtime) Mod Val(bas!unitTime) = 0 Then
t = Val(consumtime) / Val(bas!unitTime) '计算消费时间(单位为小时)
Else
t = Val(consumtime) / Val(bas!unitTime) + 1
End If
'更新数据库
txtSQL = "select * from student_info"
Set stu = ExecuteSQL(txtSQL, Msgtext)
If Trim(stu!Type) = Trim("固定用户") Then
consume = t * bas.Fields(0) '计算消费金额
consumptionAmount.Text = Val(consume)
Else
consume = t * bas.Fields(1)
consumptionAmount.Text = Val(consume)
End If
txtCash = Val(stu!cash) - consume '余额
txtBalance.Text = Val(txtCash)
'更新数据库
txtSQL = "update student_Info set cash=" & txtCash & " where cardno='" & txtCardNum.Text & "'"
Set stu1 = ExecuteSQL(txtSQL, Msgtext)
txtSQL = "select * from Line_Info "
Set line = ExecuteSQL(txtSQL, Msgtext)
line.AddNew
line.Fields(1) = Trim(txtCardNum.Text)
line.Fields(2) = Trim(stu.Fields(1))
line.Fields(3) = Trim(stu.Fields(2))
line.Fields(4) = Trim(stu.Fields(4))
line.Fields(5) = Trim(stu.Fields(3))
line.Fields(6) = Trim(OnLine.Fields(6))
line.Fields(7) = Trim(OnLine.Fields(7))
line.Fields(8) = Format(Now(), "yyyy-MM-dd")
line.Fields(9) = Format(Now(), "HH:mm:ss")
line.Fields(10) = txttime.Text
line.Fields(11) = consume
line.Fields(12) = txtCash
line.Fields(13) = "正常下机"
line.Fields(14) = Environ("computername")
line.Update
txtSQL = "delete from Online_Info where cardno='" & txtCardNum.Text & "'"
Set line = ExecuteSQL(txtSQL, Msgtext)
txtSQL = "select * from OnLine_info"
Set OnLine = ExecuteSQL(txtSQL, Msgtext)
OnlinePerson.Caption = OnLine.RecordCount '显示上机人数
OffDate.Text = Date
OffTime.Text = Time
MsgBox "下机完成", vbOKOnly + vbExclamation, "提示"
stu.Close
OnLine.Close
End If
End If
End If
End Sub
自我觉得代码有些冗余,如果有更好的写法,请大神们指点一二!!
标签:Trim,之下,Fields,txtSQL,OnLine,机房,Text,收费,line From: https://blog.51cto.com/u_15902978/5965185