Solved

Tree query assistance in Access 2003

Posted on 2009-05-03
8
224 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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
 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

810 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