小弟,想得到记录中的数目,我试过2个办法都,遇到了问题,请大家帮助我把程序修改一下!(我用的是ACCESS 97 数据库)1。用RecordCount的方法,但是返回的是-1
Dim conn As ADODB.Connection '定义ADO链接conn
Dim rs As ADODB.Recordset '定义ADO记录集rs
Dim str As String
Set conn = New ADODB.Connection '初始化conn,否则会出错
conn.Open "driver={Microsoft Access Driver (*.mdb)};pwd='';dbq=d:\shcandle\shcandle"str = "select * from candle where ItemNo like '%" & strin & "%'"Set rs = conn.Execute(str)MsgBox (rs.RecordCount)
我看过一些文章,说道可能是游标设置,打开方式之类的问题,我试过,但是我的好像没有用,请帮我修改一下!!谢谢
2。SQL的办法Dim conn As ADODB.Connection '定义ADO链接conn
Dim rs As ADODB.Recordset '定义ADO记录集rs
Dim str As String
Dim numstr As String
Set conn = New ADODB.Connection '初始化conn,否则会出错
conn.Open "driver={Microsoft Access Driver (*.mdb)};pwd='';dbq=d:\shcandle\shcandle"numstr = "select count(*) from candle where ItemNo like '%" & strin & "%'"
str = "select * from candle where ItemNo like '%" & strin & "%'"Dim y As Integer
y = CInt(conn.Execute(numstr))
MsgBox (y)Set rs = conn.Execute(str)用该方法,在y = CInt(conn.Execute(numstr))这句上编译不通过,提示为类型不对,我不知道执行"select count(*) from candle where ItemNo like '%" & strin & "%'" 后,返回的是不是整数?如何修改?

解决方案 »

  1.   

    1.Conn.CursorLocation = adUseClient '设置为客户端
      

  2.   

    2.
    numstr = "select count(*) as lsum from candle where ItemNo like '%" & strin & "%'"
    str = "select * from candle where ItemNo like '%" & strin & "%'"
    Set rs = conn.Execute(str)
    msgbox rs.fields("lsum")
      

  3.   

    1.
    同意楼上,尽量不用RecordCount,在使用之前最好
    if not rs.eof  and not rs.bof then
       rs.movelast
       rs.movefirst
        MsgBox (rs.RecordCount)end if
      

  4.   

    2.
    Dim y As Integer
    numstr = "select count(*) as 总数 from candle where ItemNo like '%" & strin & "%'"
    str = "select * from candle where ItemNo like '%" & strin & "%'"
    Set rs = conn.Execute(numstr)if not rs.eof then
       y=rs.fields("总数")
    end if
    MsgBox (y)Set rs = conn.Execute(str)
      

  5.   

    rs.Open sql, mcnn, adOpenDynamic, , adCmdText
    第三个参数要设置成动态的,默认的那个不支持行集的反向搜索。-------------------------------------------------------------
    adOpenUnspecified -1 This indicates an unspecified value for the CursorType. 
    This value is not supported by the Microsoft® OLE DB Provider for AS/400 and VSAM or the Microsoft® OLE DB Provider for DB2.
     
    AdOpenForwardOnly 0 Specifying this value opens a forward-only-type cursor. This CursorType is identical to a static cursor, except that you can only scroll forward through records. This improves performance when only one pass through a Recordset is needed. 
    This value is not supported by the Microsoft® OLE DB Provider for AS/400 and VSAM.
     
    AdOpenKeyset 1 Specifying this value opens a keyset-type cursor. This CursorType is similar to a dynamic cursor with a few exceptions. Records that other users delete are inaccessible from your Recordset. Data changes to existing records by other users are still visible, but records added by other users are not visible (cannot be seen). 
    This value is not supported by the OLE DB Provider for AS/400 and VSAM.
     
    AdOpenDynamic 2 Specifying this value opens a dynamic-type cursor. Additions, changes, and deletions by other users are visible, and all types of movement through the recordset are allowed, except for books if the provider does not support them. 
    A dynamic cursor is the only CursorType supported by the OLE DB Provider for AS/400 and VSAM. 
     
    AdOpenStatic 3 Specifying this value opens a static-type cursor. A static cursor provides a static copy of a set of records that can be used to find data or generate reports. Additions, changes, or deletions by other users are not visible with a static cursor. 
    This value is not supported by the OLE DB Provider for AS/400 and VSAM.
     
    Note that the Open method on a Recordset object defaults this property to adOpenForwardOnly, a value that is mapped to adOpenDynamic by the OLE DB provider for AS/400 and VSAM.This property setting only affects connections established after the property has been set. Changing the CursorType property has no effect on existing connections. The CursorType property is read/write when the Recordset is closed and read-only when it is open.If a provider does not support the requested cursor type, the provider may return another cursor type. The CursorType property will change to match the actual cursor type in use when the Recordset object is open. To verify whether a specific returned cursor is supported, use the Supports method. When using the OLE DB Provider for AS/400 and VSAM, the Supports method returns adMovePrevious as true with CursorType set to adOpenDynamic. After you close the Recordset, the CursorType property reverts to its original setting.
      

  6.   

    For xx = 1 To rs.Recordset.RecordCount
    i=i+1
    rs.Recordset.MoveNext
    next xx
    msgbox "i"大概可以吧
      

  7.   

    。用RecordCount的方法,但是返回的是-1
    Dim conn As ADODB.Connection '定义ADO链接conn
    Dim rs As ADODB.Recordset '定义ADO记录集rs
    Dim str As String
    Set conn = New ADODB.Connection '初始化conn,否则会出错
    conn.Open "driver={Microsoft Access Driver (*.mdb)};pwd='';dbq=d:\shcandle\shcandle"str = "select * from candle where ItemNo like '%" & strin & "%'"rs.open str,conn,3,3MsgBox (rs.RecordCount)
    游标设置,打开方式之类有问题2。SQL的办法Dim conn As ADODB.Connection '定义ADO链接conn
    Dim rs As ADODB.Recordset '定义ADO记录集rs
    Dim str As String
    Dim numstr As String
    Set conn = New ADODB.Connection '初始化conn,否则会出错
    conn.Open "driver={Microsoft Access Driver (*.mdb)};pwd='';dbq=d:\shcandle\shcandle"numstr = "select count(*) from candle where ItemNo like '%" & strin & "%'"
    str = "select * from candle where ItemNo like '%" & strin & "%'"Dim y As Integer
    set rs=conn.execute (numstr)
    y=rs.field(0)
    rs.closeSet rs = conn.Execute(str)