I have delete and append functions that build Table1 based on inputs from the user. Therefore Table1 has a different number of records appending to it for every user.

My SQL code works to find the dates, but it only does it once, I need to loop the SQL code for the length of the table. I'm not great at coding, I tried a while statement, not sure if I can use variable Z in the criteria for that, but I want it to run until the due\_date in the record with the smallest ID value has been filled.

Here's what I tried:

```vb
Private Sub Command7_Click()
Y = DMax("ID", "Table1", BuildCriteria("Due_date", dbDate, "Null"))
A = DMin("ID", "Table1", BuildCriteria("Due_date", dbDate, "Not Null"))
X = DMin("ID", "Table1")
Z = DLookup("Due_date", "Table1", BuildCriteria("ID", dbLong, CStr(X)))

B = DLookup("Duration", "Table1", BuildCriteria("ID", dbLong, CStr(Y)))
C = DLookup("Due_date", "Table1", BuildCriteria("ID", dbLong, CStr(A)))
E = DateAdd("d", -B, C)

Dim SQL As String

SQL = "UPDATE Table1 " & _
    "SET " & BuildCriteria("Due_date", dbDate, CStr(E)) & " " & _
    "WHERE " & BuildCriteria("ID", dbLong, CStr(Y))

While Z Is Null
DoCmd.RunSQL SQL

End While

End Sub

```

To illustrate:

* Before Running SQL

[![Before Running SQL](https://i.stack.imgur.com/2ZtKy.png)](https://i.stack.imgur.com/2ZtKy.png)
* After running SQL once

[![After running SQL once](https://i.stack.imgur.com/EoX8F.png)](https://i.stack.imgur.com/EoX8F.png)
* After clicking several times

[![After clicking several times](https://i.stack.imgur.com/02RyQ.png)](https://i.stack.imgur.com/02RyQ.png)

The goal would be to click once and the whole table fills