FAQ TBarCode OCX
Barcodes in Crystal Reports 8
Barcodes in Crystal Reports 8
It isn't possible to add TBarCode directly into a report, but you can create a barcode image “on the fly” and load it to a Picture Box. Follow the instructions below to see how embedding of TBarCode into Crystal Reports 8 works.
Embedding via Picture Box (VB6 sample)
In a form (named “Form1”) embed a TBarCode Control (named “tbc”) and a Picture Box (named “TmpPicture1”). Then add the function code below to the form. It creates a barcode image „on the fly“ and uses the PictureBox as drawing surface.
Public Function BarcodeGenerate(Id As String, Width As Long, Height As Long) As PictureBox
Dim nSizing, cm
tbc.Text = Id
cm = tbc.CountModules
TmpPicture1.Cls
TmpPicture1.ScaleMode = vbPixels
Form1.ScaleMode = vbPixels
Width = ScaleX(Width, vbTwips, vbPixels)
Height = ScaleY(Height, vbTwips, vbPixels)
nSizing = Int(Width / cm)
If nSizing < 1 Then nSizing = 1
TmpPicture1.Width = cm * nSizing
TmpPicture1.Height = Height
DoEvents
tbc.BCDraw TmpPicture1.hDC, 0, 0, TmpPicture1.Width, TmpPicture1.Height
Set BarcodeGenerate = TmpPicture1
End Function
In the Report event code (Format event) call the barcode generator function and assign the newly created barcode image to the Picture Box (named Picture1) in the report.
Private Sub Section3_Format(ByVal pFormattingInfo As Object)
'create the barcode for each record set
Dim data As String
data = Field1.Value
Set Me.Picture1.FormattedPicture =
Form1.BarcodeGenerate(data, Me.Picture1.Width, Me.Picture1.Height).Image
End Sub
Embedding via Bitmap File (VB6 sample)
In a form (named “Form1”) embed a TBarCode Control (named “TBarCode”) and adjust the barcode type and all other settings as desired. This barcode object will be used during report runtime for barcode creation “on the fly”.
In the Report event code (Format event) create a dynamic barcode image in a temp folder and load this image to the Picture Box (named pictBarCode) in the report.
Private Sub Section3_Format(ByVal pFormattingInfo As Object)
' Simulate data binding
' by saving the barcode as bitmap with data from fldArticleID
' then reload it to a picture control
On Error Resume Next
Dim nWidth
Dim path
Dim fso
' here we use hard coded temp path (must exist!)
path = "c:\temp\CR8_" & Me.fldArticleID.Value & ".bmp"
' reference the TBarCode Control in the Form1
Form1.TBarCode.Text = Me.fldArticleID.Value
Form1.TBarCode.PrintDataText = False
nWidth = Form1.TBarCode.CountModules * 3 'adapt width to number of graphical modules
Form1.TBarCode.SaveImage path, eIMBmp, nWidth, 100, 96, 96
Me.pictBarcode.SetOleLocation (path)
End Sub
If you don’t want to add a barcode control on the form you can create an instance of TBarCode also in memory and apply the settings programmatically.