Link to home
Start Free TrialLog in
Avatar of senucarthur
senucarthur

asked on

how to close a recordset really?

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 ?

ASKER CERTIFIED SOLUTION
Avatar of myqlG
myqlG

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sage020999
Sage020999

set RSm = nothing
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.
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.
               
You are using recursion, which is not good for cleanup of your variables.