简体   繁体   中英

VBA multiple users to update same table in a shared MS-Access database using DAO

Excel crashes, VBA raises Error 3218 “Could Not Update” Record Locking Errors when multiple users try to update same table in a shared MS-Access database using DAO.

I have a special configuration like this: a MS-Access database located in shared network folder, multiple user connect to update that database using VBA DAO build on Excel file. The VBA code in each Excel file is the same. The problem happens when there are 2 users click on update button at the same time. User Excel file turn hanging, or showing error 3218 "Could not update".

Sub ExportToAccess()
    Dim oSelect As Range, i As Long, j As Integer, sPath As String
    'tblSuppliers.Active
    Set oSelect = Application.InputBox("Range", , Range("A1").CurrentRegion.Address, , , , , 8)

    Dim oDAO As DAO.DBEngine, oDB As DAO.Database, oRS As DAO.Recordset
    sPath = "\\sharedfolder\Database.accdb"

    Set oDAO = New DAO.DBEngine
    Set oDB = oDAO.OpenDatabase(sPath)
    Set oRS = oDB.OpenRecordset("tblSuppliers")

    For i = 2 To oSelect.Rows.Count 'skip label row
        oRS.AddNew
        For j = 1 To oSelect.Columns.Count 'Field(0) is Auto#
            oRS.Fields(j) = oSelect.Cells(i, j)

        Next j
        oRS.Update
    Next i
    oDB.Close
    MsgBox ("Updated Done!")

End Sub

I know my configuration is not good for database application, however I have to stick with this for a while. Could you please advise any solutions to avoid error when multiple users update Access database in this case ? Is there a way to detect if database is being updating by others and script to wait until that process to finish first. Any technical solution for this issue is welcome! Thank you!

You need some type of flag to tell if anyone is updating the table or not. Examples of what this flag can be:

  • An Excel file cell (that is probably the easiest in your case; if multiple excel files are used, just link to the one cell)
  • A field in an Access table (even a table with a single field and a single record dedicated just for that)
  • A (text) file in your shared drive (the flag can be the content of the file or even whether the file exists or not)

Then your update process would be:

- Check the flag, if set, loop until flag is cleared
- Set the flag
- Update the table
- Clear the flag

You will probably also need some way for the users (or just you) to clear the flag manually, in case something else goes wrong while updating the table and the flag gets stuck raised.

Well , this is not probably the most elegant solution

but you may create a field in a table , and ask for it before working with the table

something like this :

Set LockedStatus= oDB.OpenRecordset("mycontroltable")
if LockedStatus("lockedSuppiers")=False then 
  oDB.Execute"update mycontroltable set lockedSuppiers=true"

Set oRS = oDB.OpenRecordset("tblSuppliers")

    For i = 2 To oSelect.Rows.Count 'skip label row
        oRS.AddNew
        For j = 1 To oSelect.Columns.Count 'Field(0) is Auto#
            oRS.Fields(j) = oSelect.Cells(i, j)
        ........

         ......

     oDB.Execute"update mycontroltable set lockedSuppiers=false"
end if

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM