The first time the computer room charging system is offline

When I got off the computer, I didn't have any idea at the beginning, I just knew a general framework, but the details such as which table to update, which table to delete, when to delete, etc., are still not very clear. But it will be much clearer if the idea is reflected in the flowchart.

The tables involved in this form are basicdata (for unit price comparison), lin table, online table, and student table.

Code display

In the process of getting off the machine, first judge whether the card number is empty and whether it has been on the machine

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, "提示"
        

If all the above conditions are met, the machine will start to shut down normally. In this case, it is necessary to judge whether the consumption time is more than 2 minutes. It should be noted here that when comparing the time, you must directly connect to the database. Do not write numbers directly. The value of changes, the corresponding consumption time will also change

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, "提示"
                

Update the database (online table, line table)

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, "提示"

All of the above codes are not reached the minimum consumption time on the machine, then after the consumption time is reached, it is necessary to determine whether it is a fixed user or a temporary user. Here, first determine the consumption time

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

Determine whether it is a fixed user or a temporary user

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)

Just update the database after these are completed, but here you need to update one more student table, and the others (line table and online table are the same as the data updated above)

txtSQL = "update student_Info set cash=" & txtCash & " where cardno='" & txtCardNum.Text & "'"
                Set stu1 = ExecuteSQL(txtSQL, Msgtext)
                ‘把余额更新到学生表中

All code display

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

I feel that the code is a bit redundant. If there is a better way of writing, please give me some advice! !

Guess you like

Origin blog.csdn.net/weixin_45309155/article/details/106326694