Ahhh I think I know what you mean.


I developed a catering system which had a tree like this

Category Description Parent
1 Food NULL
2 Beverage NULL
3 Meat 1
4 Chicken 3
5 Beef 3
6 Spirits 2
7 Vodka 6
8 Whiskey 6
9 Wine 2
10 Red 9
11 Rose 9
12 White 9


Now what I wanted to do was find out what the true parant category was for reports i.e. All the Beverage that was sold.

So Red Wine has a category of 11 and parent of 9 but the true parent is actually 2.

What I did was to create a category parent table which I would add records on the fly (incase someone moved categories around.

I first would delete everything out of the table

Then create a recordset that had just the category number and the category parent

VB Code:
  1. SELECT PrdCatNo, PrdCatParent FROM tblProductCategories

I would then use the following code to loop through the recordset and find the true parent category.

This I would then insert into the table which made it very easy to group the records the way I required.

VB Code:
  1. Do Until rsnpCategoryNo.ActiveRecordset.EOF
  2.         'Set the search category number to the category number
  3.         'in the recordset
  4.         varSearchCatNo = rsnpCategoryNo.ActiveRecordset.Fields("PrdCatNo").Value
  5.         varParentCategoryNo = rsnpCategoryNo.ActiveRecordset.Fields("PrdCatParent").Value
  6.         Do Until varParentCategoryNo = ""
  7.             'Loop through until the parent category is null
  8.             If Not gedbMain.GetField("SELECT PrdCatNo, PrdCatParent FROM tblProductCategories WHERE PrdCatNo=" & _
  9.                 varSearchCatNo, exOpenStatic, varCategoryNo, varParentCategoryNo) Then
  10.                 Exit Do
  11.             End If
  12.             varSearchCatNo = varParentCategoryNo
  13.         Loop
  14.          
  15.         strSQL = "INSERT INTO" & _
  16.                     " tblTillCategories (TllCatNo, TllCatParent) " & _
  17.                 " VALUES" & _
  18.                     " (" & rsnpCategoryNo.ActiveRecordset.Fields("PrdCatNo").Value & ", " & varCategoryNo & ")"
  19.        
  20.         If Not gedbMain.ExecSQL(strSQL, False) Then
  21.             gerrError.PopErrorStack
  22.             Exit Sub
  23.         End If
  24.        
  25.         'Move to next record
  26.         If Not rsnpCategoryNo.MoveRecord(exMoveNext) Then
  27.             Exit Do
  28.         End If
  29.     Loop

There maybe an easier way of doing this but I know this works and isn't that intensive.

Hope this is of use Ryan