
Automatic numbering via VBA module numbers out of sequence
Colette,
You seem to be using the recordcount + 443 as your recordID.
So.. If a previous record was deleted... you'd be reusing the number of the "last" record!
I would think it better to have a table with the next available recordID and never repeat.
tblSysID, One field ID, Number, Long
Then use a public function to return the next ID (and store the next one). This code uses Lockedits to keep someone else from grabbing a number before your code stores the next ID.
Steve
Load the table with the initial value 444 (443 + 1)
**************************************************************************
Public Function NewID() As Long
'this function is for numeric ID values.
'it uses tblSysID to get and store ID numbers
On Error GoTo ProcErr
Dim rs As Recordset
TryAgain:
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblSysID;")
rs.LockEdits = True
rs.Edit
NewID = rs!ID 'set the return value
'store next ID back in table
rs!ID = NewID + 1
rs.Update
rs.LockEdits = False
rs.Close
Set rs = Nothing
Exit Function
ProcErr:
'Another user picked the exact same time as you to get a recordID, TryAgain!
Select Case Err.Number
Case Is <> 0
Resume TryAgain 'at each error you need to keep returning and trying until success
End Select
End Function
**************************************************************************
Quote:
> We have an Access application which was setup to autonumber by use of the
> following code rather than utilizing the Autonumber feature because they
> wanted the numbering to being with 443. Occasionally, the assigned number
> will duplicate, e.g., this afternoon the next number should have been 5146,
> but instead it assigned 5143. Any ideas as to what might be causing this?
> TIA
> Colette
> Public Function AssignLotNo()
> Dim rst As Recordset, dbf As Database
> Dim Key As Long
> Dim Lastrec As Long
> Dim TEMP_RECORD
> Set dbf = CurrentDb
> Set rst = dbf.OpenRecordset("tblRecvLog")
> rst.MoveLast
> Lastrec = rst.RecordCount
> If Lastrec = -1 Then
> AssignLotNo = 1
> Exit Function
> End If
> TEMP_RECORD = Lastrec + 443
> AssignLotNo = TEMP_RECORD + 1
> 'rst.Edit
> 'rst!LotNo = AssignLotNo
> 'rst.Update
> rst.Close
> End Function