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
相关产品推荐
相关产品推荐

