Solved

how to close a recordset really?

Posted on 2000-04-28
5
160 Views
Last Modified: 2010-05-02
as follow:
*************************************
Function rhigh(maxdate As Date, Edate As Date, stockID As String, n As Integer, resultvalue As Integer, eachfield As String, resultfield As String)
Dim RSm As ADODB.Recordset
Dim str
Dim rmaxdate As Date
Set RSm = New ADODB.Recordset
str = "select count(*) as sum ,max(" & eachfield & ") as max ,stockID from " & stockID & " where tdate>#" & maxdate & "# and tdate<#" & Edate & "# group by stockID"
    Set RSm = stockDB.Execute(str)
    If Not RSm.EOF Then
    If RSm("sum") > n Then
        str = "select tdate from " & stockID & " where tdate>#" & maxdate & "# and tdate<#" & Edate & "# and " & eachfield & "=" & RSm("max") & " order by tdate"
        RSm.Close
        RSm.Open str, stockDB, adOpenStatic
        If Not RSm.EOF Then
            rmaxdate = RSm("tdate")
            RSm.Close
            str = "select count(*) as count from " & stockID & " where tdate<#" & rmaxdate & "# and tdate>#" & maxdate & "#"
            RSm.Open str, stockDB
            If RSm("count") >= n Then
                str = "update resulthis set " & resultfield & "='" & resultvalue & "' where stockID ='" & stockID & "' and tdate=#" & rmaxdate & "#"
                stockDB.Execute str
                lhigh maxdate, rmaxdate, stockID, n, resultvalue, eachfield, resultfield
            End If
            rhigh rmaxdate, Edate, stockID, n, resultvalue, eachfield, resultfield
        End If
    End If
    End If
    RSm.Close
End If
End Function

Function newlhigh(maxdate As Date, Edate As Date, stockID As String, n As Integer, resultvalue As Integer, eachfield As String, resultfield As String)
Dim RSm As ADODB.Recordset
Dim str, rmaxdate
Set RSm = New ADODB.Recordset
If maxdate < Edate And Edate > startdate Then
        str = "select max(" & eachfield & ") as max ,stockID from " & stockID & " where tdate>#" & maxdate & "# and tdate<#" & Edate & "# group by stockID"
        Set RSm = stockDB.Execute(str)
        str = "select tdate from " & stockID & " where tdate>#" & maxdate & "# and tdate<#" & Edate & "# and " & eachfield & "=" & RSm("max") & " order by tdate"
        RSm.Close
        RSm.Open str, stockDB, adOpenStatic
        If Not RSm.EOF Then
            str = "update resulthis set " & resultfield & "='" & resultvalue & "' where stockID ='" & stockID & "' and tdate=#" & RSm("tdate") & "#"
            stockDB.Execute str
            newlhigh maxdate, RSm("tdate"), stockID, n, resultvalue, eachfield, resultfield
            rhigh RSm("tdate"), Edate, stockID, n, resultvalue, eachfield, resultfield
        End If
        RSm.Close
End If
End Function

Function lhigh(maxdate As Date, Edate As Date, stockID As String, n As Integer, resultvalue As Integer, eachfield As String, resultfield As String)
Dim RSm As ADODB.Recordset
Dim str, rmaxdate
Set RSm = New ADODB.Recordset
If maxdate < Edate And Edate > startdate Then
    str = "select count(*) as sum ,max(" & eachfield & ") as max ,stockID from " & stockID & " where tdate>#" & maxdate & "# and tdate<#" & Edate & "# group by stockID"
    Set RSm = stockDB.Execute(str)
    If Not RSm.EOF Then
    If RSm("sum") > n Then
        str = "select tdate from " & stockID & " where tdate>#" & maxdate & "# and tdate<#" & Edate & "# and " & eachfield & "=" & RSm("max") & " order by tdate"
        RSm.Close
        RSm.Open str, stockDB, adOpenStatic
        If Not RSm.EOF Then
            str = "update resulthis set " & resultfield & "='" & resultvalue & "' where stockID ='" & stockID & "' and tdate=#" & RSm("tdate") & "#"
            stockDB.Execute str
            lhigh maxdate, RSm("tdate"), stockID, n, resultvalue, eachfield, resultfield
            rhigh RSm("tdate"), Edate, stockID, n, resultvalue, eachfield, resultfield
        End If
    End If
    End If
    RSm.Close
End If
End Function
**************************************
errors appears:
runtime error(80004005),cannot open a table again.

please tell me why to open many table but not to close them really?
how to do ?

0
Comment
Question by:senucarthur
5 Comments
 
LVL 3

Accepted Solution

by:
myqlG earned 50 total points
ID: 2760366
rs.close
set rs = nothing
0
 
LVL 2

Expert Comment

by:Sage020999
ID: 2760368
set RSm = nothing
0
 
LVL 6

Expert Comment

by:Marine
ID: 2760371
To properly close recordset is to first close it by using .Close Method and then to Set it to Nothing therefore releasing it from memory. You can use more then one recordset which will point to other tables.
0
 
LVL 6

Expert Comment

by:Marine
ID: 2760390
you using connection alot and executing Update statements on it which is great, but if you don't want it to return recordset do it like this.

stockDB.Execute str,,adExecuteNoRecords
Because behind the sence ADO builds a recordset for you, this won't cause you problems but it might be faster for your application to run.
               
0
 
LVL 15

Expert Comment

by:ameba
ID: 2761087
You are using recursion, which is not good for cleanup of your variables.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now