主要代码:
Private Sub 月结库存_Click() Dim sql As String DoCmd.SetWarnings no If Me.计价选项 > 0 Then Select Case Me.计价选项
Case Is = 1 '加权平均计价法 sql = "SELECT 发料查询.记录号, 发料查询.物资ID, 加权平均查询.发料单价, 加权平均查询.发料数量, 加权平均查询.发料金额 INTO 临时表 " sql = sql + "FROM 发料查询 LEFT JOIN 加权平均查询 ON (发料查询.记录号 = 加权平均查询.记录号) AND (发料查询.物资ID = 加权平均查询.物资ID);"
DoCmd.RunSQL sql '生成临时表查询 sql = "UPDATE 临时表 INNER JOIN 发料查询 ON (临时表.记录号 = 发料查询.记录号) AND (临时表.物资ID = 发料查询.物资ID) SET 发料查询.单价 = 临时表.发料单价, 发料查询.金额 = 临时表.发料金额;" DoCmd.RunSQL sql '更新发料表查询 Case Is = 2 '移动平均计价法 DoCmd.GoToRecord acForm, "主窗体", acFirst For i = 1 To DCount("*", "卡片", "true") sql = "SELECT No AS 标记, (select count(*) from 基础查询 as b where b.物资ID=a.物资ID and b.记录号<=a.记录号) AS 序号, " sql = sql + "a.记录号, a.日期, a.物资ID, a.名称, a.期初单价, a.期初数量, a.期初金额, a.收料单价, a.收料数量, a.收料金额, a.发料单价, a.发料数量, a.发料金额, "
sql = sql + "Round(期末金额/期末数量,2) AS 期末单价, (select sum(b.期初数量+b.收料数量-b.发料数量) from 基础查询 as b where b.物资ID=a.物资ID and b.记录号<=a.记录号) AS 期末数量, " sql = sql + "(select sum(b.期初金额+b.收料金额-b.发料金额) from 基础查询 as b where b.物资ID=a.物资ID and b.记录号<=a.记录号) AS 期末金额 " sql = sql + "INTO 临时表 FROM 基础查询 AS a where 物资ID=forms![主窗体]![物资ID];" DoCmd.RunSQL sql '生成临时表查询 For j = 1 To DCount("*", "移动平均查询", "发料数量<>0") sql = "SELECT 记录号, 物资ID, 发料单价, 发料数量,发料金额 INTO 临时表2 FROM 移动平均单价查询;" DoCmd.RunSQL sql '生成临时表2查询 sql = "UPDATE 发料查询 INNER JOIN 临时表2 ON (发料查询.物资ID = 临时表2.物资ID) AND (发料查询.记录号 = 临时表2.记录号) SET 发料查询.单价 = 临时表2.发料单价, 发料查询.金额 = round(临时表2.发料单价*发料查询.数量,2);" DoCmd.RunSQL sql '更新发料表查询 sql = "UPDATE 临时表2 INNER JOIN 临时表 ON (临时表2.物资ID = 临时表.物资ID) AND (临时表2.记录号 = 临时表.记录号) SET 临时表.发料单价 = 临时表2.发料单价, 临时表.发料金额 = 临时表2.发料金额;" DoCmd.RunSQL sql '更新临时表查询 sql = "UPDATE 移动平均单价查询 SET 移动平均单价查询.标记 = -1;" DoCmd.RunSQL sql '更新临时表标识查询 Next DoCmd.GoToRecord acForm, "主窗体", acNext, 1 Next DoCmd.GoToRecord acForm, "主窗体", acFirst End Select DoCmd.RunSQL "DELETE * FROM 临时表;" DoCmd.RunSQL "DELETE * FROM 临时表2;" Me.子窗体.Form.Requery Me.计价选项 = 0 '增加下月期初月结单号 DoCmd.RunSQL "INSERT INTO 单号表 ( 单据ID, 类型, 日期 ) SELECT '01' AS 单据ID, '月结单' AS 类型, DateAdd('m',1,Forms!主窗体!月度) AS 日期;" '生成临时表 sql = "SELECT '01' AS 单据ID, '' AS ID, DateAdd('m',1,forms!主窗体!月度) AS 日期, 收发存查询.物资ID, 收发存查询.期末单价, 收发存查询.期末数量, 收发存查询.期末金额 INTO 临时表 " sql = sql + "FROM 收发存查询 WHERE (((收发存查询.序号)=99));" DoCmd.RunSQL sql '追加期初 sql = "INSERT INTO 期初 ( 单据ID, ID, 日期, 物资ID, 单价, 数量, 金额 ) " sql = sql + "SELECT a.单据ID, Format((select count(*) from 临时表 as b where b.物资ID<=a.物资ID),'00') AS ID, a.日期, a.物资ID, a.期末单价, a.期末数量, a.期末金额 " sql = sql + "FROM 临时表 AS a;" DoCmd.RunSQL sql Else sql = "UPDATE 发料 SET 发料.单价 = 0, 发料.金额 = 0;" DoCmd.RunSQL sql '发料表清零查询 sql = "DELETE 期初.单据ID, 期初.ID, 期初.日期, 期初.物资ID, 期初.单价, 期初.数量, 期初.金额, Format([日期],'yymm') AS 月度 " sql = sql + "FROM 期初 WHERE (((Format([日期],'yymm'))=Format(DateAdd('m',1,[forms]![主窗体]![月度]),'yymm')));" DoCmd.RunSQL sql '删除期初 sql = "DELETE 单号表.日期, 单号表.单据ID, 单号表.类型, Format([日期],'yymm') AS 月度 " sql = sql + "FROM 单号表 WHERE (((单号表.类型)='月结单') AND ((Format([日期],'yymm'))=Format(DateAdd('m',1,[forms]![主窗体]![月度]),'yymm')));" DoCmd.RunSQL sql '删除期初月结单记录号 Me.子窗体.Form.Requery End If End Sub
[ 本帖最后由 todaynew 于 2009-1-20 07:10 编辑 ] |