How To Restart A VBA Loop Counter

This is related to an interesting question I received from an individual, and this processing example may help someone.

Basically we are conducting a 10 day clinical study on a group of patients.

We want to store the results of the finding in a database and reset the study counter when a result comes back positive.

So if on “Day 1” the result is “NEG” then we proceed to “Day 2”, and if on “Day 2” the result is “NEG” then we proceed to “Day 3”, and so on for 10 days.

However, if on “Day 9”, the result is “POS”, then we must start the count over from “Day 1”.

This post is going to demonstrate 1 way of coding this.

Here is a shot of the database set up:

…and here is what the form looks like after the “Tally Results” button is clicked:

Notice that whenever the “POS” result is indicated, the test days need to start at 1 again.

Here is the code:

Private Sub btnTallyResults_Click()
    
    'erik@loeblcomservices.com
    '4/15/19
    
    Dim strSQL As String
    Dim rstResult As Recordset
    Dim rstUpdate As Recordset
    
    Dim intCounter As Integer
    
    'set up your main update loop based on the current patient ID.
    Set rstUpdate = CurrentDb.OpenRecordset("tblPatientTesting", dbOpenDynaset)
       
    strSQL = "SELECT ptID,ptPatID,ptResults,ptDay FROM tblPatientTesting WHERE ptPatID = " & Me.patID & " ORDER BY ptDate"
    Set rstResult = CurrentDb.OpenRecordset(strSQL)
    
    'initialize your counter
    intCounter = 1
        
    'loop the sub data records
    Do Until rstResult.EOF
               
        'update the ptDay based on the value in the ptResults field
        If rstResult.Fields("ptResults") = "POS" Then
            
            'reset the counter
            intCounter = 1
            
            'search for the current record to update
            rstUpdate.FindFirst "[ptID]=" & rstResult("ptID")
            
            'restart the counter and update the tally count
            rstUpdate.Edit
            rstUpdate.Fields("ptDay") = "Day " & intCounter
            rstUpdate.Update
            
        Else
                    
            'search for the current record to update
            rstUpdate.FindFirst "[ptID]=" & rstResult("ptID")
            
            'update the tally count
            rstUpdate.Edit
            rstUpdate.Fields("ptDay") = "Day " & intCounter
            rstUpdate.Update
                       
        End If
        
        'increment the counter
        intCounter = intCounter + 1
        
        rstResult.MoveNext
    Loop
    
    'show the updates on the screen
    DoCmd.RunCommand acCmdRefresh
    
    rstResult.Close
    Set rstResult = Nothing
    
    rstUpdate.Close
    Set rstUpdate = Nothing
End Sub

Watch how I do it:

Let me know if you have questions regarding this.

center>




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Generate A XML File With Access VBA

XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]

Mouse Click Counter On Access Form

This post will demonstrate how you can count the number of clicks on your button in a certain time frame. It will function like a game . Here is the database form all in one: Option Compare Database Public m_dteStartTime As Date Public m_dteStopTime As Date Private Sub btnClicker_Click() Dim intValue As Integer Dim rst […]

Previous Post

How To Declare A Variable With VBA Dim

Next Post

Docmd.OpenForm With Multiple Parameters