Access / Excel集成:如何避免同时发生的用户之间发生冲突?

时间:2019-05-08 08:02:34

标签: sql excel database vba ms-access

我正在开发一个excel工具,该工具通过VBA连接到Access数据库,并且应用户要求在excel和DB之间传输数据。每个用户都有自己的excel工作簿版本,该版本连接到中央数据库。在大多数情况下,这可以正常工作,但是当两个用户试图同时连接时,我发现了一些冲突,导致其中一个用户excel / VBA崩溃而没有任何VBA错误代码。

我怀疑这是由数据库造成的,第二个用户以某种方式中断了连接。

所以我的问题是: 有没有一种方法可以在第一个用户连接后“锁定”连接,然后在第二个用户尝试连接时知道该数据库正在使用,因此他的请求可以中止/延迟,而不会导致excel崩溃?

我已经包含了其中一项交易的代码,该交易将数据从excel移动到数据库。

Public Sub ExportToAccess()

''''Establish connection to DB
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath & ";Persist Security Info=False;"
cn.Open


Set rstDbTable = New ADODB.Recordset

With rstDbTable

    .Open "AllCapacity", cn, adOpenKeyset, adLockPessimistic, adCmdTable

End With

''''Move data''''
'one area at the time

For area = 0 To UBound(cap_areas)

    toDbArray = sh_capacity.Range(cap_areas(area)) 'Save the range in question ot the db array. starts at 1

    'MsgBox (toDbArray(1, 1))

    For i = 1 To UBound(toDbArray, 1) 'for all rows

            rstDbTable.Filter = "CapArea = '" & NamesArea(area) & "'" _
            & " AND Week = '" & week & "'" _
            & " AND AreaWeekID = '" & i & "'"

        If rstDbTable.EOF Then 'Check if its empty and then create new records
            MsgBox ("Could not find row in DB. Script is ending. CapArea =" & NamesArea(area) & ". AreaWeekId = " & i)
            Stop ' Debugger
            GoTo EndOfCode

            'rstDbTable.AddNew 'Use if we need to add new row

        Else
                'rstDbTable("AreaWeekID").Value = toDbArray(i, 27)
                'rstDbTable("Week").Value = toDbArray(i, 28)
                'rstDbTable("CapArea").Value = toDbArray(i, 29)
                'start = Timer

                rstDbTable("MO_DA").Value = toDbArray(i, 2)
               ' MsgBox (Timer - start)

                rstDbTable("MO_EV").Value = toDbArray(i, 3)
                rstDbTable("MO_NI").Value = toDbArray(i, 4)
                rstDbTable("TU_DA").Value = toDbArray(i, 5)
                rstDbTable("TU_EV").Value = toDbArray(i, 6)
                rstDbTable("TU_NI").Value = toDbArray(i, 7)
                rstDbTable("WE_DA").Value = toDbArray(i, 8)
                rstDbTable("WE_EV").Value = toDbArray(i, 9)
                rstDbTable("WE_NI").Value = toDbArray(i, 10)
                rstDbTable("TH_DA").Value = toDbArray(i, 11)
                rstDbTable("TH_EV").Value = toDbArray(i, 12)
                rstDbTable("TH_NI").Value = toDbArray(i, 13)
                rstDbTable("FR_DA").Value = toDbArray(i, 14)
                rstDbTable("FR_EV").Value = toDbArray(i, 15)
                rstDbTable("FR_NI").Value = toDbArray(i, 16)
                rstDbTable("SA_DA").Value = toDbArray(i, 17)
                rstDbTable("SA_EV").Value = toDbArray(i, 18)
                rstDbTable("SA_NI").Value = toDbArray(i, 19)
                rstDbTable("SU_DA").Value = toDbArray(i, 20)
                rstDbTable("SU_EV").Value = toDbArray(i, 21)
                rstDbTable("SU_NI").Value = toDbArray(i, 22)



        End If

        rstDbTable.Update

    Next i
Next area

EndOfCode:
rstDbTable.Close

0 个答案:

没有答案