DarrenBoyer
IS-IT--Management
I've been working on this concept for some time and once it's done my database is ready to be field tested at work. There isn't too much time pressure just the occasional, "why is this taking so long?" right now.
This is modified from MichaelRed's FAQ700-184. However, once it was running correctly it still wasn't updating the CustomerID field when I wanted to create a new record. My code modifications to implement CustomerID field update are in bold. My idea was to perform the action on an event such as BeforeUpdate even though the original code was written as a function. At this point I'm very open to suggestions.
Function Custom_Counter()
(Was Public Sub Custom-Counter) [b/]
On Error GoTo Custom_Counter_Err
Dim NextAvailableCustomerID As Long
Dim db As DAO.Database
Dim BaseData As DAO.Recordset
Dim tblCounterTable As DAO.Recordset
Dim qMaxCustID As DAO.Recordset
Dim Customers As DAO.Recordset
Const RiErr = 3000
Const LockErr = 3260
Const InUseErr = 3262
Const NumReTries = 20#
'Variables for the Retry Counts
Dim NumLocks As Integer
Dim lngX As Long
'Variables Used in the Code
Dim NextAvailableCounter As Long
Dim lngOldCustomerID As Long
Dim lngNewCustomerID As Long
Dim lngBigCustomerID As Long
Dim CustomerID As Long
Set db = CurrentDb()
Set qMaxCustID = db.OpenRecordset("qMaxCustID", dbDenyRead)
Set BaseData = db.OpenRecordset("Customers")
Set tblCounterTable = db.OpenRecordset("tblCounterTable", dbDenyRead)
Set Customers = db.OpenRecordset("Customers", dbAppendOnly)
lngOldCustomerID = qMaxCustID!CustomerID
lngNewCustomerID = tblCounterTable!NextAvailableCounter 'C
lngBigCustomerID = lngNewCustomerID 'Big=C
If (lngOldCustomerID > lngBigCustomerID) Then
lngBigCustomerID = lngOldCustomerID
End If
If (NextAvailableCounter > lngBigCustomerID) Then
lngBigCustomerID = NextAvailableCounter
End If
'Increment the ID
lngBigCustomerID = lngBigCustomerID + 1
lngBigCustomerID = CustomerID
'Update the ID Value
With tblCounterTable
.Edit
!NextAvailableCounter = lngBigCustomerID
.Update
End With
With Customers
.AddNew
!CustomerID = lngBigCustomerID
.Update
End With
MsgBox "Next Available Counter is " & Str(lngBigCustomerID)
lngNewCustomerID = lngBigCustomerID
NormExit:
Set BaseData = Nothing
Set db = Nothing
Exit Function 'Return
Custom_Counter_Err:
'Check For the expected errors
If ((Err = InUseErr) Or (Err = LockErr) Or (Err = RiErr)) Then
'If one of the expected ones, increment the counter
NumLocks = NumLocks + 1
If (NumLocks < NumReTries) Then
For lngX = 1 To NumLocks ^ 2 * Int(Rnd * 20 + 5)
DoEvents
Next lngX
Resume Next
Else
End If
Else
MsgBox "Error" & Err.Number & ": " & Err.Description, _
vbOKOnly & vbCritical, "Get CustomerID"
GoTo NormExit
End If
End Function
This is modified from MichaelRed's FAQ700-184. However, once it was running correctly it still wasn't updating the CustomerID field when I wanted to create a new record. My code modifications to implement CustomerID field update are in bold. My idea was to perform the action on an event such as BeforeUpdate even though the original code was written as a function. At this point I'm very open to suggestions.
Function Custom_Counter()
(Was Public Sub Custom-Counter) [b/]
On Error GoTo Custom_Counter_Err
Dim NextAvailableCustomerID As Long
Dim db As DAO.Database
Dim BaseData As DAO.Recordset
Dim tblCounterTable As DAO.Recordset
Dim qMaxCustID As DAO.Recordset
Dim Customers As DAO.Recordset
Const RiErr = 3000
Const LockErr = 3260
Const InUseErr = 3262
Const NumReTries = 20#
'Variables for the Retry Counts
Dim NumLocks As Integer
Dim lngX As Long
'Variables Used in the Code
Dim NextAvailableCounter As Long
Dim lngOldCustomerID As Long
Dim lngNewCustomerID As Long
Dim lngBigCustomerID As Long
Dim CustomerID As Long
Set db = CurrentDb()
Set qMaxCustID = db.OpenRecordset("qMaxCustID", dbDenyRead)
Set BaseData = db.OpenRecordset("Customers")
Set tblCounterTable = db.OpenRecordset("tblCounterTable", dbDenyRead)
Set Customers = db.OpenRecordset("Customers", dbAppendOnly)
lngOldCustomerID = qMaxCustID!CustomerID
lngNewCustomerID = tblCounterTable!NextAvailableCounter 'C
lngBigCustomerID = lngNewCustomerID 'Big=C
If (lngOldCustomerID > lngBigCustomerID) Then
lngBigCustomerID = lngOldCustomerID
End If
If (NextAvailableCounter > lngBigCustomerID) Then
lngBigCustomerID = NextAvailableCounter
End If
'Increment the ID
lngBigCustomerID = lngBigCustomerID + 1
lngBigCustomerID = CustomerID
'Update the ID Value
With tblCounterTable
.Edit
!NextAvailableCounter = lngBigCustomerID
.Update
End With
With Customers
.AddNew
!CustomerID = lngBigCustomerID
.Update
End With
MsgBox "Next Available Counter is " & Str(lngBigCustomerID)
lngNewCustomerID = lngBigCustomerID
NormExit:
Set BaseData = Nothing
Set db = Nothing
Exit Function 'Return
Custom_Counter_Err:
'Check For the expected errors
If ((Err = InUseErr) Or (Err = LockErr) Or (Err = RiErr)) Then
'If one of the expected ones, increment the counter
NumLocks = NumLocks + 1
If (NumLocks < NumReTries) Then
For lngX = 1 To NumLocks ^ 2 * Int(Rnd * 20 + 5)
DoEvents
Next lngX
Resume Next
Else
End If
Else
MsgBox "Error" & Err.Number & ": " & Err.Description, _
vbOKOnly & vbCritical, "Get CustomerID"
GoTo NormExit
End If
End Function