Here is the full code with the suggested changes and comments to explain.
I've added about 5 more lines of code to make this subroutine the heart of a simple "Do List database"
Only one table, tTasks with fields: Task, Priority, ActionDate, TaskID
In this subroutine:
. Tasks get sorted by priority and then re-allocated priorities 100, 110, 120 etc
. Tasks = "xx" get priority 999999
It is a short, useful example of DAO code
Thanks for your help.
Code:
Private Sub btnresetPriorities_Click()
On Error GoTo Err_btnresetPriorities_Click
Dim dbsDoList As DAO.Database
' DAO.database user-defined type not defined
' Fix with Tools > References > Tick Microsoft DAO 3.6 object library
' I un-ticked Microsoft ActiveX Data Objects 2.1 library
' Access 2010 32 bit
Dim rsTasks As DAO.Recordset
Dim strSQL As String
Dim NewPriority As Integer
NewPriority = 100 ' Set first priority
Set dbsDoList = CurrentDb
' Open a recordset for all records from tTasks
' tTasks Fields: Task, Priority, ActionDate
strSQL = "SELECT Task, Priority FROM tTasks ORDER BY Priority"
Set rsTasks = dbsDoList.OpenRecordset(strSQL, dbOpenDynaset)
With rsTasks
Do Until .EOF
If ![Task] = "xx" Then
.Edit
![Priority] = 999999
.Update
Else
.Edit
![Priority] = NewPriority
.Update
NewPriority = NewPriority + 10
End If
.MoveNext
Loop ' Do Until Loop
End With ' rsTasks
Exit_btnresetPriorities_Click:
rsTasks.Close
Set rsTasks = Nothing
' Set rsTasks = dbsDoList.OpenRecordset(strSQL, dbOpenDynaset)
' This line creates / declares rsTasks and opens it
' So at end, (1) close it and then (2) set it to nothing = destroy it
' Set dbsDoList = CurrentDb
' This line creates dbsDoList, so at end I set it to nothing = destroy it
' I did not open it, so I do not close it
' dbsDoList.Close
Set dbsDoList = Nothing
Exit Sub
Err_btnresetPriorities_Click:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
' vbCrLf = visual basic carriage return line feed
Resume Exit_btnresetPriorities_Click
End Sub