I have a situation where I need to renumber a field occasionally. It is a prize list for awards which needs added to and occasionally items deleted from. I do not have a good understanding of code but managed to find the below code and have adapted it to suit the Field I am trying to renumber. I have entered it as a module and run it 'on click' from a macro on the Form where I make the alterations. I have been told this is not a normal procedure for Access but is what I need to do. The code is giving me "updating the Special numbers has Failed.
Function UpdateSpecial() As Boolean
Dim db As Database
Dim LSQL As String
Dim Lrs As DAO.Recordset
Dim LUpdate As String
Dim LSpecial As Long
On Error GoTo Err_Execute
'Query user for the starting Special number
LSpecial = InputBox("Please enter starting Special Number.", "Renumber Specials")
Set db = CurrentDb()
'Retrieve each record
LSQL = "select [Special]from DATA"
LSQL = LSQL & " where[Special] <" & LSpecial
LSQL = LSQL & " order by [Special]"
Set Lrs = db.OpenRecordset(LSQL)
Do Until Lrs.EOF
'Renumber Special Numbers
LUpdate = "update DATA"
LUpdate = LUpdate & " set[Special] = " & LSpecial
LUpdate = LUpdate & " where[Special] = " & Lrs("Special")
db.Execute LUpdate, dbFailOnError
'Increment Ring Number
LSpecial = LSpecial + 1
Lrs.MoveNext
Loop
Set Lrs = Nothing
Set db = Nothing
MsgBox "Renumbering the Special Numbers has successfully completed."
UpdateSpecial = True
On Error GoTo 0
Exit Function
Err_Execute:
MsgBox "Updating the Special Numbers failed."
UpdateSpecial = False
End Function
Can anyone point out what is wrong? Thanks
Function UpdateSpecial() As Boolean
Dim db As Database
Dim LSQL As String
Dim Lrs As DAO.Recordset
Dim LUpdate As String
Dim LSpecial As Long
On Error GoTo Err_Execute
'Query user for the starting Special number
LSpecial = InputBox("Please enter starting Special Number.", "Renumber Specials")
Set db = CurrentDb()
'Retrieve each record
LSQL = "select [Special]from DATA"
LSQL = LSQL & " where[Special] <" & LSpecial
LSQL = LSQL & " order by [Special]"
Set Lrs = db.OpenRecordset(LSQL)
Do Until Lrs.EOF
'Renumber Special Numbers
LUpdate = "update DATA"
LUpdate = LUpdate & " set[Special] = " & LSpecial
LUpdate = LUpdate & " where[Special] = " & Lrs("Special")
db.Execute LUpdate, dbFailOnError
'Increment Ring Number
LSpecial = LSpecial + 1
Lrs.MoveNext
Loop
Set Lrs = Nothing
Set db = Nothing
MsgBox "Renumbering the Special Numbers has successfully completed."
UpdateSpecial = True
On Error GoTo 0
Exit Function
Err_Execute:
MsgBox "Updating the Special Numbers failed."
UpdateSpecial = False
End Function
Can anyone point out what is wrong? Thanks