Multiple Users pulling same record from access file

antediluvian

New Member
Joined
Nov 9, 2016
Messages
1
So little background, I have an excel userform that my end users interact with. This form pulls and pushes data to and from Access databases. The trouble I am running into is that when I have several users on and they are pulling records at the same time, often they will pull matching records. This appears to happen when two users attempt to pull their next record at very near the same time. I need the function to prevent another user from pulling the same record. I thought the solution would be to immediately delete the record after copying it, but in practice we are still getting several duplication occurrences daily.

Below is the beginning of the code, the issue seems to be contained inside the if statement. Sometimes a user will get a lock error saying that the access file is locked. If they just debug and push it through it will finish running the code a moment later, but often they will have a duplicate record with another user.

Code:
Dim strMyPath As String, strDBName As String, strDB As String, strSQL As String
Dim i As Long, Time1 As Integer, lastrow As Long, n As Long, lFieldCount As Long
Dim rng As Range
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
Dim ws As Worksheet


strDBName = "ACTML.accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\DataFilesAdminOnly\" & strDBName
 
'Connect to a data source:
connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB & ";Persist Security Info=False;jet oledb:Database Password= XXXXXXXXXX ;"
'set the worksheet:
Set ws = ActiveWorkbook.Sheets("Data")
'Set the ADO Recordset object:
Set adoRecSet = New ADODB.Recordset
strTable = "Data"




Time1 = Hour(Now())


If Time1 > 10 And Time1 < 19 Then
    strSQL = "SELECT TOP 1 * FROM Data"
    adoRecSet.Open strSQL, connDB, adOpenStatic, adLockOptimistic
    Set rng = ws.Range("A1")
    rng.Offset(1, 0).CopyFromRecordset adoRecSet
    connDB.Execute "DELETE FROM (SELECT TOP 1 * FROM Data)"
    adoRecSet.Close
Else:

The if statement has several iterations depending on time of day the SQL code changes slightly. but the duplicates seem to be stemming from here. Any assistance would be very much appreciated. Thank You.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi,

Try Changing Lock type from adLockOptimistic to adLockPessimistic in below line of you code. This will prevent multiple users from accessing the same record at same time.

Code:
[/B]adoRecSet.Open strSQL, connDB, adOpenStatic, adLockOptimistic
 
Upvote 0

Forum statistics

Threads
1,215,359
Messages
6,124,488
Members
449,166
Latest member
hokjock

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top