后台统计出错:
窗口中有一内部表("材料月结"),一个按钮. 通过三个外部表取数,外部表名称为:1,DataTables("材料入库明细表").2,DataTables("材料出库明细表").3,外部表DataTables("材料期初库存明细表"). 由于三个外部表设置了初始不加载任何数据,点击此查询按钮时出错.如果将外部表加载数据,按钮功能正常,麻烦老师指点一下
按钮代码如下:
Dim s As String = e.Form.Controls("查询月份").Value
Dim fil As String = "月份 <= " & s
DataTables("材料入库明细表").LoadFilter = fil
DataTables("材料入库明细表").Load
DataTables("材料出库明细表").LoadFilter = fil
DataTables("材料出库明细表").Load
DataTables("材料期初库存明细表").LoadFilter = fil
DataTables("材料期初库存明细表").Load
If s <> "" Then
Forms("材料月结").StopRedraw()
Tables("材料月结").StopRedraw() '停止重绘
DataTables("材料月结").DataRows.Clear
Dim drs_out As List(Of DataRow) = DataTables("材料出库明细表").Select("材料编号 is not null", "材料编号,材料名称,规格型号,材料_大类,材料_中类,月份") '排序取出 出仓 全部数据
Dim dt_in As List(Of DataRow) = DataTables("材料入库明细表").Select("材料编号 is not null", "材料编号,材料名称,规格型号,材料_大类,材料_中类,月份") '排序取出 入仓 全部数据
Dim drs_init As List(Of DataRow) = DataTables("材料期初库存明细表").Select("月份 <= " & s)
Dim r As Row
Dim sum_curr_money, sum_prev_money As Double
Dim sum_curr_count, sum_prev_count As Double
Dim sum_curr_money3, sum_prev_money3 As Double
Dim sum_curr_count2, sum_prev_count2 As Double
r = Tables("材料月结").AddNew
r("材料编号") = dt_in(0)("材料编号")
r("材料名称") = dt_in(0)("材料名称")
r("规格型号") = dt_in(0)("规格型号")
r("材料_大类") = dt_in(0)("材料_大类")
r("材料_中类") = dt_in(0)("材料_中类")
If dt_in(0)("月份") < s Then '之前月
sum_prev_count = sum_prev_count + dt_in(0)("数量")
sum_prev_count2 = sum_prev_count2 + dt_in(0)("辅助数量米")
sum_prev_money3 = sum_prev_money3 + dt_in(0)("含税金额")
sum_prev_money = sum_prev_money + dt_in(0)("不含税金额")
Else If dt_in(0)("月份") = s Then '本月份
sum_curr_count = sum_curr_count + dt_in(0)("数量")
sum_curr_count2 = sum_curr_count2 + dt_in(0)("辅助数量米")
sum_curr_money3 = sum_curr_money3 + dt_in(0)("含税金额")
sum_curr_money = sum_curr_money + dt_in(0)("不含税金额")
End If
For i As Single = 1 To dt_in.Count - 1 '写入型号和规格,同时统计入仓数量和金额
If dt_in(i)("材料编号") <> dt_in(i-1)("材料编号") OrElse dt_in(i)("材料名称") <> dt_in(i-1)("材料名称") OrElse dt_in(i)("规格型号") <> dt_in(i-1)("规格型号") OrElse dt_in(i)("材料_大类") <> dt_in(i-1)("材料_大类") OrElse dt_in(i)("材料_中类") <> dt_in(i-1)("材料_中类") Then '不同产品
r("本月入库_数量") = sum_curr_count
r("本月入库_数量米") = sum_curr_count2
r("本月入库_不含税金额") = sum_curr_money
r("本月入库_含税金额") = sum_curr_money3
r("月初结存_数量") = sum_prev_count '暂存
r("月初结存_数量米") = sum_prev_count2
r("月初结存_不含税金额") = sum_prev_money '暂存
r("月初结存_含税金额") = sum_prev_money3
r = Tables("材料月结").AddNew '插入新数据
r("材料编号") = dt_in(i)("材料编号")
r("材料名称") = dt_in(i)("材料名称")
r("规格型号") = dt_in(i)("规格型号")
r("材料_大类") = dt_in(i)("材料_大类")
r("材料_中类") = dt_in(i)("材料_中类")
sum_curr_money = 0
sum_curr_count = 0
sum_prev_count = 0
sum_prev_money = 0
sum_curr_money3 = 0
sum_curr_count2 = 0
sum_prev_count2 = 0
sum_prev_money3 = 0
End If
If dt_in(i)("月份") < s Then '之前月
sum_prev_count = sum_prev_count + dt_in(i)("数量")
sum_prev_count2 = sum_prev_count2 + dt_in(i)("辅助数量米")
sum_prev_money = sum_prev_money + dt_in(i)("不含税金额")
sum_prev_money3 = sum_prev_money3 + dt_in(i)("含税金额")
Else If dt_in(i)("月份") = s Then '本月份
sum_curr_count = sum_curr_count + dt_in(i)("数量")
sum_curr_count2 = sum_curr_count2 + dt_in(i)("辅助数量米")
sum_curr_money = sum_curr_money + dt_in(i)("不含税金额")
sum_curr_money3 = sum_curr_money3 + dt_in(i)("含税金额")
End If
Next
For Each dr As DataRow In drs_init
If DataTables("材料月结").find("材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "'") Is Nothing Then
Dim nr = Tables("材料月结").AddNew
nr("材料编号") = dr("材料编号")
nr("材料名称") = dr("材料名称")
nr("规格型号") = dr("规格型号")
nr("材料_大类") = dr("材料_大类")
nr("材料_中类") = dr("材料_中类")
End If
Next
For Each dr As DataRow In drs_out
If DataTables("材料月结").find("材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "'") Is Nothing Then
Dim nr = Tables("材料月结").AddNew
nr("材料编号") = dr("材料编号")
nr("材料名称") = dr("材料名称")
nr("规格型号") = dr("规格型号")
nr("材料_大类") = dr("材料_大类")
nr("材料_中类") = dr("材料_中类")
End If
Next
r("本月入库_数量") = sum_curr_count
r("本月入库_数量米") = sum_curr_count2
r("本月入库_不含税金额") = sum_curr_money
r("本月入库_含税金额") = sum_curr_money3
r("月初结存_数量") = sum_prev_count '暂存
r("月初结存_数量米") = sum_prev_count2
r("月初结存_不含税金额") = sum_prev_money '暂存
r("月初结存_含税金额") = sum_prev_money3
For Each dr As DataRow In DataTables("材料月结").DataRows '填充统计数据
sum_curr_count = 0 '累计本月出仓数量
sum_prev_count = 0 '累计之前出仓数量
sum_curr_money = 0 '累计之前出仓金额
sum_prev_money = 0 '累计之前出仓金额
sum_curr_count2 = 0 '累计本月出仓数量
sum_prev_count2 = 0 '累计之前出仓数量
sum_curr_money3 = 0 '累计之前出仓金额
sum_prev_money3 = 0 '累计之前出仓金额
Dim filter As String = "材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "'"
drs_out = DataTables("材料出库明细表").Select(filter, "月份") '排序取出 出仓 全部数据
'' MessageBox.Show("耗时: " & (Date.Now - st).TotalSeconds & "秒")
'' st = Date.Now '用于计算时间
For Each dr_out As DataRow In drs_out
If dr_out("月份") < s Then
sum_prev_count = sum_prev_count + dr_out("数量")
sum_prev_count2 = sum_prev_count2 + dr_out("辅助数量米")
sum_prev_money = sum_prev_money + dr_out("出库不含税金额")
sum_prev_money3 = sum_prev_money3 + dr_out("出库含税金额")
Else If dr_out("月份") = s Then
sum_curr_count = sum_curr_count + dr_out("数量")
sum_curr_count2 = sum_curr_count2 + dr_out("辅助数量米")
sum_curr_money = sum_curr_money + dr_out("出库不含税金额")
sum_curr_money3 = sum_curr_money3 + dr_out("出库含税金额")
Else
Exit For
End If
Next
Dim qcsl As Single = DataTables("材料期初库存明细表").compute("sum(数量)", "材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "' and 月份 <= " & s)
Dim qcs2 As Single = DataTables("材料期初库存明细表").compute("sum(辅助数量米)", "材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "' and 月份 <= " & s)
Dim qcsl1 As Single = DataTables("材料期初库存明细表").compute("sum(不含税金额)", "材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "' and 月份 <= " & s)
Dim qcsl3 As Single = DataTables("材料期初库存明细表").compute("sum(含税金额)", "材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "' and 月份 <= " & s)
Dim sum_in_prev_count As Double = dr("月初结存_数量") '从表中取出暂存数据
Dim sum_in_prev_count2 As Double = dr("月初结存_数量米")
Dim sum_in_prev_money As Double = dr("月初结存_不含税金额") '从表中取出暂存数据
Dim sum_in_prev_money3 As Double = dr("月初结存_含税金额")
dr("本月出库_数量") = sum_curr_count
dr("本月出库_数量米") = sum_curr_count2
dr("本月出库_不含税金额") = sum_curr_money
dr("本月出库_含税金额") = sum_curr_money3
dr("月初结存_数量") = sum_in_prev_count - sum_prev_count + qcsl
dr("月初结存_数量米") = sum_in_prev_count2 - sum_prev_count2 + qcs2
dr("月初结存_不含税金额") = sum_in_prev_money - sum_prev_money + qcsl1
dr("月初结存_含税金额") = sum_in_prev_money3 - sum_prev_money3 + qcsl3
dr("月末结存_数量") = dr("月初结存_数量") + dr("本月入库_数量") - dr("本月出库_数量")
dr("月末结存_数量米") = dr("月初结存_数量米") + dr("本月入库_数量米") - dr("本月出库_数量米")
dr("月末结存_不含税金额") = dr("月初结存_不含税金额") + dr("本月入库_不含税金额") - dr("本月出库_不含税金额")
dr("月末结存_含税金额") = dr("月初结存_含税金额") + dr("本月入库_含税金额") - dr("本月出库_含税金额")
dr("加权平均单价") = (dr("月初结存_不含税金额") + dr("本月入库_不含税金额"))/(dr("月初结存_数量") + dr("本月入库_数量"))
dr("加权含税单价") = (dr("月初结存_含税金额") + dr("本月入库_含税金额"))/(dr("月初结存_数量") + dr("本月入库_数量"))
Next
Tables("材料月结").ResumeRedraw()
Forms("材料月结").ResumeRedraw()
End If
With Tables("材料月结")
.Cols("本月入库_数量").GrandTotal = True
.Cols("本月入库_数量米").GrandTotal = True
.Cols("本月入库_不含税金额").GrandTotal = True
.Cols("本月入库_含税金额").GrandTotal = True
.Cols("本月出库_数量").GrandTotal = True
.Cols("本月出库_数量米").GrandTotal = True
.Cols("本月出库_不含税金额").GrandTotal = True
.Cols("本月出库_含税金额").GrandTotal = True
.Cols("月初结存_数量").GrandTotal = True
.Cols("月初结存_数量米").GrandTotal = True
.Cols("月初结存_不含税金额").GrandTotal = True
.Cols("月初结存_含税金额").GrandTotal = True
.Cols("月末结存_数量").GrandTotal = True
.Cols("月末结存_数量米").GrandTotal = True
.Cols("月末结存_不含税金额").GrandTotal = True
.Cols("月末结存_含税金额").GrandTotal = True
.GrandTotal = True
End With
此主题相关图片如下:加载出错.bmp