R
Ray Cacciatore
I'm trying to implement a recursive function in Access. I
have a table that references itself. Two columns are
used, one is the parent id, the other is the child id.
The problem is that my recursive function calls itself so
many times that I get the error message:"Cannot open any
more tables". I have a line that opens a recordset in my
function using the CurrentDb.OpenRecordset method. It
looks like this line opens too many databases and
eventually shows the error message.
Here's the code for your convenience. Please Help.
Function LoadCategory(sId) As Boolean
Dim rs As DAO.Recordset
Dim sql As String
Dim db As DAO.Database
'Check for the bottom of the tree
If IsNull(sId) Then
Exit Function
End If
Set db = CurrentDb
'/// put all the children of sID into a recordset
sql = "select * from tblMyTable WHERE mYear=2003 and
ParentCCID='" & sId & "'"
Set rs = CurrentDb().OpenRecordset(sql)
Do While Not rs.EOF
'Only print if it's a leaf node
If Len(rs("CCID")) = 5 Then
Debug.Print rs("CCID")
End If
'Recursive call to check for children
Call LoadCategory(rs("CCID"))
rs.MoveNext
Loop
If Not rs Is Nothing Then
rs.Close
End If
Set rs = Nothing
End Function
Is there another way to tackle the problem without having
to open too many recordsets.
Thanks in advance
Ray C.
have a table that references itself. Two columns are
used, one is the parent id, the other is the child id.
The problem is that my recursive function calls itself so
many times that I get the error message:"Cannot open any
more tables". I have a line that opens a recordset in my
function using the CurrentDb.OpenRecordset method. It
looks like this line opens too many databases and
eventually shows the error message.
Here's the code for your convenience. Please Help.
Function LoadCategory(sId) As Boolean
Dim rs As DAO.Recordset
Dim sql As String
Dim db As DAO.Database
'Check for the bottom of the tree
If IsNull(sId) Then
Exit Function
End If
Set db = CurrentDb
'/// put all the children of sID into a recordset
sql = "select * from tblMyTable WHERE mYear=2003 and
ParentCCID='" & sId & "'"
Set rs = CurrentDb().OpenRecordset(sql)
Do While Not rs.EOF
'Only print if it's a leaf node
If Len(rs("CCID")) = 5 Then
Debug.Print rs("CCID")
End If
'Recursive call to check for children
Call LoadCategory(rs("CCID"))
rs.MoveNext
Loop
If Not rs Is Nothing Then
rs.Close
End If
Set rs = Nothing
End Function
Is there another way to tackle the problem without having
to open too many recordsets.
Thanks in advance
Ray C.