两个表:入库 耗用
入库表 入库数量 入库金额
耗用表 耗用数量
统计表结构 单价=(期初金额+入库金额)/(期初数量+入库数量)
期初_数量 期初_金额
入库_数量 入库_金额
耗用_数量
库存_数量 库存_金额
不会写“单价”的表达式,Fiter来Fiter去,晕菜了,帮帮忙。
With e.Form.Controls("日期")
If .Value Is Nothing Then
Return
End If
End With
Tables("统计_Table1").StopRedraw
Dim Filter As String
Dim curD As Date=e.Form.Controls("日期").Value '当前日期
Dim lastD As Date = curD.AddMonths(-1) '上一个月的日期
Dim lastD2 As New Date(lastD.Year, lastD.Month, Date.DaysInMonth(lastD.Year,lastD.Month)) '上个月的最后一天
Filter = Filter & "日期 > #" & lastD2 & "#" & " And 日期 <= #" & curD & "#"
Dim bd1 As New SQLGroupTableBuilder("统计表1","进货")
Dim dt1 As fxDataSource
bd1.Groups.AddDef("型号") '根据型号分组
bd1.Totals.AddDef("进货_数量","进货_数量") '对数量进行统计
bd1.Totals.AddDef("进货_金额","进货_金额") '对金额进行统计
bd1.Filter = Filter
dt1 = bd1.BuildDataSource()
Dim bd2 As New SQLGroupTableBuilder("统计表2","销售")
Dim dt2 As fxDataSource
bd2.Groups.AddDef("型号") '根据型号分组
bd2.Totals.AddDef("销售_数量","销售_数量") '对数量进行统计
bd2.Filter = Filter
dt2 = bd2.BuildDataSource()
dt1.Combine("型号",dt2,"型号") '将销售统计数据组合到进货统计数据
Tables("统计_Table1").DataSource = dt1 '将统计结果绑定到Table
With DataTables("统计_Table1").DataCols '用表达式列计算单价数据
.Add("单价")
End With
?????????????????????
With DataTables("统计_Table1").DataCols '用表达式列期初数据
.Add("期初_数量",Gettype(Double),"","期初_数量")
.Add("期初_金额",Gettype(Double),"","期初_金额")
End With
Filter = "日期 <= #" & lastD2 & "#"
Dim dt_jh As DataTable
Dim dt_xs As DataTable
Dim cmd As new SQLCommand
cmd.CommandText = "select * from {进货} where " & Filter
dt_jh = cmd.ExecuteReader
cmd.CommandText = "select * from {销售} where " & Filter
dt_xs = cmd.ExecuteReader
For Each r As Row In Tables("统计_Table1").Rows
Filter = "型号 = '" & r("型号") & "'"
r("期初_数量") = dt_jh.Compute("sum(进货_数量)", filter) - dt_xs.Compute("sum(销售_数量)", filter)
r("期初_金额") = dt_jh.Compute("sum(进货_金额)", filter)
Next
With DataTables("统计_Table1").DataCols '用表达式列计算库存数据
.Add("库存_数量",Gettype(Double), "IsNull([期初_数量],0) + IsNull([进货_数量],0) - ISNULL([销售_数量],0)
.Add("库存_金额",Gettype(Double), "[期初_金额] * [单价]","库存_金额")
End With
Tables("统计_Table1").SetColVisibleWidth("型号|80|单价|80|期初_数量|80|期初_金额|80|进货_数量|80|进货_金额|80|销售_数量|80|库存_数量|80|库存_金额|80")
Tables("统计_Table1").DefaultRowHeight = 35
DataTables("统计_Table1").DataCols("单价").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("期初_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("期初_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("进货_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("进货_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("销售_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("库存_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("库存_金额").SetFormat("#,###.00")
Tables("统计_Table1").ResumeRedraw
[此贴子已经被作者于2014-7-12 15:23:32编辑过]