GK in the UK
Registered User.
- Local time
- Today, 05:33
- Joined
- Dec 20, 2017
- Messages
- 281
I have a function to fetch the next unique document ID from a table.
But, I don't think it's robust if there are two users trying to fetch at the same time. What do I need to add to this code to
a) keep trying if the record is locked
b) once I have it opened, make sure it's not readable by another user
(Error handling code not added yet)
But, I don't think it's robust if there are two users trying to fetch at the same time. What do I need to add to this code to
a) keep trying if the record is locked
b) once I have it opened, make sure it's not readable by another user
(Error handling code not added yet)
Code:
Public Function fNextDocRef(strDocType As String) As String
Dim strRef As String
Dim db As Database
Dim rec As DAO.Recordset
Set db = CurrentDb
Set rec = CurrentDb.OpenRecordset("tbl2DocTypes", dbOpenDynaset)
With rec
.FindFirst "DocTypesID = '" & strDocType & "'" ' find the row for eg SORD
If Not .NoMatch Then
.Edit
strRef = Str(!dtNextDocNum) ' get the next number ...
!dtNextDocNum = !dtNextDocNum + 1 ' ... and increment
.Update
End If
End With
strRef = TrimChar(strRef) ' got eg 1234
strRef = Right("0000000" & strRef, LengthOfRefDigits) ' now 0001234
fNextDocRef = rec!dtPrefix & strRef ' now SORD0001234 (user defined prefix)
rec.Close
Set rec = Nothing
End Function