Solved

Tree query assistance in Access 2003

Posted on 2009-05-03
8
222 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

13 Experts available now in Live!

Get 1:1 Help Now