我在执行如下语句时遇到问题,我的tblcz中有15条满足条件纪录的,可是debug.print rs.RecordCount的结果却是1,只把第一条纪录取了出来,请问这是怎么回事,我在前面的代码中使用是没问题的哦?
Private Sub Form_Load()
    Dim db As Database
    Dim rs As Recordset
    
    Set db = OpenDatabase("c:\cz.mdb", False, False)
    Set rs = db.OpenRecordset("select * from tblcz where name='"+trim(text1.text)+"'")
    Debug.Print rs.RecordCount
End Sub

解决方案 »

  1.   

    Private Sub Form_Load()
        Dim db As Database
        Dim rs As Recordset
        
        Set db = OpenDatabase("c:\cz.mdb", False, False)
        Set rs = db.OpenRecordset("select * from tblcz where name='"+trim(text1.text)+"'")
        if rs.RecordCount>0 then rs.MoveLast'把游标指向最后一条
        Debug.Print rs.RecordCount'这个就是所有条数了
    End Sub
      

  2.   

    这样处理的确可以实现,但是请问为何要把游标指向最后一条?
    ==========
    DAO不像ADO的,得到RecordCount后,还要通过MoveLast或MoveNext之类的事件来访问记录集之后才能得到实际的RecordCount值,要不然即使有记录都会是1。详情请查阅MSDN:
      

  3.   

    RecordCount Property
       Returns the number of records accessed in a CdbRecordset object or the total number of records in a table-type CdbRecordset or CdbTableDef object.SyntaxLONGGetRecordCount(VOID);ResUse the RecordCount property to find out how many records in a CdbRecordset or CdbTableDef object have been accessed. The RecordCount property doesn't indicate how many records are contained in a dynaset-, snapshot-, or forward-only–type CdbRecordset object until all records have been accessed. Once the last record has been accessed, the RecordCount property indicates the total number of undeleted records in the CdbRecordset or CdbTableDef object. To force the last record to be accessed, use the MoveLast method on the CdbRecordset object. You can also use an SQL Count function to determine the approximate number of records your query will return.Note Using the MoveLast method to populate a newly opened CdbRecordset negatively impacts performance. Unless it is necessary to have an accurate RecordCount as soon as you open a CdbRecordset, it's better to wait until you populate the CdbRecordset with other portions of code before checking the RecordCount property.As your application deletes records in a dynaset-type CdbRecordset object, the value of the RecordCount property decreases. However, records deleted by other users aren't reflected by the RecordCount property until the current record is positioned to a deleted record. If you execute a transaction that affects the RecordCount property setting and you subsequently roll back the transaction, the RecordCount property won't reflect the actual number of remaining records.The RecordCount property of a snapshot- or forward-only–type CdbRecordset object isn't affected by changes in the underlying tables.A CdbRecordset or CdbTableDef object with no records has a RecordCount property setting of 0.When you work with linked CdbTableDef objects, the RecordCount property setting is always –1.Using the Requery method on a CdbRecordset object resets the RecordCount property just as if the query were re-executed.
      

  4.   

    我用的是ADO,曾经有一次也遇到同样的问题,后来发现是OPEN方法的一参数的设置问题,与游标的位置有一定的关系,不知道是不是对你有帮助
      

  5.   

    Private Sub Form_Load()
        Dim db As Database
        Dim rs As Recordset
        
        Set db = OpenDatabase("c:\cz.mdb", False, False)
        Set rs = db.OpenRecordset("select * from tblcz where name='"+trim(text1.text)+"'")
        rs.movelast
        rs.movefirst
        Debug.Print rs.RecordCount
    End Sub
      

  6.   

    Private Sub Form_Load()
        Dim db As Database
        Dim rs As Recordset    
        Set db = OpenDatabase("c:\cz.mdb", False, False)
        Set rs = db.OpenRecordset("select * from tblcz where name='"+trim(text1.text)+"'")
        if rs.eof and rs.bof then exit sub
        rs.movelast    
        Debug.Print rs.RecordCount
    End Sub