RE: Recursive function help

From: Tim M (TimM_at_discussions.microsoft.com)
Date: 07/28/04


Date: Wed, 28 Jul 2004 07:15:05 -0700

You open dbRS in GetParents() and then you try to use it again in GetReplies(). You need to explicitly define objects like these and other variables so you can be clear about their scope. (eg dim dbRs, conn, ...) You should also have the Option Explicit declaration at the start of the page.

You could use a single query and return all employees sorted in to levels and use nested do while loops or a recursive function.

Tim M

"JP SIngh" wrote:

> Hi All
>
> I am trying to write a recursive function to list the managers and his
> employees in a tree like sctructure
>
> Manager 1
> Emp1
> Emp1.1
> Emp 1.2
> Emp 2
> Emp 3
> Emp 3.1
> Emp 3.2
>
> Have written the following code but don't seems to be getting anywhere
>
> It gives me this error
>
> Microsoft VBScript runtime error '800a01fb'
> An exception occurred: 'dbRS.Open'
> /admin/man.asp, line 20
> Someone please help
>
> Regards
>
> Jas
>
> <%
> set conn = Server.Createobject("ADODB.Connection")
> conn.Provider = "Microsoft.Jet.OLEDB.4.0"
> conn.Open "D:\Applications\Holidays12004\Includes\holidays.mdb"
> Getparents()
>
> Function GetParents()
> Set dbRS = Server.CreateObject("ADODB.Recordset")
> ' ADO code to load all the posts with a null parent
> dbRS.Open "SELECT * FROM empprofile order by managername", conn
> Do While Not dbRS.EOF
> GetReplies(dbRS.Fields("empname"))
> dbRS.MoveNext
> Loop
> End Function
>
> Function GetReplies(parentID)
> Set dbRS = Server.CreateObject("ADODB.Recordset")
> dbRS.Open "SELECT * FROM empprofile WHERE managername = '" & parentid &
> "'", conn
> Do While Not dbRS.EOF
> Response.write "mangername = " & dbRS.Fields("managername") & "
> empname = " & dbRS.Fields("empname") & "<br>"
> ' <-- Your recursive call. Walks down each tree branch until
> ' it hits the last reply in a tree (dbRS returns no rows), then backs
> out.
> GetReplies(dbRS.Fields("empname"))
> dbRS.MoveNext
> Loop
> End Function
> %>
>
>
>



Relevant Pages

  • Re: Any use for recursion?
    ... better done as a recursive function? ... ridiculous overhead in terms of execution speed and stack usage. ... that putchar is routed into a communications link that's running at 1 ... try writing that in a loop and making it easy to understand. ...
    (comp.programming)
  • Re: Recursive Functions
    ... requiring it as a recursive function does scream of homework. ... >> simple for loop should be easier and faster. ... > iterative version that can hold a candle to the recursive technique? ... multiplications are possible using the square and multiply idiom. ...
    (comp.lang.c)
  • Re: Iteration in lisp
    ... In Common Lisp, always prefer a loop where practical unless you know ... recursive function can run out of stack since CL does not guarantee ... Even if you have tail recursion, you are still wasting productivity by ...
    (comp.lang.lisp)
  • Re: Iteration in lisp
    ... If the recursive function defines a iterative process, it might be as good as a loop. ... If the loop defines a linear recursive process, it might be as bad as the recursive function. ...
    (comp.lang.lisp)
  • Re: Copying file down from web to a local directory
    ... > Okay, n00b question here. ... > This code errors out with: ... > 4) Microsoft VBScript runtime error: ...
    (microsoft.public.scripting.vbscript)