I have a Microsoft Excel spreadsheet that uses VBA to connect to a Microsoft Access database. The problem is that when multiple users are working with their local copy of the spreadsheet, often they are getting "Could not update; currently locked" error when trying to query the database.
I have tried setting the database to use optimistic locking when Excel creates a recordset but I'm still getting the error. Not sure what I'm doing wrong.
This is the code that creates the connection in each user's Excel file. accessCon is a global variable.
Dim accessFileLoc As String: accessFileLoc = ThisWorkbook.path & "\DB\GrabDB.accdb"
Set accessCon = CreateObject("ADODB.connection")
accessCon.Mode = 16 + 3 'adModeShareDenyNone + adModeShareReadWrite
accessCon.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & accessFileLoc
And this is the code that uses the connection to create a recordset. accessRS is local variable to the function with the query.
Dim accessQuery as string
accessQuery = "SELECT [ID] FROM user_table"
Dim accessRS As Object
Set accessRS = CreateObject("ADODB.Recordset")
accessRS.CursorLocation = adUseClient
accessRS.CursorType = adOpenStatic
accessRS.LockType = adLockOptimistic
accessRS.Open accessQuery, accessCon
...do something with accessRS...
If accessRS.State = 1 Then accessRS.close
Set accessRS = Nothing
What am I doing wrong? Thanks G