如何重置表格中的编号?

时间:2019-01-30 12:22:51

标签: sql ms-access

我有一些正在使用的数据。这样,我就可以实时删除记录。现在我有了一个有效的数据模型,我想重新开始自动编号。如何重设现有数据以遵循从1 .... n开始的自动格式设置?

我尝试做一个Compact and Repair,但这似乎不能解决问题。

供参考,我的表如下所示:

R-002
R-054
R-123
R-057
R-061

我想让他们回到:

   R-001
   R-002
   R-003
   R-004
   R-005

对于随后添加的每个条目,请遵循此新的自动编号,因此下一条记录将为R-006

编写UPDATE查询可立即修复,但不能解决将来输入自动编号的问题。

我认为有一种优雅的方法可以做到这一点。我表中的数据是Relationships的一部分,因此有些棘手。

如果有人有任何提示/建议,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要的是记录的顺序编号

我写了一篇有关执行此操作的各种方法的文章: Sequential Rows in Microsoft Access

一种是添加行号

' Builds consecutive row numbers in a select, append, or create query
' with the option of a initial automatic reset.
' Optionally, a grouping key can be passed to reset the row count
' for every group key.
'
' 2018-08-23. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RowNumber( _
    ByVal Key As String, _
    Optional ByVal GroupKey As String, _
    Optional ByVal Reset As Boolean) _
    As Long

    ' Uncommon character string to assemble GroupKey and Key as a compound key.
    Const KeySeparator      As String = "¤§¤"
    ' Expected error codes to accept.
    Const CannotAddKey      As Long = 457
    Const CannotRemoveKey   As Long = 5

    Static Keys             As New Collection
    Static GroupKeys        As New Collection
    Dim Count               As Long
    Dim CompoundKey         As String

    On Error GoTo Err_RowNumber

    If Reset = True Then
        ' Erase the collection of keys and group key counts.
        Set Keys = Nothing
        Set GroupKeys = Nothing
    Else
        ' Create a compound key to uniquely identify GroupKey and its Key.
        ' Note: If GroupKey is not used, only one element will be added.
        CompoundKey = GroupKey & KeySeparator & Key
        Count = Keys(CompoundKey)

        If Count = 0 Then
            ' This record has not been enumerated.
            '
            ' Will either fail if the group key is new, leaving Count as zero,
            ' or retrieve the count of already enumerated records with this group key.
            Count = GroupKeys(GroupKey) + 1
            If Count > 0 Then
                ' The group key has been recorded.
                ' Remove it to allow it to be recreated holding the new count.
                GroupKeys.Remove (GroupKey)
            Else
                ' This record is the first having this group key.
                ' Thus, the count is 1.
                Count = 1
            End If
            ' (Re)create the group key item with the value of the count of keys.
            GroupKeys.Add Count, GroupKey
        End If
        ' Add the key and its enumeration.
        ' This will be:
        '   Using no group key: Relative to the full recordset.
        '   Using a group key:  Relative to the group key.
        ' Will fail if the key already has been created.
        Keys.Add Count, CompoundKey
    End If

    ' Return the key value as this is the row counter.
    RowNumber = Count

Exit_RowNumber:
    Exit Function

Err_RowNumber:
    Select Case Err
        Case CannotAddKey
            ' Key is present, thus cannot be added again.
            Resume Next
        Case CannotRemoveKey
            ' GroupKey is not present, thus cannot be removed.
            Resume Next
        Case Else
            ' Some other error. Ignore.
            Resume Exit_RowNumber
    End Select
End Function

您可以随意设置以下格式,例如:

RNumber: "R-" & Format([RwoNumber], "000")