MS Access 在字段中为每个值递增一个数字

MS Access incrementing a number per value in a field

PO_D_Temp 包含多个字段 2,其中 [InvNo] 和 [Detail_Line]。将有许多行属于相同的 [InvNo]。我希望每个 [Detail_Line] 每个 [InvNo]

以 1、2 等开头

示例:[InvNo]1 [Detail_Line] 1 [InvNo]1 [Detail_Line] 2 [InvNo]2 [Detail_Line] 1 [InvNo]3 [Detail_Line] 1 ETC!

我能弄清楚的是一个循环,它向所有记录添加一个递增的数字 - 而不是在每个不同的 InvNo 处重新编号。

这是我的(我是幼儿园水平,不好意思):

    Private Sub Command1_Click()

    Dim db As Database
    Set db = CurrentDb()

    Dim rstPO_D_Temp As Recordset
    Dim strSQL As String
    Dim intI As Integer
    Dim DetailNum As Integer

    'Open a recordset on all records from the PO_D_Temp table
       strSQL = "SELECT * FROM PO_D_Temp"
       Set rstPO_D_Temp = db.OpenRecordset(strSQL, dbOpenDynaset)
       DetailNum = 0

    '   If the recordset is empty, exit.
       If rstPO_D_Temp.EOF Then Exit Sub

       intI = 1
       With rstPO_D_Temp
          Do Until .EOF
             .Edit
             ![Detail_Line] = DetailNum + intI
             .Update
             .MoveNext
             intI = intI + 1
          Loop
       End With

       rstPO_D_Temp.Close
       Set rstPO_D_Temp = Nothing

    End Sub

重置详细信息编号。每张发票编号:

Private Sub Command1_Click()

    Dim db As DAO.Database
    Set db = CurrentDb()

    Dim rstPO_D_Temp As DAO.Recordset
    Dim strSQL As String
    Dim DetailNum As Integer
    Dim LastInvoice As Long

    'Open a recordset on all records from the PO_D_Temp table
    strSQL = "SELECT * FROM PO_D_Temp"
    Set rstPO_D_Temp = db.OpenRecordset(strSQL, dbOpenDynaset)

    With rstPO_D_Temp
        Do Until .EOF
            If LastInvoice <> !InvNo.Value Then
                DetailNum = 1
                LastInvoice = !InvNo.Value
            Else
                DetailNum = DetailNum + 1
            End If
            .Edit
            ![Detail_Line].Value = DetailNum
            .Update
            .MoveNext
        Loop
        .Close
    End With

    Set rstPO_D_Temp = Nothing

End Sub