Is it possible to create a recursive query in Access?

前端 未结 7 1997
有刺的猬
有刺的猬 2020-11-27 19:12

I have a job table

Id
ParentID
jobName
jobStatus

The root ParentID is 0.

Is it possible in Access to create a query to

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 19:49

    I had a related problem working with a treeview structure, when a user wants to delete a node, he wants to delete all then nodes under that tree. Childs, childs of childs etc etc.

    A job for recursion....

    So to delete the data in your table to match the treeview node deletes, use a recursive function that will delete the node, and recurse down and delete all the nodes that are children, grandchildren ect.

    example of the function:

    Public Sub RemoveChildKeys(MyKey)
    ' deletes passed key and removes all children and grandchildren ect etc of passed key recursively
    
       Dim TheDB As DAO.Database
       Dim TheTable As DAO.Recordset
       Dim MySql As String
    
       Set TheDB = CurrentDb
    
       MySql = "Select * from TblIndex WHERE [Parent]=" & MyKey & ";"
       Set TheTable = TheDB.OpenRecordset(MySql)
    
       While Not TheTable.EOF
         RemoveChildKeys (TheTable!Key)  ' <---- Calls itself
         TheTable.MoveNext
       Wend
    
       DoCmd.RunSQL "Delete * FROM TblIndex WHERE [Key]=" & MyKey  ' delete in table
    
    End Sub
    

提交回复
热议问题