rs.Filter数据无法跨工程传递
在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 的值没有传递
请高手指教。
[解决办法]
去掉Set rs = Nothing 看看
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
[解决办法]
如下这样试试:
- VB code
Private Function GetRate(FID As Long, Fdate As Date,rst as recordset) As longon 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
[解决办法]
Private Function GetRate(FID As Long, Fdate As Date) As ADODB.Recordset ?你声明为私有的了,当然不可以在进程外存取了,把Private 换成Public试试吧
[解决办法]
Dim rs As New ADODB.Recordset
此句应改为模块级或全局变量,否则,过程结束后VB会自动释放超过作用域的变量,从而导致rs为Nothing。
[解决办法]
另外,还需要在过程中去掉set rs=nothing一句。
[解决办法]
在ActiveX EXE/DLL工程中添加一个公共类,在类中添加一个公共方法,代码如下:
Dim m_RS As ADODB.Recordset
Public 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
[解决办法]
我理解错了?让你传址理解错了?.....
[解决办法]
byval FID As Long, byval Fdate As Date
而且只能是DLL.
[解决办法]
貌似是不行的
rs.Filter只是过滤一下,但是rs的数据并没有发生任何改变
因此如果真需要过滤后的rs的话,可以重新构造一个recordset
然后把过滤后的rs写进重新构造的recordset
[解决办法]
另外,如果是这样的函数结构,我觉得也没必要用rs.Filter
直接把filter的内容放到where条件中不是更好?
- VB code
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 = NothingEnd Function