makebarcode.com
Home   Contact   About     |   PCL Barcode Fonts   |   Information about Barcodes  

AppNote 002
Calculating a UPC Barcode Checksum in Access Basic

UPC retail codes require a Modulo 10 check digit.  This sample code accepts the UPC number, calculates the check digit, and returns the finished string.  The general method involves adding up the digits in the odd-numbered positions and multiplying that sum by 3; then adding to that result the values of the digits in the even-numbered positions.  Divide this result by 10 and subtract the remainder from 10.  If the result is equal to 10, set it to zero; otherwise this is the check digit.

Function Format_UPC_String (InString As String) As String Dim OutString As String Dim Multiplier As Integer, Sum As Integer, i As Integer Dim CheckDigit As Integer ' ' Initialize the sum to zero ' Sum = 0 ' ' Add up the values of digits in the odd-numbered positions ' For i = 1 To Len(InString) Step 2 Sum = Sum + Val(Mid$(InString, i, 1)) Next i ' ' Multiply this result by 3, then add in the values of ' the digits in the even-numbered positions ' Sum = Sum * 3 For i = 2 To Len(InString) Step 2 Sum = Sum + Val(Mid$(InString, i, 1)) Next i ' ' Now calculate the Modulo 10 check digit ' CheckDigit = Sum Mod 10 CheckDigit = 10 - CheckDigit If CheckDigit = 10 Then CheckDigit = 0 End If OutString = InString + Format$(CheckDigit) Format_UPC_String = OutString End Function