How to write a logical loop

GBalcom

Much to learn!
Local time
Today, 13:45
Joined
Jun 7, 2012
Messages
460
I have a database that I need to read for my append query. One of the criteria I need to filter for is if the direct record has been ignored...this was easy...However, they can also be indirectly ignored. This is a hierarchical tree in which I may be looking at 3 or 4 tiers down. If a parent above that record has been ignored, I should filter that record out.....

Please see attached for a snippet of the datasheet view of the table.

The logic of the criteria appears to be:
"If I am ignored, then ignore. Otherwise, if my parent is ignored, then ignore. Otherwise if my parent's parent is ignored, then ignore...etc until the ParentRecNo = 0"

Could this be done with a Loop function in VBA?
Or with some sort of Macro?

Thanks,
Gary
 

Attachments

  • Loop for Parent Ignore.jpg
    Loop for Parent Ignore.jpg
    100.6 KB · Views: 193
As it turns out, while I was looking at it this morning, I found a much simpler solution....There is a column already populated in the database called "aggregate Qty" that takes into account whether or not the record has a parent that is ignored...I can simply reference this field (if it is not null, I'll include the record).....

Sometimes I forget to look for the simplest solutions first!
 
ok, I'm back on this for another issue....same table....I need a summary of all of labor operations for each summary node of the tree....This seems to be the only way to accomplish that.

I found some VBA code that I think may help if I knew how to implement it. here's the code:

Public Function JobRoot(Id As Long, ParentId As Long) As Long
If ParentId = 0 Then
JobRoot = Id
Exit Function
End If

Dim Rst As New ADODB.Recordset
Dim sql As String
sql = "SELECT Id, ParentID FROM JobTable WHERE Id = " & ParentId & ";"
Rst.Open sql, CurrentProject.Connection, adOpenKeyset, adLockReadOnly

If Rst.Fields("ParentID") = 0 Then
JobRoot = Rst.Fields("Id")
Else
JobRoot = JobRoot(Id, Rst.Fields("ParentID")) ' Recursive.
End If

Rst.Close
Set Rst = Nothing
End Function
 

Users who are viewing this thread

Back
Top Bottom