以下代码运行中,发现一个问题,并不是每个建筑材料每天都有销量,比如说沙、石子,新增行时如果填了日销量,月累计、年累计可计算出来,如果没填,就出不来,帮忙看一下,怎么样在没填的时候也计算月累计、年累计,代码是放在DataColChanged属性里的,另外,这段代码可否更精简一点?
Dim cnt1 As Double '定义cnt(...)为双精度小数类型
Dim cnt2 As Double
Dim cnt3 As Double
Dim cnt4 As Double
Dim cnt5 As Double
Dim cnt6 As Double
Dim cnt7 As Double
Dim cnt8 As Double
Dim cnt9 As Double
Dim cnt1"0" As Double
Dim cnt11 As Double
Dim cnt12 As Double
Dim y As Integer = Date.Today.Year '取Y为今年
Dim m As Integer = Date.Today.Month '取M为当月
Dim d As Integer = Date.Today.Day '取D为当日
Dim dt1 As New Date(y, m, 1) '获取本月第一天
Dim dt2 As New Date(y, m, d) '获取本月第几天
Dim dt3 As New Date(y, 1, 1) '取本年度第一天,如2"0"19年1月1日,此为变量,根据年份自动形成.
Select Case e.DataCol.Name
Case "本期沙日销量"
If e.DataRow.IsNull("本期沙日销量") Then
e.DataRow("本期沙日销量") = "0"
Else
cnt1 = DataTables("日报").SQLCompute("sum(本期沙日销量)", "报表日期 >= '"& dt1 & "' And 报表日期<='" & dt2 & "' ")
cnt2 = DataTables("日报").SQLCompute("sum(本期沙日销量)", "报表日期 >= '" & dt3 & "' And 报表日期<='" & dt2 & "' ")
e.DataRow("本期沙月累计") = cnt1+ e.DataRow("本期沙日销量")
e.DataRow("本期沙年累计") = cnt2+ e.DataRow("本期沙日销量")
End If
Case "本期石子日销量"
If e.DataRow.IsNull("本期石子日销量") Then
e.DataRow("本期石子日销量") = "0"
Else
cnt3 = DataTables("日报").SQLCompute("sum(本期石子日销量)", "报表日期 >= '" & dt1 & "' And 报表日期<='" & dt2 & "'")
cnt4 = DataTables("日报").SQLCompute("sum(本期石子日销量)", "报表日期 >= '" & dt3 & "' And 报表日期<='" & dt2 & "' ")
e.DataRow("本期石子月累计") = cnt3+ e.DataRow("本期石子日销量")
e.DataRow("本期石子年累计") = cnt4+ e.DataRow("本期石子日销量")
End If
Case "本期鹅卵石日销量"
If e.DataRow.IsNull("本期鹅卵石日销量") Then
e.DataRow("本期鹅卵石日销量") = "0"
Else
cnt5 = DataTables("日报").SQLCompute("sum(本期鹅卵石日销量)", "报表日期 >= '" & dt1 & "' And 报表日期<='" & dt2 & "'")
cnt6 = DataTables("日报").SQLCompute("sum(本期鹅卵石日销量)", "报表日期 >= '" & dt3 & "' And 报表日期<='" & dt2 & "' ")
e.DataRow("本期鹅卵石月累计") = cnt5+ e.DataRow("本期鹅卵石日销量")
e.DataRow("本期鹅卵石年累计") = cnt6+ e.DataRow("本期鹅卵石日销量")
End If
Case "同期沙日销量"
If e.DataRow.IsNull("同期沙日销量") Then
e.DataRow("同期沙日销量") = "0"
Else
cnt7 = DataTables("日报").SQLCompute("sum(同期沙日销量)", "报表日期 >= '" & dt1 & "' And 报表日期<='" & dt2 & "'")
cnt8 = DataTables("日报").SQLCompute("sum(同期沙日销量)", "报表日期 >= '" & dt3 & "' And 报表日期<='" & dt2 & "' ")
e.DataRow("同期沙月累计") = cnt7+ e.DataRow("同期沙日销量")
e.DataRow("同期沙年累计") = cnt8+ e.DataRow("同期沙日销量")
End If
Case "同期石子日销量"
If e.DataRow.IsNull("同期石子日销量") Then
e.DataRow("同期石子日销量") = "0"
Else
cnt9 = DataTables("日报").SQLCompute("sum(同期石子日销量)", "报表日期 >= '" & dt1 & "' And 报表日期<='" & dt2 & "'")
cnt1"0" = DataTables("日报").SQLCompute("sum(同期石子日销量)", "报表日期 >= '" & dt3 & "' And 报表日期<='" & dt2 & "' ")
e.DataRow("同期石子月累计") = cnt9+ e.DataRow("同期石子日销量")
e.DataRow("同期石子年累计") = cnt1"0"+ e.DataRow("同期石子日销量")
End If
Case "同期鹅卵石日销量"
If e.DataRow.IsNull("同期鹅卵石日销量") Then
e.DataRow("同期鹅卵石日销量") = "0"
Else
cnt11 = DataTables("日报").SQLCompute("sum(同期鹅卵石日销量)", "报表日期 >= '" & dt1 & "' And 报表日期<='" & dt2 & "'")
cnt12 = DataTables("日报").SQLCompute("sum(同期鹅卵石日销量)", "报表日期 >= '" & dt3 & "' And 报表日期<='" & dt2 & "' ")
e.DataRow("同期鹅卵石月累计") = cnt11+ e.DataRow("同期鹅卵石日销量")
e.DataRow("同期鹅卵石年累计") = cnt12+ e.DataRow("同期鹅卵石日销量")
End If
End Select