Public Function GetSaleNo() As Long
Dim SN As DAO.Recordset
Dim TM As Double
Dim StartAt As Date
StartAt = Now
On Error GoTo CantLock
Restart:
Set SN = dbControl.OpenRecordset("SALENUMB", dbOpenTable, dbDenyWrite Or dbDenyRead)
With SN
If .EOF Then
GetSaleNo = 1
.AddNew
![Sale_No] = 2
.Update
Else
GetSaleNo = ![Sale_No]
.Edit
![Sale_No] = GetSaleNo + 1
.Update
End If
.Close
End With
Set SN = Nothing
Exit Function
CantLock:
Select Case Err.Number
Case 3008, 3009, 3187, 3189, 3211, 3212, 3262
' Errors mean that exclusive use of the recordset did not work.
' Wait for half a second and then try again.
' 3008 - The table <name> is already opened exclusively by another user
' 3009 - You tried to lock table <table> while opening it, but the table can't be locked because it is currently in use.
' 3187 - Couldn't read; currently locked by user <name> on machine <name>.
' 3189 - Table <name> is exclusively locked by user <name> on machine <name>.
' 3211 - couldn't lock table <name> because it's already in use by another person or process.
' 3212 - Couldn't lock table <name>; currently in use by user <name> on machine <name>.
' 3262 - Couldn't lock table <name>; currently in use by user <name> on machine <name>.
TM = Timer + 0.5
Do Until Timer >= TM
Loop
If DateDiff("s", StartAt, Now) > 10 Then
Dim Ans As Integer
Ans = TimedMessageBox(Err.Description & vbCrLf & vbCrLf & _
"The database containing the Sale Number " & _
"has been locked for over 10 seconds." & vbCrLf & vbCrLf & _
"Continue Trying?", vbCritical + vbYesNo, "Locked Database")
If Ans = vbNo Then
Dim db As DAO.Database
Dim Frm As Form
For Each Frm In Forms
If Frm.Name <> "SaleMain" Then
Unload Frm
End If
Next
If Forms.Count > 0 Then
ForceShutDown = True
Unload SaleMain
Else
For Each db In DAO.DBEngine(0).Databases
db.Close
Next
End If
End
Else
StartAt = Now
End If
End If
Resume
Case 3046, 3186
' 3046 - Couldn't save because another user has the page locked.
' 3186 - Couldn't save; currently locked by user <name> on machine <name>.
TM = Timer + 0.5
Do Until Timer >= TM
Loop
Resume
Case 3197
' 3197 - you and another user are attempting to change the same data at the same time.
TM = Timer + 0.5
Do Until Timer >= TM
Loop
Resume Restart
Case Else
Set SN = Nothing
End Select
End Function