看评论,小伙伴们建议画张流程图来直观表示,博客发布的意义之一在于广开言路,察纳雅言,故更新博客,添上流程图一张,赠与新的读者~~~
充值表中添加了UserID这个字段,便于在学生注册的时候就将金额相关信息存入充值表中,减少了计算的复杂性。
'汇总(注意count和sum的区别) If SSTab1.Tab = 4 Then If comboOpUserID.Text = "" Then MsgBox "请选择操作员用户!", vbOKOnly + vbExclamation, 提示 Exit Sub End If '计算售卡张数,少用 count(字段名),因为它会排除NULL值 txtSQL = "select * from student_Info where userID = '" & comboOpUserID.Text & "'and Ischeck = '" & "未结账" & "'" Set mrc = ExecuteSQL(txtSQL, MsgText)' If mrc.RecordCount = 0 Then txtSellCardSum.Text = 0 Else txtSellCardSum.Text = mrc.RecordCount '售卡张数 End If '计算退卡张数 txtSQL = "select * from CancelCard_Info where userID = '" & comboOpUserID.Text & "'and status = '" & "未结账" & "'" Set mrc = ExecuteSQL(txtSQL, MsgText) If mrc.RecordCount = 0 Then txtBackCardSum.Text = 0 Else txtBackCardSum.Text = mrc.RecordCount '退卡张数 End If '计算充值金额(不区分固定用户还有临时用户) txtSQL = "select sum(addmoney) as sum_info from Recharge_Info where userID = '" & comboOpUserID.Text & "'and status = '" & "未结账" & "'" Set mrc = ExecuteSQL(txtSQL, MsgText) If IsNull(Trim(mrc.Fields(0))) Then txtRecharge.Text = 0 Else txtRecharge.Text = mrc.Fields(0) '括号里需要是0,因为临时表sum_info只有一列 End If '计算退卡金额 txtSQL = "select sum(CancelCash) as sum_info from CancelCard_Info where userID = '" & comboOpUserID.Text & "'and status = '" & "未结账" & "'" Set mrc = ExecuteSQL(txtSQL, MsgText) If IsNull(Trim(mrc.Fields(0))) Then '为空 txtBackCardMoney.Text = 0 Else txtBackCardMoney.Text = mrc.Fields(0) End If '计算临时收费金额 txtSQL = "select sum(addmoney) as sum_info from Recharge_Info where userID = '" & comboOpUserID.Text & " 'and type = '临时用户' and status = '未结账'" Set mrc = ExecuteSQL(txtSQL, MsgText) If IsNull(mrc.Fields(0)) Then '无记录 txtTemRecharge.Text = 0 Else txtTemRecharge.Text = mrc.Fields(0) End If '计算总售卡数 txtSellCardActual.Text = Val(txtSellCardSum.Text) - Val(txtBackCardSum.Text) '计算应收金额 txtCollectMoney.Text = Val(txtRecharge.Text) - Val(txtBackCardMoney.Text) End If
sum是对符合条件的记录的数值列求和
count 是对查询中符合条件的结果(或记录)的个数
例如:
表fruit
id name price
1 apple 3.00
2 pear 4.00
select count(price) from fruit; ----执行之后结果为:2 (表示有2条记录)
select sum(price) from fruit;---执行之后结果为:7:00(表示各记录price字段之和为7.00)
结账
'结账分为两部分: '一是将相关信息存入日结账单中 '二是改变相关表中的结账状态,即将"未结账"改为"已结账" Private Sub cmdAccount_Click() Dim txtSQL As String Dim MsgText As String Dim mrcS As ADODB.Recordset Dim mrcR As ADODB.Recordset Dim mrcc As ADODB.Recordset '退卡 Dim mrcday As ADODB.Recordset Dim mrcL As ADODB.Recordset Dim ConsumeCash As Integer '消费金额中间变量 Dim sumcash As Integer Dim mrc As ADODB.Recordset 'sum的临时表即sum_info If comboOpUserID.Text = "" Then MsgBox "请选择操作员后再结账!", vbOKOnly + vbExclamation, 提示 Exit Sub End If '计算上期充值卡余额 txtSQL = "select sum(cash) from student_Info where UserID='" & Trim(comboOpUserID.Text) & "'and ischeck = '" & "未结账" & "'" Set mrcS = ExecuteSQL(txtSQL, MsgText) If IsNull(mrcS.Fields(0)) Then sumcash = 0 Else sumcash = mrcS.Fields(0) End If '从line表计算当日消费金额 txtSQL = "select sum(consume) from Line_Info " Set mrcL = ExecuteSQL(txtSQL, MsgText) If IsNull(mrcL.Fields(0)) Then '为空 ConsumeCash = 0 Else ConsumeCash = mrcL.Fields(0) End If '将相应的记录存入到日结表 txtSQL = "select * from CheckDay_Info where UserID='" & Trim(comboOpUserID.Text) & "'and date=' " & Date & "'" Set mrcday = ExecuteSQL(txtSQL, MsgText) '当天结多次账的情况,就在原有基础上更新 If mrcday.EOF = False Then mrcday!RemainCash = sumcash + mrcday!RemainCash mrcday!RechargeCash = Val(txtRecharge.Text) + mrcday!RechargeCash mrcday!ConsumeCash = ConsumeCash + mrcday!ConsumeCash mrcday!CancelCash = Val(txtBackCardMoney.Text) + mrcday!CancelCash mrcday!AllCash = (sumcash + Val(txtRecharge.Text) - Val(txtBackCardMoney.Text) - ConsumeCash) + mrcday!AllCash mrcday!Date = Date mrcday!UserID = comboOpUserID.Text mrcday.Update mrcday.Close mrcS.Close mrcL.Close Else '如果没结过账就添加新记录 mrcday.AddNew mrcday!RemainCash = sumcash mrcday!RechargeCash = Val(txtRecharge.Text) mrcday!ConsumeCash = ConsumeCash mrcday!CancelCash = Val(txtBackCardMoney.Text) mrcday!AllCash = sumcash + Val(txtRecharge.Text) - Val(txtBackCardMoney.Text) - ConsumeCash mrcday!Date = Date mrcday!UserID = comboOpUserID.Text mrcday.Update mrcday.Close mrcS.Close mrcL.Close End If '------------------------------------------------------------------------------------------------------------------ '更新学生表 txtSQL = "select * from student_Info where UserID='" & Trim(comboOpUserID.Text) & "'and Ischeck = '" & "未结账" & "'" Set mrcS = ExecuteSQL(txtSQL, MsgText) Do While Not mrcS.EOF mrcS!Ischeck = "已结账" mrcS.Update mrcS.MoveNext Loop mrcS.Close '更新充值表 txtSQL = "select * from Recharge_Info where UserID='" & Trim(comboOpUserID.Text) & "'and status = '" & "未结账" & "'" Set mrcR = ExecuteSQL(txtSQL, MsgText) Do While Not mrcR.EOF mrcR!Status = "已结账" mrcR.Update mrcR.MoveNext Loop mrcR.Close '更新退卡表 txtSQL = "select * from Cancelcard_Info where UserID='" & Trim(comboOpUserID.Text) & "'and status = '" & "未结账" & "'" Set mrcc = ExecuteSQL(txtSQL, MsgText) Do While Not mrcc.EOF mrcc!Status = "已结账" mrcc.Update mrcc.MoveNext Loop mrcc.Close MsgBox "结账成功!", vbOKOnly + vbExclamation, "提示" '------------------------------------------------------------------------------------------------------------------ '清空文本框显示的信息 Dim ctr1 As Control For Each ctr1 In Me.Controls If TypeOf ctr1 Is TextBox Then ctr1.Text = "" End If Next Unload Me End Sub
日结账单添加了UserID这个选项,便于查看是谁结的账
上下机请见:机房收费系统之纵观全局