You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Access VBA:如何让生成记录的SerialNumber从指定值开始?

自定义起始编号生成序列号记录

可以实现这个需求,只需对现有代码做几处关键调整,以下是修改后的完整代码及说明:

关键修改点

  • 新增读取Start文本框值的变量,为空时默认设为1
  • 调整循环范围:从Start开始,到Start + 数量 - 1结束
  • 保留原有的两位数字格式化逻辑,确保小于10的编号自动补0

修改后的完整代码

Private Sub OpenForm_Click()
    Dim strToTextBox As String
    Dim strAantal As Integer
    Dim intHowManyCopies As Integer
    Dim intStart As Integer ' 新增起始编号变量
    
    strAantal = Me.Aantal
    ' 读取起始编号,空值默认设为1
    intStart = CInt(Nz(Me.Start, 1))

    On Error Resume Next
    intHowManyCopies = CInt(strAantal)
    On Error GoTo 0

    If intHowManyCopies <> 0 Then
        ' 循环从起始编号开始,到起始编号+数量-1
        For intCopy = intStart To intStart + intHowManyCopies - 1
            ' 保持两位数字格式化
            If intCopy < 10 Then
                strToTextBox = Me.OrderNr & "0" & CStr(intCopy)
            Else
                strToTextBox = Me.OrderNr & CStr(intCopy)
            End If
            CurrentDb.Execute "INSERT INTO Geleidelijst " & _
            "(SerialNmbr, InvoerOrderNr, InvoerAantal, InvoerSapArtNr, InvoerVermogen, LS1, InvoerSAPItemName, LS2, HS1, HS2) " & _
            "VALUES('" & strToTextBox & "','" & Me.OrderNr & "'," & Me.Aantal & ",'" & Me.SapArtNr & "','" & Me.Vermogen & "','" & Me.LS1 & "','" & Me.SAPItemName & "','" & Me.LS2 & "','" & Me.HS1 & "','" & Me.HS2 & "')"
        Next intCopy
    End If
    
    DoCmd.OpenForm "GeleidelijstForm"

    DoCmd.GoToRecord acDataForm, "GeleidelijstForm", acLast
    Dim t1 As Long, i As Long
    t1 = CLng(Nz(Me.Aantal, 1))
    DoCmd.GoToRecord acDataForm, "GeleidelijstForm", acLast
    For i = 1 To t1 - 1
        DoCmd.GoToRecord acDataForm, "GeleidelijstForm", acPrevious
    Next i
    DoCmd.Close acForm, "OrderinvoerForm"
    Call Forms.GeleidelijstForm.PrintFrm_Click
End Sub

验证示例

当OrderNr=1111、Aantal=6、Start=13时,循环会从13到13+6-1=18,生成的序列号依次为:111113、111114、111115、111116、111117、111118,完全符合需求。

内容的提问来源于stack exchange,提问作者SeeeeS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 16:18:21