设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

[ADO/DAO] 数据返回问题

[复制链接]
跳转到指定楼层
1#
发表于 2009-2-14 10:26:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我在ACCESS中写以下的代码:
Dim cTab As Recordset
    Dim cTab As Recordset
    Set cTab = CurrentDb.OpenRecordset("生产进度查询", 2)
    For i = 1 To cTab.RecordCount
在"生产进度查询"中可见数据有超过10000条,但返回的"cTab.RecordCount"值仅为1,请问题各位是什么原因造成的.有关ADO的写法我也是刚在学习,请各位大大帮忙看看,谢谢!在线等待中!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2009-2-14 10:35:46 | 只看该作者
你用的是DAO,1就是有值,要movelast一下才知道有多少条
不知道要用循环作什么,可以考虑用do循环
3#
 楼主| 发表于 2009-2-14 11:38:28 | 只看该作者
本帖最后由 mag770t 于 2009-2-14 11:39 编辑

各位大大,我要实现的是将"订单配套表"中的"送成品时间"更新进"生产进度表"中的"成品入库"中,两表以订单编号进行1:1的关联,因两表中数据量都比较大,超10000条,请各位看看如何实现.谢谢! 下面为我写的代码,问题为"cTab.RecordCount"值为1,所以在做"For"循环就不能实现:
Dim cTab As Recordset
    Dim nTab As Recordset
    Set cTab = CurrentDb.OpenRecordset("生产进度表", 2)
    For i = 1 To cTab.RecordCount
        cOrdID = cTab("订单编号")
        Set nTab = CurrentDb.OpenRecordset("select 订单编号,送成品时间 from 订单配套表 where 订单编号='" & cOrdID & "'", 2)
        nData = nTab("送成品时间")
        DoCmd.SetWarnings False '屏蔽操作查询提示
        
        DoCmd.RunSQL "update 生产进度表 set 成品入库= " & nData & " where 订单编号='" & cOrdID & "'"
    Next
4#
发表于 2009-2-14 12:29:02 | 只看该作者
本帖最后由 wu8313 于 2009-2-14 12:38 编辑

在代码窗格中,按f2,得到 "对象浏览器",使用搜索,你的问题涉及 dao,所以搜索关键词 ,如图片所示。

然后在 图片中所示的位置,点击那个 问号,弹出acc的帮助,帮助里面给出了 示例代码,就是用来解释 recordcount 属性的。

-----------------------帮助中给出的代码----------------------
Sub RecordCountX()
   Dim dbsNorthwind As Database
   Dim rstEmployees As Recordset
   
   'Set dbsNorthwind = OpenDatabase("Northwind.mdb") '帮助中的原代码
   Set dbsNorthwind = OpenDatabase(CurrentProject.path & "\" & "Northwind.mdb") '这里我修改了一下,才能针对当前库中的"employees"表进行引用
   
   ' Debug.Print dbsNorthwind.Name
   With dbsNorthwind
      ' Open table-type Recordset and show RecordCount
      ' property.
      Set rstEmployees = .OpenRecordset("Employees")
      Debug.Print _
         "Table-type recordset from Employees table"
      Debug.Print "  RecordCount = " & _
         rstEmployees.recordcount
      rstEmployees.Close
      ' Open dynaset-type Recordset and show RecordCount
      ' property before populating the Recordset.
      Set rstEmployees = .OpenRecordset("Employees", _
         dbOpenDynaset)
      Debug.Print "Dynaset-type recordset " & _
         "from Employees table before MoveLast"
      Debug.Print "  RecordCount = " & _
         rstEmployees.recordcount
      ' Show the RecordCount property after populating the
      ' Recordset.
      rstEmployees.MoveLast
      Debug.Print "Dynaset-type recordset " & _
         "from Employees table after MoveLast"
      Debug.Print "  RecordCount = " & _
         rstEmployees.recordcount
      rstEmployees.Close
      ' Open snapshot-type Recordset and show RecordCount
      ' property before populating the Recordset.
      Set rstEmployees = .OpenRecordset("Employees", _
         dbOpenSnapshot)
      Debug.Print "Snapshot-type recordset " & _
         "from Employees table before MoveLast"
      Debug.Print "  RecordCount = " & _
         rstEmployees.recordcount
      ' Show the RecordCount property after populating the
      ' Recordset.
      rstEmployees.MoveLast
      Debug.Print "Snapshot-type recordset " & _
         "from Employees table after MoveLast"
      Debug.Print "  RecordCount = " & _
         rstEmployees.recordcount
      rstEmployees.Close
      ' Open forward-only-type Recordset and show
      ' RecordCount property before populating the
      ' Recordset.
      Set rstEmployees = .OpenRecordset("Employees", _
         dbOpenForwardOnly)
      Debug.Print "Forward-only-type recordset " & _
         "from Employees table before MoveLast"
      Debug.Print "  RecordCount = " & _
         rstEmployees.recordcount
      ' Show the RecordCount property after calling the
      ' MoveNext method.
      rstEmployees.MoveNext
      Debug.Print "Forward-only-type recordset " & _
         "from Employees table after MoveNext"
      Debug.Print "  RecordCount = " & _
         rstEmployees.recordcount
      rstEmployees.Close
      .Close
   End With
End Sub
-------------------------------------------------------------

附件中,包含 一个模块 "Mod_recordcount"(就是上面的代码),一个表"employees".

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
5#
发表于 2009-2-14 15:42:02 | 只看该作者
以下摘自 Microsoft Access 2003 Help

Use the RecordCount property to find out how many records in a Recordset or TableDef object have been accessed. The RecordCount property doesn't indicate how many records are contained in a dynaset-, snapshot-, or forward-only–type Recordset object until all records have been accessed.

你的 CurrentDb.OpenRecordset("生产进度表", 2) 2= dbOpenDynaset

把你的循环控制改为
        do while not cTab.eof
        loop

另外你的程序很怪异,循环了这么多次做的都是相同的事件,你FOR I=循环中的I在循环体中根本就没出现。 所以也没办法帮你写出do while的完整程序了。



******************
*  一切皆有可能  *
******************

.

.
QQ群 48866293 / 12035577 / 7440532 / 13666209
http://forum.csdn.net/BList/OtherDatabase .
http://www.accessbbs.cn/bbs/index.php .
http://www.accessoft.com/bbs/index.asp .
http://www.access-programmers.co.uk/forums .
http://www.office-cn.net .
.
http://www.office-cn.net/home/space.php?uid=141646 .
6#
发表于 2009-2-14 16:46:58 | 只看该作者
如果你在10000条的循环里用DoCmd.RunSQL ,那就完蛋了
7#
 楼主| 发表于 2009-2-16 08:47:00 | 只看该作者
哦!
"如果你在10000条的循环里用DoCmd.RunSQL ,那就完蛋了"!我知道,做超10000的DoCmd.RunSQL的速度会很慢的,不知你"tz-chf元老",那你帮忙看看怎样才能实现我的需求,试试!....
8#
发表于 2009-2-16 10:21:45 | 只看该作者
两个表是一对一关系,直接查询就可以了。
9#
发表于 2009-2-16 21:31:23 | 只看该作者
本帖最后由 wu8313 于 2009-2-17 09:37 编辑

根据你给出的情况,做了一个示例,你看看是否解决你的问题。

1:这一步,需要面对两个表
我把你的两个表 "订单配套表"  ,"生产进度表",模拟成我示例中的 "tbl_a" 和 "tbl_b"

2:通过一个选择查询,把两个表的事情简化为一个查询
做一个选择查询 "qry_1",包含两个表中 date 列,这个完全没有任何问题,因为两个表是一对一的关系

3:现在的问题,就是 如何在第二步的查询中让:
表b中的 1990-6-5 更新为 表a中的 1980-06-05
表b中的 1991-6-5 更新为 表a中的 1981-06-05

具体形如 我给出的图片。(颜色相同,表明id相同,也希望 日期通过更新变得相同)

4:运行如下代码,完成第3步体提出的要求,完毕!
-------------------------------------------------
Sub my_update()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("select * from qry_1")
Dim m As Long
Dim h As Long
m = DCount("编号", "qry_1") '记录很多的话有些慢,可以参考我在 4 楼给的代码来获取记录数作为for循环的终值
With rs
    .MoveFirst
   
    For i = 1 To m
        .Edit
        h = Nz(.Fields("tbl_a.date"), Empty)
        
            Select Case h '根据一定的条件,向处在同一行的另外一个列写入
                Case h
                .Fields("tbl_b.date") = Nz(.Fields("tbl_a.date"), Empty)
                .Update
            End Select
            
        .MoveNext
    Next i
   
End With
rs.Close
Set db = Nothing
Set rs = Nothing
End Sub
-----------------------------------------------------
总之,问题可以描述为:在同一个id所在的行,根据某列的值,更新另外一列的值。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

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

本版积分规则

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

GMT+8, 2024-5-10 17:02 , Processed in 0.084475 second(s), 33 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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