Dave,
Here is a mockup to insert a new record in the middle of an ordered list. Just 1 way, but there are others.
Code:
----------------------------------------------------------------
' Procedure Name: DaveDelivery
' Purpose: Routine to insert a new record into an established list of delivery numbers
' Must increase the existing delivery numbers after the position of the new record,
' allowing for insertion of a new record at the known position in the list
' In the sample there are 6 records, need to insert a new record 4,
'so in original 4 becomes 5, 5 becomes 6 , 6 becomes 7, 1 2 an3 don't change and a new 4 is inserted
'
' Procedure Kind: Sub
' Procedure Access: Public
' Author: Jack
' Date: 03-Aug-23
' ------------------------MOCK UP FOR------------------------------
' ----------https://www.accessforums.net/showthread.php?t=88489&p=514877#post514877-----------------
Sub DaveDelivery()
10 On Error GoTo DaveDelivery_Error
Dim newNum As Integer
20 newNum = 4
Dim db As DAO.Database
Dim rs As DAO.Recordset
30 Set db = CurrentDb
Dim i As Integer
Dim recCount As Integer
'NOTE Only DelNo is in the Select for rs
40 Set rs = db.OpenRecordset("select delno from tblDave order by delno")
50 rs.MoveLast
60 recCount = rs.RecordCount
' starting with last record in the recordset
' adjust the DelNo
70 For i = recCount To newNum Step -1
80 rs.Edit
90 rs!DelNo = rs!DelNo + 1
100 Debug.Print i; rs!DelNo
110 rs.Update
120 rs.Move -1
130 Next i
'DelNo s have been adjusted to allow insert of new record
'insert the new record .... only explicitly insert DelNo
140 rs.AddNew
150 rs!DelNo = newNum
170 rs.Update
180 On Error GoTo 0
DaveDelivery_Exit:
190 Exit Sub
DaveDelivery_Error:
200 MsgBox "Error " & Err.Number & " (" & Err.Description & "), line " & Erl & " in Procedure DaveDelivery" _
& " Module ZZ_ScratchPad "
210 GoTo DaveDelivery_Exit
End Sub
Simpler, sql approach:
1: Update tblDave
set delNo = delNo +1 where delno >= 4;
2: insert into tblDave (delno, otherinfo)
values (4,"newDeliveryRecord")