Hi, I'm struggling with structuring the order of my IF and LOOP statements.
My database is supposed to generate samples (records) per soil boring. For every Boring, add samples to the Samples table. The top half of every boring will start with Continuous samples (Set1) first, then bottom half is Every Other samples (Set2). The loops are the same except for the increment. Some borings have samples already added, and some have none. First loop is to make sure all the borings have all the Set1 samples, and then second loop is to add the Set2 samples.
So basically, the code should be something like:
All borings need to go through both loops. Here is my code. I had it working beautifully, except I realized I didn't code for borings with zero samples. So I tried to add it and that has messed me up. I moved some code around but got confused and gave up, so I know some of it is out of order. I know how to fix this by duplicating the loop again for each scenario, but that just seems wasteful and redundant. I just thought there is probably a more efficient way to write this, but I just can't think of it because my brain is fried right now.
rs1 is Borings table with sampling definitions.
rs2 is Samples table.
rs3 is Existing samples if they have been added, to determine deepest sample already added.
My database is supposed to generate samples (records) per soil boring. For every Boring, add samples to the Samples table. The top half of every boring will start with Continuous samples (Set1) first, then bottom half is Every Other samples (Set2). The loops are the same except for the increment. Some borings have samples already added, and some have none. First loop is to make sure all the borings have all the Set1 samples, and then second loop is to add the Set2 samples.
So basically, the code should be something like:
If Boring Has: | Then: | Do: |
---|---|---|
Zero samples | Set counter at 1 | Add Set1 samples, then add Set2 samples |
Some Set1 samples | Set counter at previous sample + 1 | Add Set1 samples, then add Set2 samples |
All Set1 samples | Set counter at previous sample + 1 | Add Set2 samples |
All borings need to go through both loops. Here is my code. I had it working beautifully, except I realized I didn't code for borings with zero samples. So I tried to add it and that has messed me up. I moved some code around but got confused and gave up, so I know some of it is out of order. I know how to fix this by duplicating the loop again for each scenario, but that just seems wasteful and redundant. I just thought there is probably a more efficient way to write this, but I just can't think of it because my brain is fried right now.
rs1 is Borings table with sampling definitions.
rs2 is Samples table.
rs3 is Existing samples if they have been added, to determine deepest sample already added.
Code:
Do Until rs1.EOF
'Checks if boring has any samples.
If rs3.RecordCount = 0 Then
SampleNumber = 1 'Sets the counter for sample numbers.
SampleDepth = 0
'Checks if more Continuous To samples need to be added.
ElseIf rs3!DrilledDepth < rs1![Continuous To] Then
SampleNumber = rs3!LastDrilledSampleNumber + 1 'Sets the counter for sample numbers.
SampleDepth = rs3!LastDrilledSampleDepth + rs1![Sample Length]
'If all Continuous To samples are added, then move to loop for Every Other samples.
'Not sure where this If statement should go
ElseIf rs3!DrilledDepth >= rs1![Continuous To] Then
'Sets the depth counter to Every Other increment.
SampleDepth = rs3!LastDrilledSampleDepth + rs1![Every Other]
End If
'Loop for Continuous To samples. This loop populates the samples to the defined Continuous To depth.
Do
rs2.AddNew 'Begins new record (add mode).
rs2!BoringID = rs1!BoringID 'Sets BoringID for new sample to current BoringID.
rs2!Number = SampleNumber 'Sets sample number.
rs2!Depth = SampleDepth 'Sets sample depth.
rs2!Length = rs1![Sample Length] 'Sets sample length to specified sample length.
rs2.Update 'Saves sample record.
rs3.Requery
'Increments sample number.
SampleNumber = SampleNumber + 1
'Checks to see if all samples have been added up to specified Continuous To depth.
If rs3!DrilledDepth >= rs1![Continuous To] Then Exit Do
'Increments sample depth.
SampleDepth = SampleDepth + rs1![Sample Length]
'End of Continuous To loop.
Loop
End If
'If all Continuous To samples are added, then move to loop for Every Other samples.
If rs3!DrilledDepth >= rs1![Continuous To] Then
'Sets the depth counter to specified Every Other increment.
SampleDepth = rs3!LastDrilledSampleDepth + rs1![Every Other]
'Loop for Every Other samples. This loop populates the samples for the specified Every Other depth.
Do
rs2.AddNew 'Begins new record (add mode).
rs2!BoringID = rs1!BoringID 'Sets BoringID for new sample to current BoringID.
rs2!Number = SampleNumber 'Sets sample number starting at 1.
rs2!Depth = SampleDepth 'Sets sample depth starting at 0.
rs2!Length = rs1![Sample Length] 'Sets sample length to specified sample length.
rs2.Update 'Saves sample record.
rs3.Requery
'Increments sample number.
SampleNumber = SampleNumber + 1
'Checks to see if all samples have been added.
If rs3!DrilledDepth >= rs1!HoleDepth Then Exit Do
'End of Every Other loop.
Loop
'End of appending samples per boring.
End If
rs1.MoveNext 'Moves to the next boring for adding samples.
rs3.Requery
'End of adding samples for all borings (EOF).
Loop
Last edited: