简体   繁体   English

使用自动编号作为后端ms Access数据库中的主键的表是否存在问题?

[英]Are there issues with tables using an autonumber as a primary key in a back-end ms access db?

I inherited an MS Access database at my office that is heavily used by several people over the network. 我在办公室继承了一个MS Access数据库,该数据库被网络上的许多人大量使用。 This causes many issues with data collisions and locks. 这会导致数据冲突和锁定的许多问题。 I want to split the db so that each user has thier own front-end app and maintain the core data on the server. 我想拆分数据库,以便每个用户都有自己的前端应用程序,并在服务器上维护核心数据。

Several of the tables use an autonumber:sequence:long as thier primary key - in researching how to perform the split I've come across several posts that hint this can cause issues when distributing a database but I haven't been able to find anything solid. 有几个表使用自动编号:序列:长期作为主键 - 在研究如何执行拆分时我遇到了几个帖子,提示这可能会导致分发数据库时出现问题,但我找不到任何东西固体。 The issue seems to be that a user can begin a new record and receive the next autonumber but a second user can create a new record within a short interval and receive the same autonumber resulting in an error? 问题似乎在于,用户可以开始新记录并接收下一个自动编号,而第二个用户可以在很短的间隔内创建新记录并接收相同的自动编号,从而导致错误?

Does Jet handle this correctly or are there autonumber issues with a FE/BE database? Jet是否正确处理此问题,或者FE / BE数据库是否存在自动编号问题? If it's an unlikely-but-possile occurance I'm sure it will still be much better than what my users are currently experiencing but I'd like to know if there are ways I can minimize such issues. 我相信如果这是一次不太可能但可能发生的事情,它肯定会比我的用户目前所遇到的情况要好得多,但是我想知道是否有办法可以最大程度地减少此类问题。

Thanks for your help! 谢谢你的帮助!

I've had the misfortune of working with many Access databases in my youth. 我很幸运在我年轻时使用过许多Access数据库。 While there are many issues with Access, I do not know if I've ever run into a problem with AutoNumber columns in a split database, multi-user environment. 虽然Access存在许多问题,但我不知道在拆分数据库,多用户环境中是否遇到过AutoNumber列的问题。 It should work fine. 它应该工作正常。 This is such a common setup that there would be posts all over the Internet about it if were an issue. 这是一个常见的设置,如果是一个问题,将在互联网上发布关于它的帖子。

I had the same problem, nevertheless i did a workarround to get the autonumbering work from an Onload() Event 我有同样的问题,但我做了一个workarround来从Onload()事件获得自动编号工作

What I did is : 我做的是:

  1. I create a recordset based on Your_Table everytime the user needs an autonumber 每当用户需要自动编号时,我都会根据Your_Table创建一个记录集
  2. Open the recordset (rst) 打开记录集(第一个)
  3. Search if: 搜索是否:
    -Your_Table is Empty, then assigns the value "1" to Your_field -Your_Table为空, 然后将值“ 1”分配给Your_field
    -Your_Table is has data without missing numbers, then assigns the value = "Count of lines + 1" to Your_field (1,2,....,n+1) -Your_Table有没有丢失数字的数据, 然后将值=“行数+ 1”分配给Your_field(1,2,....,n + 1)
    -Your_Table has missing data (1,3,4,5,7) [Note "#2 and #7 are missing]", then uses a function to search in Your_Table the missing fields and assign to Your_Field the first missing value (#2 in this example) -Your_Table缺少数据(1,3,4,5,7)[注意“#2和#7缺失]”, 然后使用函数在Your_Table中搜索缺少的字段并为Your_Field分配第一个缺失值(#在此示例中为2)


Private Sub Autonumbering(Your_Table As String)
Dim rst As DAO.Recordset
Dim db As Database

On Error GoTo ErrorHandler

Application.Echo False

Set db = CurrentDb
Set rst = db.OpenRecordset(Your_Table, dbOpenDynaset)

                    With rst
                        .AddNew
                            'Your_Table is Empty, **then** assigns the value "1" to Your_field
                            If DMin("[Your_Field]", Your_Table) = 1 Then
                                'Your_Table is has data without missing numbers,**then** assigns the value = "Count of lines + 1" to Your_field (1,2,....,n+1)
                                If DMax("[Your_Field]", Your_Table) = .RecordCount Then
                                    'Assings n+1 value to [Your_Field] records
                                    Value = .RecordCount + 1
                                        ![Your_Field] = Valor
                                Else
                                    'Your_Table has missing data (1,3,4,5,7) [Note "#2 and #7 are missing]", **then** uses a function to search in Your_Table & _
                     the missing fields and assign to Your_Field the first missing value (#2 in this example)
                                    Value = MyFunction$(Your_Table, "Your_Field")
                                        ![Your_Field] = Value
                                End If
                            Else
                            'Agrega el número 1
                            Value = 1
                            ![Your_Field] = Value
                            End If
                        .Update
                        .Bookmark = .LastModified
                        Me.Requery
                        DoCmd.GoToRecord acDataForm, Me.Name, acGoTo, Value
                        .Move 0, .LastModified
                    End With
ErrorCorregido:
Application.Echo True
Exit Sub

ErrorHandler:
MsgBox "An error ocurred, please verify numbering", vbCritical + vbOKOnly
Resume ErrorCorregido

End Sub


Here is the function that i found to get the missing values on an specific table, i cant find it anymore, but thanks for the one who made it. 这是我发现的功能,以获取特定表上的缺失值,我再也找不到它,但感谢制作它的人。


Function MyFunction$(cstrTable As String, cstrField As String)

' Read table/query sequentially to record all missing IDs.
' Fill a ListBox to display to found IDs.
' A reference to Microsoft DAO must be present.

  Dim dbs     As DAO.Database
  Dim rst     As DAO.Recordset
  Dim lst     As ListBox
  Dim Col     As Collection

  Dim strSQL  As String
  Dim strList As String
  Dim lngLast As Long
  Dim lngNext As Long
  Dim lngMiss As Long

  ' Build SQL string which sorts the ID field.
  strSQL = "Select " & cstrField & "" _
   & " From " & cstrTable & " Order By 1;"

  Set Col = Nothing
  ' Control to fill with missing numbers.
  'Set lst = Me!lstMissing

  ' Collection to hold the missing IDs.
  Set Col = New Collection

  '// Vacía la colección
  'Erase Col
    ' Read the table.
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset(strSQL)

  If rst.RecordCount = 0 Then
    ' The recordset is empty.
    ' Nothing to do.
  Else
    ' Read and save the ID of the first record.
    lngLast = rst(cstrField).value
    rst.MoveNext
    ' Loop from the second record through the recordset
    ' while reading each ID.
    While rst.EOF = False
      lngNext = rst(cstrField).value
      ' For each ID, fill the collection with the
      ' missing IDs between the last ID and this ID.
      For lngMiss = lngLast + 1 To lngNext - 1
        Col.Add (lngMiss)
      Next
      ' Save the last read ID and move on.
      lngLast = lngNext
      rst.MoveNext
    Wend
    ' Finally, add the next possible ID to use.
    Col.Add (lngLast + 1)
  End If
  rst.Close

  For lngMiss = 1 To Col.Count
    ' Build the value list for the ListBox.
    If Len(strList) > 0 Then
      ' Append separator.
      strList = strList & ";"
    End If
    ' Append next item from the collection.
    strList = strList & Col(lngMiss)
    ' For debugging only. May be removed.
    Debug.Print Col(lngMiss)
  Next
  ' Pass the value list to the ListBox.
  ' Doing so will requery it too.
  ' lst.RowSource = strList
  ' For debugging only. May be removed.
  ' Debug.Print strList
  MyFunction$ = Col(1)
  ' Clean up.
  Set rst = Nothing
  Set dbs = Nothing
  Set Col = Nothing
  Set lst = Nothing

End Function

As long as you are not going for data replication (ie multiple subscriber databases, where users can insert new records in same tables but in different locations), you will not have problems with autonumbers as primary keys. 只要您不打算进行数据复制(即,多个订户数据库,用户可以在其中在同一表中但在不同位置插入新记录),自动编号作为主键就不会有问题。

If you think that one of these days you might need to go for replication (different locations, one central database), do not hesitate to switch to unique identifiers (replication IDs). 如果您认为有一天您可能需要进行复制(不同位置,一个中央数据库),请不要犹豫,切换到唯一标识符(复制ID)。

There seems to be some confusion on your part about the process of splitting. 你的分裂过程似乎有些混乱。 When you do so, you end up with multiple front ends, but the back end is still a single file. 这样做时,最终会有多个前端,但是后端仍然是单个文件。 Thus, there's no difference at all for the data tables in terms of Autonumbers from what you had before you split the application. 因此,就自动编号而言,数据表与拆分应用程序之前的数据完全没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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