0

我正在尝试在 MS Access 中设置一个代码,以增加文本字段的最后四个位置。文本字段中的数字有七位数字。例如:

0010012

0010013

前三位代表制造商,后四位代表产品。这些是我想要增加的。我正在使用我在网上找到的下面的代码,它应该可以工作,但我不断收到错误消息:“运行时错误'13':类型不匹配”

Dim varSifra As Variant
varSifra = DMax("[Sifra]", "tblProducts", "[Manufacturer] = " & Forms!frmProduct!Manufacturer)
Me.[Sifra] = Left(varSifra, 3) & Format(Val(Right(varSifra, 4)) + 1, "0000")

我尝试了没有 Format 函数的代码,但我得到的是 00114 而不是递增的数字 0010014

4

3 回答 3

2

您可以很好地使用简单的 Format 调用,但是首先需要将输入显式转换为 Long:

Function IncProductNumber(Value)
    If IsNull(Value) Then
        Let IncProductNumber = Null
    Else
        Let IncProductNumber = Format(CLng(Value) + 1, "0000000")
    End If
End Function

或者,更一般地说,可以从输入中推断出所需的填充:

Function IncTextNumber(Value)
    If IsNull(Value) Then
        Let IncTextNumber = Null
    Else
        Let IncTextNumber = Format(CLng(Value) + 1, String$(Len(Value), "0"))
    End If
End Function

IncTextNumber("0123") 将产生“0124”,IncTextNumber("00999") 将产生“01000”,依此类推。

于 2013-10-05T18:56:25.123 回答
2

Can this help?

Sub Test()
    Debug.Print IncrementProduct("0010001") //Prints 0010002
    Debug.Print IncrementProduct("0010012") //Prints 0010013
    Debug.Print IncrementProduct("0010099") //Prints 0010100
End Sub


Function IncrementProduct(code As String) As String
    Dim manufacturerCode As String, padding As String, productCode As String

    manufacturerCode = VBA.Left$(code, 3)
    productCode = CInt(VBA.Right$(code, Len(code) - Len(manufacturerCode))) + 1
    padding = Application.WorksheetFunction.Rept("0", 4 - Len(productCode))

    IncrementProduct = manufacturerCode & padding & productCode
End Function
于 2013-10-04T11:04:50.700 回答
1
Dim tempManProd As String, tempNumToInc As Integer

tempManProd = 'get the value you are wanting to increment
tempNumToInc = CInt(right(tempManProd, 4))
tempNumToInc = tempNumToInc + 1

'This will make sure that the 0s get added back to the front of the product
Do While (Len(tempManProd & "") + Len(tempNumToInc & "")) < 7
  tempManProd = tempManProd & "0"
Loop

tempManProd = tempManProd & CStr(tempNumToInc)
于 2013-10-04T13:09:57.333 回答