Sunday, September 13, 2009

RW - Working with POSTNET barcodes for US zip codes

The Postal Numeric Encoding Technique (POSTNET) is a barcoding system developed by the United States Postal Service to assist with automatic mail sorting and routing. The POSTNET barcode uses a combination of half-bars and full-bars to encode the zip code that appears on a mail piece. The barcode starts and ends with a full bar (often called a guard rail or frame bar and represented as the letter "S" in the USPS TrueType Font).

In the United States, zip codes can be of 5 digits, 9 digits (also known as Zip+4), or 11 digits in the case of a specific delivery point. Each digit in the zip code is represented by 5 bars. Therefore a 5-digit zip code is represented by 25 bars. In addition, to ensurePOSTNET accuracy during mail processing, a check digit, which is five bars, is calculated and added to the zip code. Hence a 5-digit zip code would render a total of 32 bars: 25 bars for the 5-digit zip code + 5 bars for the check digit + the 2 enclosing guard rail bars. Hence, Zip+4 uses 52 bars, and a delivery point uses 62 bars.

To calculate the check digit, each digit in the zip code is added. The result is then subtracted from the nearest multiple of 10. For example, if each digit in the zip code 33076 is added, 3+3+0+7+6 = 19, and we subtract from the nearest multiple of 10, number 20, then the check digit is 1. Mathematically speaking 10 - (digit-sum mod 10). This will yield the following S330761S. This number is then represented by the following barcode (including the guard rails, represented by "S"):

While based on a binary system, the weight of each bar is different than in standard binary. The weight of each bar is as follows:

The following illustration shows are representation of all numbers:

To add zip code barcoding capabilities to a Report Writer report, for example an Invoice or a Check, you must first download and install the fonts. You can get a set of fonts from here. To install the fonts (the TTF files in the zipped file), extract the fonts to your My Documents folder, then copy them to your Windows\Fonts folder.

1) Open Report Writer and select the SOP Blank Invoice Form. Click the Layout button to open the report in the Layout window.

2) Add the RM_Customer_MSTR_ADDR.Zip field to both Report Header and Page Header sections. Set the fields properties to Invisible.

3) Create a string calculated field called (C) Postnet. Assign the RM_Customer_MSTR_ADDR.Zip field to this calculated field as shown in the picture:

4) Add the (C) Postnet calculated field to the Report Header and Page Header sections. Change the font to IDAutomationSPOSTNET. By now, your report layout should look like this:

5) Add the report to VBA. Go to Tools > Add Report to VBA.

6) Select all 4 fields, the 2 on the report header and the 2 on the page header and add them to VBA. Go to Tools > Add Fields to VBA.

7) Open the VBA Editor, locate the SOPBlankInvoiceForm (Report) object in the Project Explorer , double-click and add the following code:

Public Function EncodeZip(pZip As String) As String
    Dim i As Integer
    Dim sZip As String
    Dim digitSum As Integer
    Dim checkDigit As Integer
    sZip = "S"
    digitSum = 0
    For i = 1 To Len(pZip)
        If IsNumeric(Mid(pZip, i, 1)) Then
            ' Concat the digit
            sZip = sZip & Mid(pZip, i, 1)
            ' Add up the digits
            digitSum = digitSum + CInt(Mid(pZip, i, 1))
        End If
    Next i
    ' Calculate check digit
    checkDigit = 10 - (digitSum Mod 10)
    sZip = sZip & CStr(checkDigit) & "S"
    EncodeZip = sZip
End Function

Private Sub Report_BeforePH(SuppressBand As Boolean)
    Dim sZip As String
    sZip = Zip
    sZip = EncodeZip(sZip)
    CPostnet = sZip
End Sub

Private Sub Report_BeforeRH(SuppressBand As Boolean)
    Dim sZip As String
    sZip = Zip
    sZip = EncodeZip(sZip)
    CPostnet = sZip
End Sub

Compile the code.

8) Return to Microsoft Dynamics GP saving all changes as you exit.

9) Grant security to the modified report. Print an invoice to test.

I hope you found this article interesting and the implementation very simple. You can find the fonts and package file for this project at the bottom of this article.


Postnet Fonts - Click here
v10 SOP Blank Invoice Form report - Click here

Until next post!

Mariano Gomez, MVP
IntellPartners, LLC


Jo Ann de Ruiter said...

How can you get this on a Word Template?

Mariano Gomez said...

Jo Ann,

Barcodes are just a type of font. You can simply download a font pack for word then change the font on the template. You will then need then save the template into a folder and reimport into GP.

Mariano Gomez, MVP