Hi everybody,
I've been building an access application where we have Entries and Categories among other things.
Each Entry can belong to a Category (Nullable FK Relationship) and Categories can belong to other Categories (multilevel hierarchy).
So, (..over-simplified..) table Entries is like:
ID
EntryTitle
CategoryID (nullable)
and table Categories is like:
ID
ParentID (nullable)
Category
Now, in detail forms bound to the Entries table I'm using a TreeView control to display the Category, the current Entry belongs to (..if any).
But I also need a Continuous Forms type of form to list all the Entries (like an advanced DataSheet). So, I made one and used a Combobox control bound to the CategoryID field to display each entry's category.
Until now everything works great.
The only problem is that the Combobox does not indicate the hierarchical structure of the Categories. It just displays them all in a list.
So, I had the idea to not bind the combo directly to the CategoryID field, but create a custom sql query (built dynamically with vba) for its Row Source property, which creates indentations and also adds the "└" character in front of the (sub) categories names to provide visual feedback of the hierarchical structure of the table Categories.
A sample of such an SQL query would be:
As you can see we have 3 columns returned by this query.
The ID which is the actual Category.ID field (that we will bind to the Entry.CategoryID field),
The CategoryName which is the plain/unaltered CategoryName that we will display in the combobox after a selection has been made,
and finally we have the CategoryTreeName which is used to display the Categories in a hierarchical manner when the combo is dropped down.
We also setup the combo so that it has 3 columns, it's bound column being column 1, and for the column widths "0;0,1;"
The column widths are setup like this so the 1st (ID) column is never shown, the 2nd (CategoryName) column is shown in the combo only after a selection has been made (not when it is dropped down), and finally the 3rd (CategoryTreeName) column is shown only in the dropped down box (not in the actual combo).
We are doing this because we don't want to see for example " └ Category A" in the actual combo. It's better to show just "Category A".
And now the hard part...
What's the recursive procedure in vba that will create this SQL query dynamically for us??
(Also note that we want the combo/tree sorted)
The only working recursive procedure that comes to my mind as a starting point is the one I used to populate the actual TreeView control in the detail form. I grabbed this from MS site (http://support.microsoft.com/kb/209891):
I need a similar recursive procedure to create the sql query to feed in the combo's row source (..remember: sorted).
I think I'm loosing my mind inside the whole recursive way of thinking!..


Any help/info would be much appreciated.
Thanks in advance,
Maria
I've been building an access application where we have Entries and Categories among other things.
Each Entry can belong to a Category (Nullable FK Relationship) and Categories can belong to other Categories (multilevel hierarchy).
So, (..over-simplified..) table Entries is like:
ID
EntryTitle
CategoryID (nullable)
and table Categories is like:
ID
ParentID (nullable)
Category
Now, in detail forms bound to the Entries table I'm using a TreeView control to display the Category, the current Entry belongs to (..if any).
But I also need a Continuous Forms type of form to list all the Entries (like an advanced DataSheet). So, I made one and used a Combobox control bound to the CategoryID field to display each entry's category.
Until now everything works great.
The only problem is that the Combobox does not indicate the hierarchical structure of the Categories. It just displays them all in a list.
So, I had the idea to not bind the combo directly to the CategoryID field, but create a custom sql query (built dynamically with vba) for its Row Source property, which creates indentations and also adds the "└" character in front of the (sub) categories names to provide visual feedback of the hierarchical structure of the table Categories.
A sample of such an SQL query would be:
Code:
SELECT "1" AS ID, "Cat A" AS CategoryName, "Cat A" AS CategoryTreeName FROM Categories UNION
SELECT "2" AS ID, "Cat A1" AS CategoryName, " └ Cat A1" AS CategoryTreeName FROM Categories UNION
SELECT "3" AS ID, "Cat A2" AS CategoryName, " └ Cat A2" AS CategoryTreeName FROM Categories UNION
SELECT "4" AS ID, "Cat A2-1" AS CategoryName, " └ Cat A2-1" AS CategoryTreeName FROM Categories;
The ID which is the actual Category.ID field (that we will bind to the Entry.CategoryID field),
The CategoryName which is the plain/unaltered CategoryName that we will display in the combobox after a selection has been made,
and finally we have the CategoryTreeName which is used to display the Categories in a hierarchical manner when the combo is dropped down.
We also setup the combo so that it has 3 columns, it's bound column being column 1, and for the column widths "0;0,1;"
The column widths are setup like this so the 1st (ID) column is never shown, the 2nd (CategoryName) column is shown in the combo only after a selection has been made (not when it is dropped down), and finally the 3rd (CategoryTreeName) column is shown only in the dropped down box (not in the actual combo).
We are doing this because we don't want to see for example " └ Category A" in the actual combo. It's better to show just "Category A".
And now the hard part...
What's the recursive procedure in vba that will create this SQL query dynamically for us??
(Also note that we want the combo/tree sorted)
The only working recursive procedure that comes to my mind as a starting point is the one I used to populate the actual TreeView control in the detail form. I grabbed this from MS site (http://support.microsoft.com/kb/209891):
Code:
'=================Load Event for the Form=======================
'Initiates the routine to fill the TreeView control
'============================================================
Private Sub Form_Load()
Const strTableQueryName = "Categories"
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(strTableQueryName, dbOpenDynaset, dbReadOnly)
AddBranch rst:=rst, strPointerField:="ParentID", strIDField:="ID", strTextField:="Category"
End Sub
'================= AddBranch Sub Procedure ======================
' Recursive Procedure to add branches to TreeView Control
'Requires:
' ActiveX Control: TreeView Control
' Name: xTree
'Parameters:
' rst: Self-referencing Recordset containing the data
' strPointerField: Name of field pointing to parent's primary key
' strIDField: Name of parent's primary key field
' strTextField: Name of field containing text to be displayed
'=============================================================
Sub AddBranch(rst As Recordset, strPointerField As String, _
strIDField As String, strTextField As String, _
Optional varReportToID As Variant)
On Error GoTo errAddBranch
Dim nodCurrent As Node, objTree As TreeView
Dim strCriteria As String, strText As String, strKey As String
Dim nodParent As Node, bk As String
Set objTree = Me!xTree.Object
If IsMissing(varReportToID) Then ' Root Branch.
strCriteria = strPointerField & " Is Null"
Else ' Search for records pointing to parent.
strCriteria = BuildCriteria(strPointerField, _
rst.Fields(strPointerField).Type, "=" & varReportToID)
Set nodParent = objTree.Nodes("a" & varReportToID)
End If
' Find the first emp to report to the boss node.
rst.FindFirst strCriteria
Do Until rst.NoMatch
' Create a string with LastName.
strText = rst(strTextField)
strKey = "a" & rst(strIDField)
If Not IsMissing(varReportToID) Then 'add new node to the parent
Set nodCurrent = objTree.Nodes.Add(nodParent, tvwChild, strKey, strText)
Else ' Add new node to the root.
Set nodCurrent = objTree.Nodes.Add(, , strKey, strText)
End If
' Save your place in the recordset so we can pass by ref for speed.
bk = rst.Bookmark
' Add employees who report to this node.
AddBranch rst, strPointerField, strIDField, strTextField, rst(strIDField)
rst.Bookmark = bk ' Return to last place and continue search.
rst.FindNext strCriteria ' Find next employee.
Loop
exitAddBranch:
Exit Sub
'--------------------------Error Trapping --------------------------
errAddBranch:
MsgBox "Can't add child: " & Err.Description, vbCritical, "AddBranch Error:"
Resume exitAddBranch
End Sub
I think I'm loosing my mind inside the whole recursive way of thinking!..



Any help/info would be much appreciated.
Thanks in advance,
Maria