Hi,
I am trying to develop a Multiuser MS access application. For this I wanted to check whether table is locked by any other user, if yes pause a while and update, else update directly. I have been trying on searching for this kind of coding and got the below.But the issue is I am not able use this Function. Could any please help on how to use this Function. I am getting error of Argument is not optional. I just wanted to learn how to use the below function
Also if anybody can help me finding any learning links, it be a great kindness.
Best Regards,
I am using FUNCTION as:
Code:
Public Sub UpdateRecord()
UpdateUnitsInStock("Northwind Traders Fruit Cocktail",5,5)
End Sub
Below is the FUNCTION code
Code:
Function UpdateUnitsInStock(strProduct As String, intUnitsInStock _
As Integer, intMaxTries As Integer)
Dim dbs As Database, rstProducts As Recordset
Dim blnError As Boolean, intCount As Integer
Dim intLockCount, intChoice As Integer, intRndCount As Integer, intI As Integer
Const conFilePath As String = "E:\SQL Planner"
On Error GoTo ErrorHandler
' Open the database in shared mode.
Set dbs = OpenDatabase(conFilePath & "Northwind.mdb")
' Open the table for editing.
Set rstProducts = dbs.OpenRecordset("Products", dbOpenDynaset)
With rstProducts
' Set the locking type to pessimistic. Setting LockEdits to
' False would use optimistic locking.
.LockEdits = True
.FindFirst "ProductName = " & Chr(34) & strProduct & Chr(34)
If .NoMatch Then
' conErrNoMatch is defined at the module level as a public
' constant of type Integer with a value of -32737.
UpdateUnitsInStock = conErrNoMatch
GoTo CleanExit
End If
' Attempt to edit the record. If a lock error occurs, the
' error handler will attempt to resolve it. Because this procedure
' uses pessimistic locking, errors are generated when you begin to edit a
' record. If it used optimistic locking, lock errors would occur when you
' update a record.
.Edit
![UnitsInStock] = intUnitsInStock
.Update
End With
CleanExit:
rstProducts.Close
dbs.Close
Exit Function
ErrorHandler:
Select Case Err
Case 3197
' Data in the recordset has changed since it was opened.
' Try to edit the record again. This automatically refreshes
' the recordset to display the most recent data.
Resume
Case 3260 ' The record is locked.
intLockCount = intLockCount + 1
' Tried to get the lock twice already.
If intLockCount > 2 Then ' Let the user cancel or retry.
intChoice = MsgBox(Err.Description & " Retry?", _
vbYesNo + vbQuestion)
If intChoice = vbYes Then
intLockCount = 1
Else
' conErrRecordLocked is defined at the module level as a public
' constant of type Integer with a value of -32,736.
UpdateUnitsInStock = conErrRecordLocked
Resume CleanExit
End If
End If
' Yield to Windows.
DoEvents
' Delay a short random interval, making it longer each time the
' lock fails.
intRndCount = intLockCount ^ 2 * Int(Rnd * 3000 + 1000)
For intI = 1 To intRndCount: Next intI
Resume ' Try the edit again.
Case Else ' Unanticipated error.
MsgBox "Error " & Err & ": " & Error, vbOKOnly, "ERROR"
' conFailed is defined at the module level as a public
' constant of type Integer with a value of -32,761.
UpdateUnitsInStock = conFailed
Resume CleanExit
End Select
End Function