Access: A query that resets the autonumbering

前端 未结 1 1000
旧时难觅i
旧时难觅i 2021-01-28 08:22

My database is made for skiing competitions. The idea is that you can fill in the times people ski, and the databse automatically calculates what kind of medal you earned based

1条回答
  •  广开言路
    2021-01-28 09:05

    An Autonumber field is only for identifying records. No more no less.

    What you need is a Priority (or Rank) field.

    In your form where you display the records, run code like this for that field:

    Private Sub Priority_AfterUpdate()
    
        Dim rst             As DAO.Recordset
        Dim lngId           As Long
        Dim lngPriorityNew  As Long
        Dim lngPriorityFix  As Long
    
        ' Save record.
        Me.Dirty = False
    
        ' Prepare form.
        DoCmd.Hourglass True
        Me.Repaint
        Me.Painting = False
    
        ' Current Id and priority.
        lngId = Me!Id.Value
        lngPriorityFix = Nz(Me!Priority.Value, 0)
        If lngPriorityFix <= 0 Then
            lngPriorityFix = 1
            Me!Priority.Value = lngPriorityFix
            Me.Dirty = False
        End If
    
        ' Rebuild priority list.
        Set rst = Me.RecordsetClone
        rst.MoveFirst
        While rst.EOF = False
            If rst!Id.Value <> lngId Then
                lngPriorityNew = lngPriorityNew + 1
                If lngPriorityNew = lngPriorityFix Then
                    ' Move this record to next lower priority.
                    lngPriorityNew = lngPriorityNew + 1
                End If
                If Nz(rst!Priority.Value, 0) = lngPriorityNew Then
                    ' Priority hasn't changed for this record.
                Else
                    ' Assign new priority.
                    rst.Edit
                        rst!Priority.Value = lngPriorityNew
                    rst.Update
                End If
            End If
            rst.MoveNext
        Wend
    
        ' Reorder form and relocate record.
        Me.Requery
        Set rst = Me.RecordsetClone
        rst.FindFirst "Id = " & lngId & ""
        Me.Bookmark = rst.Bookmark
    
        ' Present form.
        Me.Painting = True
        DoCmd.Hourglass False
    
        Set rst = Nothing
    
    End Sub
    

    Just assign a rank to any record, and records will be renumbered as and if needed.

    0 讨论(0)
提交回复
热议问题