Results 1 to 6 of 6

Thread: Table algorithm required... complex spec...

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    Question Table algorithm required... complex spec...

    I have a database table that contains a list of categories. The table schema is:

    catID
    catText
    superCatID

    Where catID is an AutoNumber field, and superCatID points to the parent of a category (this may be null, for a top level category)


    And a possible table might be:
    Code:
    catID   catText   superCatID
    1       A        
    2       B
    3       C
    4       AA        1
    5       AB        1
    6       BA        2
    7       BB        2
    8       ABA       5
    9       CA        3
    10      ABAA      8


    I want a function that will parse this table, or whatever, and build a structured list of the categories, to fill a combo. This is to simplify a UI, so that catIDs don't have to be remembered.

    e.g, the above table should come out looking something like:
    Code:
    A        
    -AA
    -AB
    --ABA
    ---ABAA
    B
    -BA
    -BB
    C
    -CA


    Now, who wants to be the genius that solves this one?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Genius????? I already have written a procedure to do just the same for our app. Use recursion, if you know how to use it




    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Yes, I understand recursion...

    However, my brain is blank as to what exactly I need to recurse...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  4. #4
    Hyperactive Member ashay's Avatar
    Join Date
    Feb 2002
    Location
    Mumbai,India
    Posts
    278
    hi,

    i hv created a dynamic hierarchy that way....but my table structure is a bit different.....n i havent used recursion.....

    i hv included 2 more fields into the table called "parentCatIds" and "parentCatNames"

    the table like this
    :
    catID catText superCatID parentCatIDs parentCatNames

    1 A
    2 B
    3 C
    4 AA 1 1 A
    5 AB 1 1 A
    6 BA 2
    7 BB 2
    8 ABA 5 1\\5 A\\AB
    9 CA 3
    10 ABAA 8

    i hv just shown u one example......for
    A
    -AA
    --AB
    -----ABA


    does this makes sense......actually it shud....coz i have implemented in my code.......


    regds,
    ashay
    "If you should die before me, ask if you could bring a friend."
    - Stone Temple Pilots

  5. #5
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    I just showed a French guy how to do this in another thread, so I suggest doing a search on posts by me in the last month...

  6. #6
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Goes something like this:

    The procedure will accept an argument, which is the SuperCat. The procedure exits when its private recordset reaches .EOF. This is the exit point for any recursions too.

    The code will go something like this:

    Code:
    Private Sub Recurse(SuperCat As String)
        Dim rs As RecordSet, strSQL As String
        strSQL = "SELECT * FROM YourTable WHERE SuperCat"
        If SuperCat = "" Then
            'This is only because you are using nulls as the topmost SuperCat
            strSQL = strSQL & " IS NULL"
        Else
            strSQL = strSQL & " = '" & SuperCat & "'"
        End If
        Set rs = YourDB.OpenRecordset(strSQL)
        With rs
            Do While Not .EOF
                'pick up each record and process it
                Recurse(.Fields("CatID")) 'To process any records where 
                                                        'present CatID is a SuperCat
                .MoveNext
            Loop
            .Close
        End With
    End Sub
    Yeah, and pass an empty string to it the first time...
    .
    Last edited by honeybee; Sep 25th, 2002 at 06:50 AM.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width