Visual Basic programs can create and use COM object directly. It does not take much time to add a text box into a Visual Basic form, retrieve the barcode string from the encoder and display it on the screen. On the other hand, unlike Access, Visual Basic does not provide a decent method to print form or reports. You need to write the printing code all by your self.
Beginning from version 3.30, a sample VB 6.0 project is included in the software. If you have difficulties printing data matrix bar codes, see the code we provided.
We will briefly go over some printing tips in the next section. The printing code uses the Printer object provided by VB6 for printing. If you use a third party printer object for printing, consult vendor documentation.
The ScaleMode determines the measurement unit you are going to use. You can set the scale mode to pixels, twips, inches, centimeters, points, character, or millimeters. Because resolution varies from device to device, the pixels scale mode is not portable among devices. 100 pixels appear much longer on a screen than on a printer.
When you print to a form or picture box on screen, the image
appears immediately. This is not the case for
printers. You must explicitly tell printer
where a page stops.
Call Printer.EndDoc method when you finish
all drawings.
If you'd like to print barcode at a specified location,
let's say 6 inches down from the top and 1 inch over from the left,
you need to set the currentx and
currenty properties accordingly,
as follows:
printer.scalemode = vbinches printer.currentx = 1.0 printer.currenty = 6.0
Visual Basic moves printer cursor down a line after executing a print statement. Unfortunately VB always moves the cursor to the left side of the printer. To have the lines started from the same currentx, you need to save this property and set it before calling Print:
pos_x_save = printer.currentx printer.print "line 1" printer.currentx = pos_x_save printer.print "line 2" printer.currentx = pos_x_save
The Print method does not recognize
line feed and carriage
return characters. To print multiple lines you need to call
the Print method multiple times, with each call printing a single line.
Since the DataMatrix Font Encoder returns the whole result in
a single string, you need to write routines parsing the result into
lines. See the code below:
Dim separator As String Dim lines() As String separator = vbCrLf lines() = split(encoder_results, separator) MsgBox "Total " & UBound(lines) & "lines."
The code below prints a Data Matrix barcode at a point 1.5 inches to the left edge of the page, 3.0 inches from the top. The barcode string is stored in an edit control called BarcodeEdit. Replace BarcodeEdit.Text with your barcode string.
Private Sub Command_Print_Barcode_Click() 'To print the barcode, we recommend using the built-in Printer object 'Do not print to a picture box and transfer it to printer. That results 'in a low quality barcode. 'You can change it to other values to adjust the barcode size. Printer.FontSize = 12 Printer.FontName = "MRV DataMatrix" 'We use inch as measurement unit here. Printer.ScaleMode = vbInches 'We want to print the barcode at point (1.5 inch, 3 inch) 'Note - printer usually has a margin 0.25" at four directions. 'You may consider this fact when you set the printer cursor Dim StartX, StartY As Double StartX = 1.5 StartY = 3# Printer.CurrentX = StartX Printer.CurrentY = StartY 'The Priner always go back to the left edge of the page after 'executing a print statement. Unless we print the barcode to 'the left ledge of the page, 'we have to parse the barcode string into lines, and print 'them one by one. 'Fortunately, VB provides a nice helper function for this purpose. Dim separator As String Dim lines() As String separator = vbCrLf lines() = Split(BarcodeEdit.Text, separator) 'Now we do the actual print line by line Dim i As Integer For i = 0 To UBound(lines) - 1 Printer.Print lines(i) 'Note: the Printer always go back to the beginning of the next line 'after executing Print. We need to set CurrentX every time after 'printing a line Printer.CurrentX = StartX Next 'EndDoc must be called to actual print Printer.EndDoc End Sub