Results 1 to 6 of 6
  1. #1
    hansdbase is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    Nov 2022
    Posts
    33

    Create Heirarchy


    Hello, I am trying to create an organizational hierarchy. It is easy enough to create the table that lists all the departments in the company. But I need then to then assign the parent and all children to each of them. And I need to ensure that only one department is at top of the organizational structure, the corporate HQ, such that no one can assign it a parent. I am not sure the best way to do this. Thanks.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    In your table include a parentFK field. This is populated with the PK of the parent record. To indicate the top parent, either leave as null or populate with the PK (I prefer the latter)

    Depends on what you are going to do with the data but you may need to review what is called a recursive function

  3. #3
    hansdbase is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    Nov 2022
    Posts
    33
    Thank you. So I understand that I should create a parentFK field in my table. So my table will now look like this:
    OrganizationID PK
    OrganizationName
    ParentFK

    And I understand that I would populate it with the organizationID of the parent record.

    I like the idea that the top parent would have a parent id of itself. But I guess I would have to add some type of unique filter or constraint so that only one department has itself as the parent because I can only have one top? I am trying to prevent someone for instance making two different departments reporting to themselves because only one, the top department, should do that. Is there an easy way to do that?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    This is a common topic. Look at Similar Threads list at bottom of this page.

    Also review
    https://www.sfmagazine.com/articles/...archical-data/
    https://answers.microsoft.com/en-us/...8-c92c27b7e196
    https://stackoverflow.com/questions/...ional-database

    Access does not handle recursion well. If you want to migrate data to SQLServerExpress, managing this might be easier.

    "Easy" depends on your POV and experience.
    Maybe could use combobox or textbox Validation Rule and Validation Text properties to control the ParentFK input. Or use VBA procedure behind form to validate data entry of the ParentFK input. Or a combobox RowSource that excludes OrganizationID of current record (this would require VBA).
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    But I guess I would have to add some type of unique filter or constraint so that only one department has itself as the parent because I can only have one top?
    yes - easy enough to do - for example a validation rule might be

    if orgpk=parentfk and dcount(“•”,”departments”,”orgpk=parentfk”)>0 then msgbox(“can’t do this”)

  6. #6
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    154
    [QUOTE=June7;522440]This is a common topic. Look at Similar Threads list at bottom of this page.

    Also review
    https://www.sfmagazine.com/articles/...archical-data/
    https://answers.microsoft.com/en-us/...8-c92c27b7e196
    https://stackoverflow.com/questions/...ional-database

    Access does not handle recursion well. If you want to migrate data to SQLServerExpress, managing this might be easier.

    At least querying it. Use a Common Table Expression and make it recursive. (Learning that was super fun!) here's an article: How Recursive Common Table Expressions Work (learnsql.com)

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 03-20-2022, 02:51 PM
  2. Replies: 14
    Last Post: 12-28-2015, 07:51 AM
  3. Another try... Heirarchy of web-based nav forms
    By pschonen in forum SharePoint
    Replies: 0
    Last Post: 02-24-2014, 03:05 PM
  4. Replies: 1
    Last Post: 05-20-2013, 01:45 PM
  5. Find all parts in a heirarchy
    By Spiftacu1ar in forum Queries
    Replies: 1
    Last Post: 08-04-2009, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums