前言
下机在机房中是比较难的,也不能说难,在搞懂逻辑之前只能说迷糊,搞懂以后你绝对就不说难了。其中也遇到了很多问题,比如时间格式的转化,这么多表到底该删除哪个,更新哪个。不过已经被我一一的解决了,流程图真的是个好工具!其中,对于上机表的删除和下机表的更新可以使用触发器实现,在优化中在继续研究!
代码片段
Private Sub cmdOffline_Click()
Dim txtSQL As String
Dim MsgText As String
Dim mrc As ADODB.Recordset
Dim mrc1 As ADODB.Recordset
Dim mrc2 As ADODB.Recordset
Dim mrc3 As ADODB.Recordset
Dim mrc4 As ADODB.Recordset
Dim StartDate As Date
Dim EndDate As Date
Dim StartTime As Date
Dim Endtime As Date
Dim CostDate As Long
Dim CostTime As Long
Dim a As Integer
'mrc连接online表
txtSQL = "select * from online_info where cardno='" & Trim(txtCardNo.Text) & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)
'mrc1连接line表
txtSQL = "select * from line_info where cardno='" & Trim(txtCardNo.Text) & "'"
Set mrc1 = ExecuteSQL(txtSQL, MsgText)
'mrc2连接student表
txtSQL = "select * from student_info where cardno='" & Trim(txtCardNo.Text) & "'"
Set mrc2 = ExecuteSQL(txtSQL, MsgText)
'mrc3连接basicdata表
txtSQL = "select * from basicdata_info"
Set mrc3 = ExecuteSQL(txtSQL, MsgText)
'mrc4连接cancelcard表
txtSQL = "select * from cancelcard_info where cardno='" & Trim(txtCardNo.Text) & "'"
Set mrc4 = ExecuteSQL(txtSQL, MsgText)
If Trim(txtCardNo.Text) = "" Then
MsgBox "请输入卡号!", 48, "警告"
txtCardNo.SetFocus
Exit Sub
End If
If IsNumeric(txtCardNo.Text) = False Then
MsgBox "卡号请输入数字!", 48, "警告"
txtCardNo.SetFocus
txtCardNo.Text = ""
Exit Sub
End If
If mrc2.Fields(0) <> Trim(txtCardNo.Text) Then
MsgBox "该卡号未注册!", 48, "警告"
Exit Sub
End If
If mrc.Fields(0) = Trim(txtCardNo.Text) Then
MsgBox "学生没有上机,无法下机!", 48, "警告"
Exit Sub
End If
'计算消费时间
labOffDate.Caption = Date
labOffTime.Caption = Time
CostDate = DateDiff("n", labOnDate.Caption, labOffDate.Caption)
CostTime = DateDiff("n", labOnTime.Caption, labOffTime.Caption)
labCTime.Caption = (Val(CostDate) + Val(CostTime))
'计算消费金额
If Trim(labCTime.Caption) <= Val(mrc3.Fields(4)) Then
labCMoney.Caption = 0
Else
If Trim(labType.Caption) = "固定用户" Then
labCMoney.Caption = Format(Val(labCTime.Caption) / mrc3.Fields(2) * mrc3.Fields(0), "0.00")
Else
labCMoney.Caption = Format(Val(labCTime.Caption) / mrc3.Fields(2) * mrc3.Fields(1), "0.00")
End If
End If
'计算余额
labBalance.Caption = Val(mrc2.Fields(7)) - Val(Trim(labCMoney.Caption))
'删除online_info表上信息
mrc.Delete
mrc.Update
mrc.Close
'更新line_info表
With mrc1
.Fields(8) = Trim(labOffDate.Caption)
.Fields(9) = Trim(labOffTime.Caption)
.Fields(10) = Trim(labCTime.Caption)
.Fields(11) = Trim(labCMoney.Caption)
.Fields(12) = Trim(labBalance.Caption)
.Fields(13) = "正常下机"
.Fields(14) = VBA.Environ("computername")
.Update
.Close
End With
'更新学生表
With mrc2
.Fields(7) = labBalance.Caption
.Update
.Close
End With
结语
要是我做机房之前让我看到这一段代码绝对就被吓死了,现在可以靠自己写代码实现这些功能。不要自己吓自己,没有解决不了的事情,只有逃避问题的自己,遇到困难积极面对解决就完了!机房就是i+1的学习材料,我们之前有了学生的铺垫,再做机房虽然有一些难度,但是都是可以克服的难度,继续加油!