设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2398|回复: 2
打印 上一主题 下一主题

[模块/函数] 一段按照页数自动更新页码的代码,感觉很简单的功能却写了这么长代码,是不是思路...

[复制链接]
1#
发表于 2015-5-21 09:52:25 | 显示全部楼层
本帖最后由 todaynew 于 2015-5-21 10:58 编辑

有呀,用三个更新查询即可:
    Dim ssql As String
    ssql = "update 表2 set 页次=1 where 序号=1"
    CurrentDb.Execute ssql

    ssql = "update 表2 set 页次="
    ssql = ssql & "DLookUp('页次','表2','档号=""' & [档号] & '"" and 序号=1')"
    ssql = ssql & "+DSum('页数','表2','档号=""' & [档号] & '"" and 序号<' & [序号])"
    ssql = ssql & " where 序号>1"
    CurrentDb.Execute ssql

    ssql = "update 表2 set 文件级档号=档号 & '.' & format(页次,'000')"
    CurrentDb.Execute ssql


如果用ado处理,则可以用两个循环处理:
Function UpdateJsYs()
    Dim rst1 As new ADODB.Recordset,rst2 As new ADODB.Recordset
    dim ssql as string
    dim i as long,j as long
    dim cnt as long

    ssql="select distinct 档号 from 表2"
    rst1.Open ssql, CurrentProject.Connection, 1, 3
    for i=1 to rst1.RecordCount  '按档号循环
        ssql="select * from 表2 where 档号='" & rst1!档号.value & "' order by 序号"
        rst2.Open ssql, CurrentProject.Connection, 1, 3
        cnt=0
        for j=1 to rst2.recordcount
            if j=1 then
                rst2!页次.value=1
             else
                rst2!页次.value=cnt
            end if
            rst2!文件级档号.value=rst2!档号 & "." & Format(rst2!页次, "000")
            rst2.update           
            '用cnt保存本条记录的页次与页数之和,准备赋值给下一条记录的页次
            '也可将写为:cnt=cnt+rst2!页数.value,同时将rst2!页次.value=cnt改为rst2!页次.value=cnt+1
            cnt=rst2!页次.value+rst2!页数.value  
            rst2.movenext
        next
        rst2.close
        rst1.movenext
    next
    set rst2=nothing
    rst1.close:set rst1=nothing
End Function





评分

参与人数 1经验 +10 收起 理由
zpy2 + 10 (V币)优秀原创教程、管理建议(1-5分)

查看全部评分

回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2025-4-29 09:42 , Processed in 0.117231 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表