在B工程类型CRATE中,有函数
Private Function GetRate(FID As Long, Fdate As Date) As ADODB.Recordset
    Dim rs As New ADODB.Recordset
    
    Set rs = Database.Execute("select * from table")   'rs可以读取5条数据
    
    rs.Filter = "fid=" & FID & " and fdate='" & Fdate & "'" '经过过滤得到一条正确的数据
    
    Set GetRate = rs
    
    Set rs = Nothing
End Function我在A工程中创建B工程的CRTE对象,然后调用函数
Dim obj As Object
Dim rs As New ADODB.Recordset
    
Set obj = CreateObject("B.CRTE")
set rs=obj.GetRate(fid,fdate)       '然而到这得到的数据又是5条数据。也就是说B工程中的rs.Filter 的值没有传递请高手指教。

解决方案 »

  1.   

    去掉Set rs = Nothing 看看  
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  2.   

    如下这样试试:Private Function GetRate(FID As Long, Fdate As Date,rst as recordset) As long
    on error goto err_GetRate       Dim rs As Recordset 
        Set rs = Database.Execute("select * from table")  'rs可以读取5条数据 
        rs.Filter = "fid=" & FID & " and fdate='" & Fdate & "'" '经过过滤得到一条正确的数据  
        Set rst = rs 
        Set rs = Nothing
        GetRate=1err_GetRate:
        GetRate=0End Function 我在A工程中创建B工程的CRTE对象,然后调用函数 
        Dim obj As Object 
        Dim rs As Recordset 
        
        Set obj = CreateObject("B.CRTE") 
        obj.GetRate fid,fdate,rs      
      

  3.   

    谢谢1楼,试过了,还是不行。
    2楼理解错误。将函数改为返回值为long型,那是肯定没问题的。我的也有返回,只是没有按我的要求返回正确的那条数据。
    如果2楼将后面的rst as recordset改为按地址传递,倒是可以一试。
    不管怎样,先谢谢2位。我先回公司去验证下,可不可以。问题解决就结贴。
      

  4.   

    Private Function GetRate(FID As Long, Fdate As Date) As ADODB.Recordset ?你声明为私有的了,当然不可以在进程外存取了,把Private 换成Public试试吧
      

  5.   

    Dim rs As New ADODB.Recordset
    此句应改为模块级或全局变量,否则,过程结束后VB会自动释放超过作用域的变量,从而导致rs为Nothing。
      

  6.   

    另外,还需要在过程中去掉set rs=nothing一句。
      

  7.   

    在ActiveX EXE/DLL工程中添加一个公共类,在类中添加一个公共方法,代码如下:
    Dim m_RS As ADODB.RecordsetPublic Function GetRate(FID As Long, Fdate As Date) As ADODB.Recordset
        If Not (m_RS Is Nothing) Then
            m_RS.Close
            Set m_RS = Nothing
        End If
        Set m_RS = Database.Execute("select * from table")  'rs可以读取5条数据
        m_RS.Filter = "fid=" & FID & " and fdate='" & Fdate & "'" '经过过滤得到一条正确的数据
        Set GetRate = m_RS
    End Function
      

  8.   

    回复4楼,我的是用public的,不然外部是没办法调用的。在这里是我写错了。
    回复lyserver,我按照你的方法试过了,问题依然存在
      

  9.   

    byval FID As Long, byval Fdate As Date
    而且只能是DLL.
      

  10.   

    貌似是不行的
    rs.Filter只是过滤一下,但是rs的数据并没有发生任何改变
    因此如果真需要过滤后的rs的话,可以重新构造一个recordset
    然后把过滤后的rs写进重新构造的recordset
      

  11.   

    另外,如果是这样的函数结构,我觉得也没必要用rs.Filter
    直接把filter的内容放到where条件中不是更好?
    Private Function GetRate(FID As Long, Fdate As Date) As ADODB.Recordset
        Dim rs As New ADODB.Recordset
        Dim strSQL As String
        
        strSQL = "select * from table Where 1=1"
        
        If FID > 0 Then
            strSQL = strSQL & " AND FID=" & FID
        End If
        If IsDate(Fdate) Then
            strSQL = strSQL & " AND FDate='" & Fdate & "'"
        End If
        
        Set rs = Database.Execute(strSQL)  'rs可以读取5条数据
        
        Set GetRate = rs.Clone
        
        Set rs = Nothing
    End Function
      

  12.   

    12楼的方式最合理。又:Database 是什么对象?
    如果是数据环境,B 与 A 之间不共享,rs 返回 A 时可能进行了数据克隆,导致过滤无效。