Not sure if this belonged in the Query or Module or Programming forum.
I have a table (tblPriorityTest) with 3 fields. ID, Description, Priority. Priorities are to range between 1-9000.
I have a form where I have each field named to the field it represents.
In the before update event i have.
Code:
Private Sub Priority_BeforeUpdate(Cancel As Integer)
PriorityUpdate Me.Priority
End Sub
And the Priority Update Code.
Code:
Private Function PriorityUpdate(ByVal tbPriority As TextBox)
Dim oldValue As Integer: oldValue = tbPriority.oldValue
Dim newValue As Integer: newValue = tbPriority
Dim SQL As String
'Dim intChange As Integer: intChange = 0
If oldValue = 0 Or newValue = 0 Or oldvale = newValue Then Exit Function
If newValue < oldValue Then
intChange = oldValue - newValue
SQL = "Update tblPriorityTest Set [Priority]=[Priority]-" & IIf(intChange <= 0, Abs(intChange), intChange) & " Where [Priority]<" & oldValue
SQL = SQL & " and ID <>" & id & ""
SQL = SQL & " and [Priority]<9000"
ElseIf newValue > oldValue Then
intChange = newValue - oldValue
SQL = "Update tblPriorityTest Set [Priority]=[Priority]+" & intChange & " Where [Priority]>" & oldValue
SQL = SQL & " and ID <>" & id & ""
SQL = SQL & " and [Priority]<9000"
End If
Debug.Print SQL
CurrentDb.Execute SQL
'intChange = CurrentDb.RecordsAffected
End Function
my data look like
id: |
Description: |
Priority: |
1 |
1 |
1 |
2 |
2 |
2 |
3 |
3 |
3 |
4 |
4 |
4 |
5 |
5 |
5 |
6 |
6 |
6 |
7 |
7 |
7 |
When I change the priority of ID#5 to a 7. I want all the numbers after it to be changed.
It sorta works. sorta not. Am I doing this the wrong way? Suggestions.