Automatic numbering via VBA module numbers out of sequence 
Author Message
 Automatic numbering via VBA module numbers out of sequence

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



Sun, 23 Jun 2002 03:00:00 GMT  
 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



Sun, 23 Jun 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. problem updating page numbers after opening a file (all via VBA)

2. Copy and paste list numbering via VBA code.

3. Finding numbers missing in a sequence

4. Tricky problem re: returning an item sequence number to a form and to a table

5. Sequence Number Reset

6. auto capitalize first word in numbered sequence?

7. Document Properties (sequence numbers)

8. Oracle Sequence Numbers

9. Finding Pairs, Doubles Triples in number sequence

10. Random Number Sequence

11. Random Number Sequence

12. Algorithm for finding biggest numbers in sequence

 

 
Powered by phpBB® Forum Software