Solved

Generate a barcode with a Code128 format

Posted on 2004-09-16
11
800 Views
1 Endorsement
Last Modified: 2013-11-18
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
Comment
Question by:jacristobal
  • 4
  • 3
11 Comments
 
LVL 3

Accepted Solution

by:
domj earned 50 total points
ID: 12081236
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
 

Author Comment

by:jacristobal
ID: 12081399
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
 
LVL 3

Expert Comment

by:domj
ID: 12081422
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
 

Author Comment

by:jacristobal
ID: 12081560
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:domj
ID: 12081604
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
 

Author Comment

by:jacristobal
ID: 12081709
ok thanks a lot domj i'll give you the points more power, hope I can ask you more soon
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 12082857
0
 
LVL 3

Expert Comment

by:domj
ID: 12559652
I think this one speaks for itself! ;-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

E-commerce is quite a gambling world, and you should never entrust your business to a lucky chance. In order to outrun your competitors in a race to attract as many customers as possible, you need to have a well thought-out strategy under your belt.…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now