• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 922
  • Last Modified:

Generate a barcode with a Code128 format

Hi experts, can someone explain this code? I've downloaded this in the web, this code generates a barcode using code 128 format, I wonder why this code generates a very long barcode compared to the other programs, when i printed the generated barcode, the scanner have difficulty in reading it, is there a way to recode this so that it will be easily readable and at the same time it will be shortened the width?


Public Function Code128(strCode As String)
Dim varBar1, varBar2
    varBar1 = Split(" <>!<>" & Chr(34) & "<>#<>$<>%<>&<>'<>(<>)<>*<>+<>,<>-<>.<>/<>0<>1<>2<>3<>4<>5<>6<>7<>8<>9<>:<>;<><<>=<>><>?<>@<>A<>B<>C<>D<>E<>F<>G<>H<>I<>J<>K<>L<>M<>N<>O<>P<>Q<>R<>S<>T<>U<>V<>W<>X<>Y<>Z<>[<>\<>]<>^<>_<>`<>a<>b<>c<>d<>e<>f<>g<>h<>i<>j<>k<>I<>m<>n<>o<>p<>q<>r<>s<>t<>u<>v<>w<>x<>y<>z<>{<>|<>}<>~<>DEL<>FNC 3<>FNC 2<>SHIFT<>CODE C<>FNC 4<>CODE A<>FNC 1<>Start A<>Start B<>Start C<>Stop", "<>")
    varBar2 = Split("212222,222122,222221,121223,121322,131222,122213,122312,132212,221213,221312,231212,112232,122132,122231,113222,123122,123221,223211,221132,221231,213212,223112,312131,311222,321122,321221,312212,322112,322211,212123,212321,232121,111323,131123,131321,112313,132113,132311,211313,231113,231311,112133,112331,132131,113123,113321,133121,313121,211331,231131,213113,213311,213131,311123,311321,331121,312113,312311,332111,314111,221411,431111,111224,111422,121124,121421,141122,141221,112214,112412,122114,122411,142112,142211,241211,221114,413111,241112,134111,111242,121142,121241,114212,124112,124211,411212,421112,421211,212141,214121,412121,111143,111341,131141,114113,114311,411113,411311,113141,114131,311141,411131,211412,211214,211232,2331112", ",")
Dim chksum As Single: chksum = 104
sBar = "211214"
For i0 = 1 To Len(strCode)
    For i1 = 0 To UBound(varBar1)
        If Mid(strCode, i0, 1) = varBar1(i1) Then
            sBar = sBar & varBar2(i1)
            chksum = chksum + (i1 * i0)
            Exit For
        End If
    Next
Next
sBar = sBar & varBar2(chksum - (Int(chksum / 103) * 103)) & "2331112"
End Function
1
jacristobal
Asked:
jacristobal
  • 4
  • 3
1 Solution
 
domjCommented:
Please see the notes inserted:

Public Function Code128(strCode As String)
Dim varBar1, varBar2

'Creates two arrays. The first is populated with all characters that can be represented in Code128.
'The second is populated with the numerical values of these characters.

    varBar1 = Split(" <>!<>" & Chr(34) & "<>#<>$<>%<>&<>'<>(<>)<>*<>+<>,<>-<>.<>/<>0<>1<>2<>3<>4<>5<>6<>7<>8<>9<>:<>;<><<>=<>><>?<>@<>A<>B<>C<>D<>E<>F<>G<>H<>I<>J<>K<>L<>M<>N<>O<>P<>Q<>R<>S<>T<>U<>V<>W<>X<>Y<>Z<>[<>\<>]<>^<>_<>`<>a<>b<>c<>d<>e<>f<>g<>h<>i<>j<>k<>I<>m<>n<>o<>p<>q<>r<>s<>t<>u<>v<>w<>x<>y<>z<>{<>|<>}<>~<>DEL<>FNC 3<>FNC 2<>SHIFT<>CODE C<>FNC 4<>CODE A<>FNC 1<>Start A<>Start B<>Start C<>Stop", "<>")
    varBar2 = Split("212222,222122,222221,121223,121322,131222,122213,122312,132212,221213,221312,231212,112232,122132,122231,113222,123122,123221,223211,221132,221231,213212,223112,312131,311222,321122,321221,312212,322112,322211,212123,212321,232121,111323,131123,131321,112313,132113,132311,211313,231113,231311,112133,112331,132131,113123,113321,133121,313121,211331,231131,213113,213311,213131,311123,311321,331121,312113,312311,332111,314111,221411,431111,111224,111422,121124,121421,141122,141221,112214,112412,122114,122411,142112,142211,241211,221114,413111,241112,134111,111242,121142,121241,114212,124112,124211,411212,421112,421211,212141,214121,412121,111143,111341,131141,114113,114311,411113,411311,113141,114131,311141,411131,211412,211214,211232,2331112", ",")

'Set the initial checksum value.

Dim chksum As Single: chksum = 104

'Set the value of the Start Bar (which is the line that the barcode takes as the beginning of a barcode

sBar = "211214"

'This part loops through all the individual characters in the string that is passed into the function and
'compares it individually with each value in the first array and once it finds a match it adds the numeric
'value to the total string (sBar)

For i0 = 1 To Len(strCode)
    For i1 = 0 To UBound(varBar1)
        If Mid(strCode, i0, 1) = varBar1(i1) Then
            sBar = sBar & varBar2(i1)

'This part recalculates the checksum depending on what values have just been added to the string.

            chksum = chksum + (i1 * i0)
            Exit For
        End If
    Next
Next

'This part puts together the barcodes numerical value, calculates the final checksum and
'adds the end bar to signify that the barcode is finished

sBar = sBar & varBar2(chksum - (Int(chksum / 103) * 103)) & "2331112"
End Function

If you want any more specific info relating to any specific functions, let me know.

Dom
0
 
jacristobalAuthor Commented:
Thanks domj, can i replace the values of the arrays? where those values came from? where is the part of the code that makes spaces beetween the bars of the barcode? can i replace it so that the spaces between is bigger than the spaces appears when I print it?
0
 
domjCommented:
You certainly can replace the values in the arrays although I dare say the script won't work if you do.
I dont know enough about the actual workings of barcodes to be able to tell you where these values have come from however I imagine that they
are the only ones that will work to produce a Code128 barcode. You have not included the part of the script that involves the printing so I cant tell
you how to make the spaces bigger. If you would like to post the rest of the script I can have a look for you.

Dom
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
jacristobalAuthor Commented:
When I print I pass these values to the Barcode Function:
                 Barcode CodeType, UCase("(01)14807788" & Mid(txtDataItem, 2, 5) & "(10)" & txtDataLot), Printer, 24, 900, 1750, 1300

The Barheight is there, I always change that into lower value, but as far as I understand the code, it makes a image or pic, but when i do resize it the spaces becomes smaller, I'm only printing in an sticker/label size 76 x 194 mm, and I have to print 25 character.

Public Function Barcode(CodeType As String, strCode As String, pic As Object, barscale As Integer, barHeight As Single, StartX As Single, startY As Single)
Dim barWidth As Single, i0 As Integer, barStart As Single
Select Case CodeType
    Case "39": strCode = UCase(strCode): Code39 strCode
    Case "128": Code128 Replace(Replace(UCase(strCode), "(", ""), ")", "")
    Case "2/5": strCode = IIf(Len(strCode) Mod 2 > 0, strCode & "0", strCode): Code25 strCode
    Case "Codabar": strCode = UCase(strCode): Codabar strCode
End Select
barStart = StartX
For i0 = 1 To Len(sBar)
    barWidth = Mid(sBar, i0, 1) * barscale
    If i0 Mod 2 > 0 Then pic.Line (barStart, startY)-Step(barWidth, barHeight), vbBlack, BF
    barStart = barStart + IIf(i0 Mod 2 > 0, barWidth, barWidth * 1)
   
Next
pic.FontName = "Andale Mono": pic.FontSize = 10: pic.CurrentX = StartX: pic.CurrentY = (startY * 1) + barHeight: pic.Print strCode
End Function
0
 
domjCommented:
From what I can see there if you decrease barscale (24) to something smaller it should reduce the size
of the barcode horiontally however it sounds like you just want to make the spaces smaller which I dont
think you can do.

Dom
0
 
jacristobalAuthor Commented:
ok thanks a lot domj i'll give you the points more power, hope I can ask you more soon
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
domjCommented:
I think this one speaks for itself! ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now