• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Tree query assistance in Access 2003

Hi,

The following is a simplified view of an Entity structure with an applicaiton.

EntityID
EntityName
EntityStatus
FEntityID

FEntityID references itself which creates the tree.  The status can be active, inactive, potential.
Lets call the relationship between a group of related entities an Entity Group.
i.e.

EntityID: 1
EntityName: TopCompany
EntityStatus: Inactive
FEntityID: null

EntityID: 2
EntityName: MiddleCompany
EntityStatus: Inactive
FEntityID: 1

EntityID: 3
EntityName: BottomCompany
EntityStatus: Active
FEntityID: 2

Lets call the above group of records an entity group as they are all related. There can be several entity groups within this table.

The rule is that if the user wants to see all Active entities then if at least one entity within an entity group is active then the rest of the entity group should be displayed.

I'm trying to do this using a SQL query but my brain is failing and I require some assistance from the experts.
0
geraintcollins
Asked:
geraintcollins
  • 5
  • 3
1 Solution
 
rockiroadsCommented:
Do you have 3 at most?

Maybe query the same table again but using aliases

eg

select t1.entityname, t2.entityname, t3.entityname
from mytable as t1, mytable as t2, mytable as t3
where t1.entityid = t2.fentityid
and t2.entityid = t3.fentityid
and t1.entitystatus = Active

Not sure what type your entitystatus is, above is just basd on what you said, you need to change it to your values.

0
 
geraintcollinsAuthor Commented:
Thanks rockiroads, I should have explained that the number of levels can be infinite and that the active status can be at any level. If one or more entities in an entity group is active then the rest of the entity group should be displayed if no entities are active in a group then the entire goup should not be displayed.  Ideally I'd like the results returned in the same format as the Entity Structure so that I can iterate through them for display purposes (I'm using a treeview).

 
0
 
rockiroadsCommented:
Wouldnt bother with just a query then, looks like you need a recursive function in vba. Basically you iterate thru the recordset, then call that function again passing in the parent key

Do you know how to populate the treeview control?

I dont have time right now, but can give you some basic code later. At the moment, Im busy at work.
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
geraintcollinsAuthor Commented:
I'm ok with populating the treeview but thanks for offering to help with that.

I was thinking along the same lines with the query but I'm conscious of having performance issues as this is likely to become a very large table.  Can you help me write an efficient VBA function?  I was thinking that identifying the logical entity groups would be the best place to start.
0
 
geraintcollinsAuthor Commented:
I think I've got it by identifying the groups in a Grouping query by introducing a derived field (see code) where group = the parent EntityID of the group:

SELECT tbl_Entity.EntityID, tbl_Entity.EntityName, tbl_Entity.FEntityID, tbl_Entity.Status, getGroup([EntityID]) AS [Group]
FROM tbl_Entity;

and then querying the grouping query and inserting the criteria e.g.

SELECT EntityID, EntityName, FEntityID, Status, Group
FROM Grouping
WHERE Group=(SELECT Group FROM Grouping WHERE Status ='Active')

I've applied this logic to a simple model with limited entities, I'll plug it into the main application and see how it performs.



Public Function GetGroup(EntityID) As Integer
 
    Dim iFEntityID As Integer
    
    iFEntityID = EntityID
    
    While Nz(iFEntityID, 0) <> 0
        iFEntityID = DLookup("FEntityID", "tbl_Entity", "EntityID = " & EntityID)
        If Nz(iFEntityID, 0) <> 0 Then EntityID = iFEntityID
    Wend
    
    GetGroup = EntityID
    
End Function

Open in new window

0
 
rockiroadsCommented:
Sorry for the delay, problems with lappy, using a temp one at the moment

What I was talking about regarding recursion, you call yourself

eg


Public Sub PopulateTree(Optional ByVal lID As Long = 0)

    Dim rs As DAO.Recordset
    Dim sSql As String

    sSql = "select * from tbl_entity"
    If lID > 0 Then sSql = sSql & " where lentityid=" & lID
    Set rs = CurrentDb.OpenRecordset(sSql)
    Do While rs.EOF = False

        'ADD TO TREE HERE
        Debug.Print lID, rs!FName

        If Val(Nz(rs!EntityID, 0)) <> 0 Then PopulateTree rs!EntityID
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
End Sub



Basically go through each record that matches the id passed in (default is 0 which is topmost record) and add to tree (in case above you are writing to immediate window)

For each record you read, check to see if it has any records under it by rerunning that same procedure passing in that entityid. That procudure checks lentityid when creating recordsource
eg

you have two topmost records A and B
A has kids A1, A2, A3
A1 has 1 kids A1a
A2 has 2 kids A2a. A2b
A3 has none

B has none

you should get records appearing like this

A -> A1 -> A1a -> A2 -> A2a, A2b -> A3 -> B
0
 
geraintcollinsAuthor Commented:
Thanks for your help on this one.  My post prior to yours did the trick.  The only change I needed was to remove the WHERE Group=(SELECT Group FROM Grouping WHERE Status ='Active') statement and add a join to the grouping table.  It works like a dream.   The tree population is a little more complex as other items such as contacts and programs of work are included but its all taken care of.
0
 
geraintcollinsAuthor Commented:
The format of the results had to be in the order requested due to the complexity of the tree population.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now