上机是机房收费系统的三大难点之一,但是不要给自己的思想设限,说它很难很难,捋清思路逻辑,用到那几个表分析清楚,敲代码的时候会顺利很多。一定要画流程图,一定要画流程图,一定要画流程图(重要的事情说三遍)。它会是你搂逻辑的一大工具。
部分代码:
If Trim(txtcardno.Text) = "" Then
MsgBox "卡号不能为空,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtcardno.SetFocus
Exit Sub
End If
Rem:判断输入卡号是否为数字
If Not IsNumeric(Trim(txtcardno)) Then
MsgBox "卡号请输入数字!", vbOKOnly + vbExclamation, "警告"
txtcardno.SetFocus
txtcardno.Text = ""
Exit Sub
End If
txtsql = "select * from student_info where cardno='" & Trim(txtcardno.Text) & " '"
Set mrc = executeSQL(txtsql, msgtext) 'mrc用于student表的查询
'判断卡号是否注册
If mrc.EOF Then
MsgBox "卡号没有被注册,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtcardno.SetFocus
Exit Sub
End If
txtsql = "select* from basicdata_info"
Set mrc1 = executeSQL(txtsql, msgtext)
'判断该卡号余额是否足够
If mrc.Fields(7) < mrc1.Fields(5) Then
MsgBox "余额只有" & mrc.Fields(7) & "少于最小金额,请充值!", vbOKOnly + vbExclamation, "警告"
txtcardno.Text = ""
txtcardno.SetFocus
Exit Sub
End If
'判断该卡号是否在上机
txtsql = "select * from online_info where cardno='" & txtcardno.Text & "'"
Set mrc2 = executeSQL(txtsql, msgtext)
If (mrc2.EOF Or mrc2.BOF) = False Then '若不为空
MsgBox "该卡号正在上机!", vbOKOnly + vbExclamation, "警告"
Rem:如果在上机从mrc2调取上机记录显示在text中
txtType = Trim(mrc2.Fields(1))
txTSTUDENTNO = Trim(mrc2.Fields(2))
txtstudentname = Trim(mrc2.Fields(3))
txtdepartment = Trim(mrc2.Fields(4))
txtsex = Trim(mrc2.Fields(6))
txtondate = Trim(Format(mrc2.Fields(6), "yyyy-mm-dd"))
txtontime = Trim(Format(mrc2.Fields(7), "hh:mm:ss"))
txtcash = Trim(mrc.Fields(7))
Else
Rem:如果没有上机,查询student中的记录
txtsql = "select * from online_info where cardno='" & txtcardno & "'"
Set mrc2 = executeSQL(txtsql, msgtext)
Rem:将信息显示在text中
txtType = Trim(mrc.Fields(14))
txTSTUDENTNO = Trim(mrc.Fields(1))
txtstudentname = Trim(mrc.Fields(2))
txtdepartment = Trim(mrc.Fields(4))
txtsex = Trim(mrc.Fields(3))
txtondate = Trim(Format(Date, "yyyy-mm-dd"))
txtontime = Trim(Format(Time, "hh:mm:ss"))
txtcash = Trim(mrc.Fields(7))
Rem:更新online
mrc2.AddNew
mrc2.Fields(0) = Trim(txTSTUDENTNO)
mrc2.Fields(1) = Trim(txtType)
mrc2.Fields(2) = Trim(txTSTUDENTNO)
mrc2.Fields(3) = Trim(txtstudentname)
mrc2.Fields(4) = Trim(txtdepartment)
mrc2.Fields(5) = Trim(txtsex)
mrc2.Fields(6) = (Format(txtondate))
mrc2.Fields(7) = (Format(txtontime))
mrc2.Fields(8) = Trim(VBA.Environ("computername"))
mrc2.Fields(9) = Trim(Now) '当前时间
mrc2.Update
Rem:向line中记录
txtsql = "select * from line_info "
Set mrc3 = executeSQL(txtsql, msgtext)
mrc3.AddNew
mrc3.Fields(1) = Trim(txtcardno)
mrc3.Fields(2) = Trim(txTSTUDENTNO)
mrc3.Fields(3) = Trim(txtstudentname)
mrc3.Fields(4) = Trim(txtdepartment)
mrc3.Fields(5) = Trim(txtsex)
mrc3.Fields(6) = Trim(txtondate)
mrc3.Fields(7) = Trim(txtontime)
mrc3.Fields(12) = mrc.Fields(7)
mrc3.Fields(13) = "正常上机"
mrc3.Fields(14) = VBA.Environ("computername")
mrc3.Update
mrc3.Close
Rem:显示上机成功
MsgBox "上机成功!", vbOKOnly, "提示"
显示上机人数:
Dim mrc0 As ADODB.Recordset
txtsql = "select * from online_info "
Set mrc0 = executeSQL(txtsql, msgtext)
Label2.Caption = "当前上机人数:" & mrc0.RecordCount
txtcardno.SetFocus
End If