Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

docmd.runsql能否返回值

无际  发表于:2013-10-10 11:20:32  
复制

现在需要实现一个功能,数据表绑定了一个窗体,从数据表中查询一个值,该值需满足的条件是:同窗体一个控件的值accounttype同类别,但是同窗体另一个recordid控件值不同确实accounttype类别中最大的值。找了多种方法一直不能实现,下面粘贴代码,麻烦问下高手,如何实现?如果使用这个代码,哪里需要改进,多谢!

Public Sub TxtlastCal()   '自定义过程,自动通过同类别上次余额及更新后“进账”“出账”计算余额
Dim curlast As Currency
Dim curtype As String
Dim maxid As Integer
Dim curid As Integer
Dim strSQL As String
Dim rst As Recordset


curtype = Me.accounttype.Value
curid = Me.txtrecordID

strSQL = "SELECT tblaccountinfo.last,tblaccountinfo.recordid FROM tblaccountinfo WHERE (((tblaccountinfo.recordid)<>[Forms]![frmaccountinfo]![txtrecordid]) AND ((tblaccountinfo.accounttype)=[Forms]![frmaccountinfo]![cboaccounttype]))"
rst = DoCmd.RunSQL(strSQL)

maxid = DMax([recordid], "rst") '找到同现有类型相同的最新记录
curlast = CCur(DLookup("[last]", "tblaccountinfo", [recordid] = maxid)) '找到的最新余额赋值给变量curlast
Me.txtlast = curlast + (Me.txtincome - Me.txtoutgo)   '自动计算本次余额为多少

End Sub


 

Top
煮江品茶 发表于:2013-10-10 15:39:36
煮江品茶 发表于:2013-10-10 16:11:05

有货品表、库存表、收发表三个数据表:
货品表:货品ID(主键)、品名、规格、单位
库存表:库存ID(主键)、日期、货品ID(外键)、数量、单价
收发表:收发ID(主键)、日期、作业类别(收/发)、货品ID(外键)、数量、单价


1、在收发数据集中得到对应库存表的最后记录,可以这样写:


 select *,nz(dmax("日期","库存表","货品ID=" & [货品ID] & " and 日期<=#" & [日期] & "#"),date()) as 库存表最后日期
from 收发表

 

2、进一步获得库存表对应的数量,可以这样写:


 select *,nz(dlookup("数量","库存表","货品ID=" & [货品ID]," and 日期=#" & nz(dmax("日期","库存表","货品ID=" & [货品ID] & " and 日期<=#" & [日期] & "#") & "#"),0),date()) as 数量
from 收发表

 

3、在进一步获得当前日期与库存表最后日期之间的累计收料数量,可以这样写:


 select *,nz(dsum("数量","收发表","货品ID=" & [货品ID] & " and 作业类别='收' and 日期>=#" & nz(dmax("日期","库存表","货品ID=" & [货品ID] & " and 日期<=#" & [日期] & "#"),date()) & "# and 日期<=#" & [日期] & "#"),0) as 累计入库数量
from 收发表

 

4、最后计算库存的完整查询可以这样写:


 select *, _
nz(dlookup("数量","库存表","货品ID=" & [货品ID]," and 日期=#" & nz(dmax("日期","库存表","货品ID=" & [货品ID] & " and 日期<=#" & [日期] & "#") & "#"),0),date()) _
+ nz(dsum("数量","收发表","货品ID=" & [货品ID] & " and 作业类别='收' and 日期>=#" & nz(dmax("日期","库存表","货品ID=" & [货品ID] & " and 日期<=#" & [日期] & "#"),date()) & "# and 日期<=#" & [日期] & "#"),0) _
- nz(dsum("数量","收发表","货品ID=" & [货品ID] & " and 作业类别='发' and 日期>=#" & nz(dmax("日期","库存表","货品ID=" & [货品ID] & " and 日期<=#" & [日期] & "#"),date()) & "# and 日期<=#" & [日期] & "#"),0) as 当前库存量
from 收发表


按照这个基本的处理思路,也可以用子查询、函数等方法解决问题。



无际 发表于:2013-10-11 16:05:24

多谢老师的悉心指导,给了我换种方式解决问题的思考,现在已经解决了问题,代码如下:


curtype = Me.accounttype
curid = Me.txtrecordID

curlast = DLookup("moneylast", "tblaccountinfo", recordid = DMax("recordid", "tblaccountinfo", accounttype = curtype And recordid <> curid))

Me.txtlast = curlast + (Me.txtincome - Me.txtoutgo)   '自动计算本次余额为多少

这样就可以自动提取上次同类别最新(最后一条记录,不是日期最新的记录)记录,自动计算!



总记录:3篇  页次:1/1 9 1 :