Solved

Tree query assistance in Access 2003

Posted on 2009-05-03
8
223 Views
Last Modified: 2012-05-06
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
Comment
Question by:geraintcollins
  • 5
  • 3
8 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 24293409
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
 

Author Comment

by:geraintcollins
ID: 24300213
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 24303509
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
 

Author Comment

by:geraintcollins
ID: 24310832
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:geraintcollins
ID: 24311349
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
 
LVL 65

Accepted Solution

by:
rockiroads earned 250 total points
ID: 24342572
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
 

Author Comment

by:geraintcollins
ID: 24347924
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
 

Author Closing Comment

by:geraintcollins
ID: 31579903
The format of the results had to be in the order requested due to the complexity of the tree population.  
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now