This code helps solving the serial number problem in a multi user environment, where several users are attempting to take the same number.
It depends on keeping the number in a separate table and then opening it one at a time with the "deny read" permisson.
One User will be allowed in while others will be locked out until he is done.
Public Function GetInvoiceNumber As Long
On Error GoTo Error_Handler
Dim LockCount As Integer, wait As Long, waitx As Long
Dim wrk As Workspace, mydb As DataBase, numTable As Recordset
DoCmd.Hourglass True
'Open the table called Autonumber in deny read mode.
Set wrk = DBEngine.Workspaces(0)
Set mydb = wrk.OpenDatabase("your database here")
Set numTable = mydb.OpenRecordset("AutoNumber", dbOpenTable, dbDenyRead)
LockCount = 0
'Read value and increment.
numTable.MoveFirst
numTable.Edit
GetInvoiceNumber = numTable!InvoiceNo
numTable!InvoiceNo = numTable!InvoiceNo + 1
numTable.Update
numTable.Close
mydb.Close
GetInvoiceNumber_Exit:
DoCmd.Hourglass False
Exit Function
Error_Handler:
Select Case Err
'list of errors that might occur due to locking issues
Case 3006, 3008, 3009, 3027, 3045, 3046, 3158, 3164, 3186, _
3187, 3188, 3189, 3197, 3202, 3211, 3212, 3218, 3260, 3261, 3262
LockCount = LockCount + 1
If LockCount > 10 Then
GetInvoiceNumber = -1
Resume GetInvoiceNumber_Exit
Else
Pause (100000) ' Pause is a wait loop defined somewhere else
Resume
End If
Case Else
'another kind of error
MsgBox "Error" & Err.number & Err.Description, vbCritical + vbOKOnly
GetInvoiceNumber = -1
Resume GetInvoiceNumber_Exit
End Select
End Function