Answered by:
VBA for Task Dependencies

Question
-
Say a task has two dependency chains associated with it. For instance, suppose one chain goes from task 2 to 4 to 8 to 10 and another chain goes from 2 to 5 to 9. When looping through the task dependencies object recursively, is there some way to detect when the links shift from one chain to another?
I'm using the approach outlined by Rod Gill on pp 335-6 of his fine book. (See below.) The approach does not seem to differentiate between the two chains--Flag20 is marked for both chains at the same time. I need to process one chain, then the other.
Public Sub DepSucc(Tsk As Task)
Dim Dep As TaskDependencyIf Tsk.Flag20 = False Then
Tsk.Flag20 = True
For Each Dep In Tsk.TaskDependencies
If (Dep.To.ID <> Tsk.ID) Then
DepSucc Dep.To
End If
Next
End IfEnd Sub
RobVV
Tuesday, April 8, 2014 11:20 PM
Answers
-
The first time you loop through all dependencies for the selected task you need to set in a variable what field to update. So you need a wrapper sub around what you have, something like (not tested)
Public Sub DepFirstSucc(Tsk As Task) Dim Dep As TaskDependency
Dim Flagnum as Long
FlagNum = FlagNum If Tsk.GetField(FlagNum) = False Then Tsk.SetField FlagNum, True For Each Dep In Tsk.TaskDependencies If (Dep.To.ID <> Tsk.ID) Then DepSucc Dep.To, FlagNum FlagNum = FlagNum - 1 End If Next End If End Sub Public Sub DepSucc(Tsk As Task, FlagNum as Long) Dim Dep As TaskDependency FlagNum = FlagNum If Tsk.GetField(FlagNum) = False Then Tsk.SetField FlagNum, True For Each Dep In Tsk.TaskDependencies If (Dep.To.ID <> Tsk.ID) Then DepSucc Dep.To, Flagnum End If Next End If End SubNote Flag20 is last flag, so I decrement the Flagnum field to use Flgag19 etc.
Rod Gill
Author of the one and only Project VBA Book
www.project-systems.co.nz- Marked as answer by RobVV Thursday, April 10, 2014 1:41 PM
Wednesday, April 9, 2014 9:13 PM
All replies
-
Talk of the devil!
What are you trying to achieve? What was the originally selected task? If it was 2, then all the tasks should end up flagged as they are all successors of task 2
Rod Gill
Author of the one and only Project VBA Book
www.project-systems.co.nzWednesday, April 9, 2014 8:03 AM -
Hi, Rod. It's great to have a response from the maestro. What I'm trying to do is identify each dependency chain individually so that I can do a calculation on each chain. Starting at task 2, there are two chains in the example: 2-4-8-10 and 2-5-9. When I use the recursive code, Flag20 is marked for 2, 4, 8, 10, 5, and then 9. I was hoping that there was some way within the recursive loop to determine when the first chain stopped and the second chain started. If so, I could take a different action, e.g., mark Flag21, for the second chain.
RobVV
Wednesday, April 9, 2014 1:57 PM -
The first time you loop through all dependencies for the selected task you need to set in a variable what field to update. So you need a wrapper sub around what you have, something like (not tested)
Public Sub DepFirstSucc(Tsk As Task) Dim Dep As TaskDependency
Dim Flagnum as Long
FlagNum = FlagNum If Tsk.GetField(FlagNum) = False Then Tsk.SetField FlagNum, True For Each Dep In Tsk.TaskDependencies If (Dep.To.ID <> Tsk.ID) Then DepSucc Dep.To, FlagNum FlagNum = FlagNum - 1 End If Next End If End Sub Public Sub DepSucc(Tsk As Task, FlagNum as Long) Dim Dep As TaskDependency FlagNum = FlagNum If Tsk.GetField(FlagNum) = False Then Tsk.SetField FlagNum, True For Each Dep In Tsk.TaskDependencies If (Dep.To.ID <> Tsk.ID) Then DepSucc Dep.To, Flagnum End If Next End If End SubNote Flag20 is last flag, so I decrement the Flagnum field to use Flgag19 etc.
Rod Gill
Author of the one and only Project VBA Book
www.project-systems.co.nz- Marked as answer by RobVV Thursday, April 10, 2014 1:41 PM
Wednesday, April 9, 2014 9:13 PM -
Thanks, Rod. In trying this out, I noticed that Project stores the sequences one-after-the-other in the dependencies object. In other words, as the For-Each-Dep loop executes, it goes through 2-4-8-10 and then 5-9 (it doesn't return to 2). So, I save the maximum Dep.To.Id in a global variable and check each new Dep.To.Id against it. If the new Dep.To.Id is less than the max, I know that I've hit a new path and set the new flag number. The code is below.
For Each Dep In t.TaskDependencies
If (Dep.To.ID <> t.ID) Then
If Dep.To.ID < SeriesMaxID Then 'reset series
SeriesMaxID = Dep.To.ID 'SeriesMaxID is a global variable
Flagnum=Flagnum-1
Else
SeriesMaxID = Dep.To.ID
End If
Call DepSucc(Dep.To, Flagnum)
End If 'new id
NextRobVV
Thursday, April 10, 2014 1:55 PM