<strong><span style="font-size:48px;"> 前言:</span></strong>
敲机房开始时,一些表的逻辑还不是很清楚,开始时,只是进行一些简单的窗体实现,模仿着学生管理系统来进行,逐渐的对于一些表的逻辑有些自己的头绪了,但还是不是特别清晰。当在敲上下机时,我简单的认为,上机不就是把学生的信息添加到online表中,下机时在表中把数据删除不就罢了嘛,咋很多人还说不简单呢。我开始把自己认为的这些写上,但是哪有这么简单呀,各个表之间的联系真的很紧密,如果只是这样简单的写,后面的一些环节也很难实现,于是我又重新梳理了一遍,画了一遍思维导图,这次感觉条理清晰很多了。
内容:
上图中是我梳理的一些顺序,有个总体的思路,然后可以按照这个逻辑进行代码的编辑喽!红色标记的是稍微难些的,尤其是消费金额与固定用户、临时用户的关系。接下来是核心代码,可以参考一下:
上机:
Dim txtSQL As String Dim MsgText As String Dim mrc As ADODB.Recordset '设定mrc用于表student的查询 Dim mrcc As ADODB.Recordset '设定mrcc用于表online的查询 Dim mrb As ADODB.Recordset '使用mrb进行表basicdata的查询 Dim mr As ADODB.Recordset '使用mr进行表line的查询 '判断卡号不为空 If Trim(txtCardno.Text) = "" Then MsgBox "卡号不能为空,请重新输入!", vbOKOnly, "提示!" txtCardno.SetFocus txtCardno = "" Exit Sub End If '在表student中查找存在的卡号的信息 txtSQL = "select * from student_info where cardno='" & Trim(txtCardno.Text) & "' and status='使用'" Set mrc = ExecuteSQL(txtSQL, MsgText) '设定mrc用于表student的查询 If mrc.EOF Then '如果卡号不存在 MsgBox "该卡号未注册!", vbOKOnly, "提示!" txtCardno.SetFocus txtcarno = "" '文本框中的内容清空 txtCardno.Text = "" txtStudentno.Text = "" txtName.Text = "" txtDepartment.Text = "" txtSex.Text = "" txtOndate.Text = "" txtOntime.Text = "" txtOffdate = "" txtOfftime = "" txtConsumeTime.Text = "" txtConsume.Text = "" txtCash.Text = "" txtType.Text = "" Else '查到该卡号记录,然后用onine_info 这张表来查询卡号是否在上机 txtSQL = "select * from online_info where cardno='" & txtCardno.Text & "'" Set mrcc = ExecuteSQL(txtSQL, MsgText) '设定mrcc用于表online的查询 If mrcc.EOF = False Then MsgBox "该卡号正在上机!", vbOKOnly, "提示!" txtCardno.SetFocus Else '查询卡号信息将信息返回到主界面 txtSQL = "select * from student_info where cardno= '" & txtCardno.Text & "'" Set mrc = ExecuteSQL(txtSQL, MsgText) txtType.Text = mrc.Fields(14) txtStudentno.Text = mrc.Fields(1) txtName.Text = mrc.Fields(2) txtDepartment.Text = mrc.Fields(4) txtSex.Text = mrc.Fields(3) txtOndate.Text = Date txtOntime.Text = Time txtCash.Text = mrc.Fields(7) txtOffdate.Text = "上机中" txtOfftime.Text = "上机中" txtConsumeTime.Text = "上机中" txtConsume.Text = "上机中" '向表格online充值一条记录 txtSQL = "select * from online_info" Set mrcc = ExecuteSQL(txtSQL, MsgText) mrcc.AddNew mrcc.Fields(0) = txtCardno.Text mrcc.Fields(1) = txtType.Text mrcc.Fields(2) = txtStudentno.Text mrcc.Fields(3) = txtName.Text mrcc.Fields(4) = txtDepartment.Text mrcc.Fields(5) = txtSex.Text mrcc.Fields(6) = txtOndate.Text mrcc.Fields(7) = txtOntime.Text mrcc.Fields(8) = VBA.Environ("computername") mrcc.Fields(9) = Now mrcc.Update '向line表中充值一条记录 txtSQL = "select*from line_info" Set mr = ExecuteSQL(txtSQL, MsgText) mr.AddNew mr.Fields(1) = txtCardno.Text mr.Fields(2) = txtStudentno.Text mr.Fields(3) = txtName.Text mr.Fields(4) = txtDepartment.Text mr.Fields(5) = txtSex.Text mr.Fields(6) = txtOndate.Text mr.Fields(7) = txtOntime.Text mr.Fields(12) = mrc.Fields(7) mr.Fields(13) = "正常上机" mr.Fields(14) = VBA.Environ("computername") mr.Update '再次查询上机人数并跟新到主界面 Label10.Caption = "当前上机人数为:" & mrcc.RecordCount & " 人" txtSQL = "select * from basicdata_info" Set mrb = ExecuteSQL(txtSQL, MsgText) '使用mrb进行表basicdata的查询 If Trim(mrc.Fields(14)) = "临时用户" Then If mrc.Fields(7) < mrb.Fields(1) Then MsgBox "余额不足,请充值后上机!", vbOKOnly, "提示!" Else mrcc.Close MsgBox "上机成功!", vbOKOnly, "提示!" End If Else '为固定用户 '假如 student表中的余额小于basicdate中的设定金额 If mrc.Fields(7) < mrb.Fields(0) Then MsgBox "余额不足,请充值!", vbOKOnly, "提示!" End If End If End If End If
下机:
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 intLineTime As Integer '用于存储实际在线时间 Dim intconsume As Single Dim curConsume As Single '用户存储真正花费钱的时间 Dim curBalance As Integer '用于存储用户的余额 Dim fixedunit As Single '用于存储固定用户的单位金额 Dim temunit As Single '用于存储临时用户的单位金额 Dim a As Integer Dim B As Integer txtStudentno.Text = "" txtName.Text = "" txtSex.Text = "" txtDepartment = "" txtType.Text = "" txtOndate.Text = "" txtOntime.Text = "" txtOffdate.Text = "" txtOfftime.Text = "" txtCash.Text = "" txtConsumeTime.Text = "" txtConsume.Text = "" '判断卡号是否为空 If Trim(txtCardno.Text) = "" Then MsgBox "请输入卡号!", vbOKOnly + vbExclamation, "提示" txtCardno.SetFocus Exit Sub Else '查询数据库中学生的基本信息表 txtSQL = "select * from student_info where cardno= '" & Trim(txtCardno.Text) & "'" Set mrc = ExecuteSQL(txtSQL, MsgText) '判断该卡号是否注册 If mrc.EOF Then MsgBox "该卡号未注册,请先注册信息!", vbOKOnly + vbExclamation, "提示" txtCardno.Text = "" txtCardno.SetFocus Exit Sub Else '判断该卡号是否可以使用(是否已被退卡) If mrc.Fields(10) = "不使用" Then MsgBox "该卡已经退卡,不能进行下机处理!", vbOKOnly + vbExclamation, "提示" txtCardno.Text = "" txtCardno.SetFocus Exit Sub Else '判断卡号是否正在上机 txtSQL = "select * from online_info where cardno= '" & Trim(txtCardno.Text) & "'" Set mrc1 = ExecuteSQL(txtSQL, MsgText) If mrc1.EOF And mrc1.BOF = True Then MsgBox "该卡没有上机,不能进行下机处理!", vbOKOnly + vbExclamation, "提示" txtCardno.Text = "" txtCardno.SetFocus Exit Sub End If End If End If End If '查询基本数据表,获得设定的基本数据 txtSQL = "select * from BasicData_info" Set mrc2 = ExecuteSQL(txtSQL, MsgText) '计算消费时间 '实际在线时间 intLineTime = (Date - DateValue(mrc1!onDate)) * 1440 + (Hour(Time) - Hour(TimeValue(mrc1!OnTime))) * 60 + (Minute(Time) - Minute(TimeValue(mrc1!OnTime))) '时间单位转换成分钟 '把固定用户、临时用户单位时间的费用分别赋给费用 fixedunit = Val(mrc2.Fields(0)) '把固定用户的金额赋给变量 temunit = Val(mrc2.Fields(1)) '把临时用户的金额赋给变量 '判断实际在线时间是否小于准备时间,若小于则消费为0 If intLineTime <= Val(Trim(mrc2.Fields(4))) Then txtConsume.Text = 0 Else '判断实际在线时间是否小于最低消费时间,如小于消费为0 If intLineTime <= Val(Trim(mrc2.Fields(3))) Then txtConsume.Text = 0 Else '实际在线时间大于最低消费时间则按单位时间算,分固定用户和临时用户 If intLineTime >= Val(Trim(mrc2!leasttime)) And intLineTime < Val(Trim(mrc2!unittime)) And Trim(mrc.Fields(14)) = "固定用户" Then txtConsume.Text = fixedunit Else If intLineTime >= Val(Trim(mrc2!leasttime)) And intLineTime < Val(Trim(mrc2!unittime)) And Trim(mrc.Fields(14)) = "临时用户" Then txtConsume.Text = temunit a = temunit Else '当实际在线时间大于单位时间,就按有几个单位时间算,分固定用户和临时用户 If intLineTime > Val(Trim(mrc2!leasttime)) And Trim(mrc.Fields(14)) = "固定用户" Then curConsume = Int(intLineTime / Val(Trim(mrc2!unittime))) txtConsume.Text = Val(curConsume) * Val(fixedunit) Else If intLineTime > Val(Trim(mrc2!unittime)) And Trim(mrc.Fields(14)) = "临时用户" Then curConsume = Int(intLineTime / Val(Trim(mrc2!unittime))) txtConsume.Text = Val(curConsume) * Val(temunit) End If End If End If End If End If End If '计算余额(账户余额=原账户余额-消费金额) B = mrc!cash curBalance = B - a '下机信息显示 txtOffdate.Text = Date txtOfftime.Text = Time txtType.Text = Trim(mrc1.Fields(1)) txtStudentno.Text = Trim(mrc1.Fields(2)) txtName.Text = Trim(mrc1.Fields(3)) txtDepartment.Text = Trim(mrc1.Fields(4)) txtSex.Text = Trim(mrc1.Fields(5)) txtOndate.Text = Trim(mrc1.Fields(6)) txtOntime.Text = Trim(mrc1.Fields(7)) txtConsumeTime.Text = intLineTime txtCash.Text = curBalance MsgBox "下机成功!欢迎下次再来!", vbOKOnly + vbExclamation, "提示" '更新学生信息表的余额 mrc.Fields(7) = curBalance mrc.Update mrc.Close '更新上机记录表(line_info) txtSQL = "select * from line_info where cardno='" & txtCardno.Text & "' and status='正常上机'" Set mrc3 = ExecuteSQL(txtSQL, MsgText) mrc3.Fields(8) = Trim(txtOffdate.Text) mrc3.Fields(9) = Trim(txtOfftime.Text) mrc3.Fields(10) = Trim(Val(txtConsumeTime.Text)) mrc3.Fields(11) = Trim(Val(txtConsume.Text)) mrc3.Fields(12) = Trim(Val(txtCash.Text)) mrc3.Fields(13) = "正常下机" mrc3.Update '删除在线表的信息 txtSQL = "select * from online_Info where cardno = '" & Trim(txtCardno.Text) & "'" Set mrc1 = ExecuteSQL(txtSQL, MsgText) mrc1.Delete mrc1.Update
感悟:
通过敲这些代码,因为自己的粗心,可是用了不少的时间调试呢,往往是一个小标点、空格等等,让我找很长时间,最后也让自己很是无语呀,所以后面的工程,也让自己谨慎了不少呢,嘿嘿,看来这个也是很好的锻炼细心、耐心的过程呀。通过下机对于金额的计算,也可以很好的增加自己的逻辑思维呢。