0

MS Access で、テキスト フィールドの最後の 4 つの位置をインクリメントするコードを設定しようとしています。テキスト フィールドの数字は 7 桁です。例えば:

0010012

0010013

最初の 3 桁はメーカーを表し、最後の 4 桁は製品を表します。これらは私が増やしたいものです。オンラインで見つけた以下のコードを使用しています。動作するはずですが、「実行時エラー '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 関数を使用せずにコードを試してみましたが、インクリメントされた数値 0010014 の代わりに 00114 が返されました

4

3 に答える 3

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 に答える
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 に答える
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 に答える