// EAN Barcode Formula for Crystal Reports. 
// Version 1.1 November 2003
// This formula creates an EAN8, EAN13 or UPC-A (UCC12) Barcode
// depending on the length of the barcode string, i.e. 8,12 or 13 digits.
// The font for this formula field must be set to "EAN Readable"
// The font size should typically be 20 points or greater.
// The validity of the barcode number and its check digit is not tested in this formula.

local stringvar BC;
// Set the value of BC to the database string that holds the barcode.
BC := {inmstk.BAR_CODE};

Local NumberVar array Pattern;

 Pattern :=    [10,10,10,10,10,
                10,20,10,20,20,
                10,20,20,10,20,
                10,20,20,20,10,
                20,10,10,20,20,
                20,20,10,10,20,
                20,20,20,10,10,
                20,10,20,10,20,
                20,10,20,20,10,
                20,20,10,20,10];

Local NumberVar Flag1 := val(Left(BC,1));

Switch (
// Format an EAN13 Barcode 
length(BC)=13,

Left(BC,1) &
'l' &
chr(asc(MID(BC,2,1))+10) &
chr(asc(MID(BC,3,1))+ Pattern[Flag1*5 + 1]) &
chr(asc(MID(BC,4,1))+ Pattern[Flag1*5 + 2]) &
chr(asc(MID(BC,5,1))+ Pattern[Flag1*5 + 3]) &
chr(asc(MID(BC,6,1))+ Pattern[Flag1*5 + 4]) &
chr(asc(MID(BC,7,1))+ Pattern[Flag1*5 + 5]) &
'c' &
chr(asc(MID(BC,8,1))+30) &
chr(asc(MID(BC,9,1))+30) &
chr(asc(MID(BC,10,1))+30) &
chr(asc(MID(BC,11,1))+30) &
chr(asc(MID(BC,12,1))+30) &
chr(asc(MID(BC,13,1))+30) &
'r',

// Format an EAN8 Barcode
length(BC)=8,
'l' &
chr(asc(MID(BC,1,1))+10) &
chr(asc(MID(BC,2,1))+10) &
chr(asc(MID(BC,3,1))+10) &
chr(asc(MID(BC,4,1))+10) &
'c' &
chr(asc(MID(BC,5,1))+30) &
chr(asc(MID(BC,6,1))+30) &
chr(asc(MID(BC,7,1))+30) &
chr(asc(MID(BC,8,1))+30) &
'r',

// Format a UPC-A (UCC12) Barcode
length(BC)=12,
'l' &
chr(asc(MID(BC,1,1))+10) &
chr(asc(MID(BC,2,1))+10) &
chr(asc(MID(BC,3,1))+10) &
chr(asc(MID(BC,4,1))+10) &
chr(asc(MID(BC,5,1))+10) &
chr(asc(MID(BC,6,1))+10) &
'c' &
chr(asc(MID(BC,7,1))+30) &
chr(asc(MID(BC,8,1))+30) &
chr(asc(MID(BC,9,1))+30) &
chr(asc(MID(BC,10,1))+30) &
chr(asc(MID(BC,11,1))+30) &
chr(asc(MID(BC,12,1))+30) &
'r'
)
