Questions? Search our site or call us at 1-800-722-6004

Using Barcode ActiveX with a VB Data Report

Skip To:

Creating the Data Environment
Creating the Data Report
Adding Barcodes to the Report
Conclusions
Using the Sample Project

The following example walks you through the process of creating a simple report based on the NorthWind Database that comes with Visual Basic. Before you begin the step-by-step process, ensure that the Northwind database (Nwind.mdb) is present on your computer. If it is not present, copy the file from your Visual Basic CD onto your hard disk.

Creating the Data Environment
  • In Visual Basic, create a new Standard EXE project.
  • On the Project menu, click Add Data Environment to add a designer to your project. If the designer is not listed on the Project menu, click Components. Click the Designers tab, and click Data Environment to add the designer to the menu. Note: The first four kinds of ActiveX designers loaded for a project are listed on the Project menu. If more than four designers are loaded, the later ones will be available from the More ActiveX Designers submenu on the Project menu.
  • On the Data Link Properties dialog box, click Microsoft Jet 3.51 OLE DB Provider.

  • Click the Next button to get to the Connection tab.

  • Click the ellipsis button (…) next to the first text box and use the Select Access Database dialog box to navigate to the nwind.mdb file, which is installed in the Program Files\Microsoft Visual Studio\Vb98 directory. Then click OK to close the dialog box.
  • Right-click the Connection1 icon, and click Rename. Change the name of the icon to Northwind.

  • Right-click the Northwind icon, and then click Add Command to display the Command properties dialog box. In the dialog box, set the properties as shown below:

  • Click OK to close the dialog box. After closing the dialog box, the Data Environment designer reflects the Customers Table:

  • Set the properties of the project and designer according to the settings below, then save the project:
    Object Property Setting
    Project Name prjNwind
    DataEnvironment Name deNwind
    Form Name frmShowReport

Creating the Data Report

  • Once the Data Environment designer has been created, you can create a data report. This sample creates a report that could be used to print a data report that could be used as fax cover sheet or a form letter or any other single record data report.
  • On the Project menu, click Add Data Report, and Visual Basic will add it to your project. If the designer is not on the Project menu, click Components. Click the Designers tab, and click Data Report to add the designer to the menu.
  • Set the properties of the DataReport object according to the table below:
    Property Setting
    Name rptNwind
    Caption Northwind Data Report
  • On the Properties window, click DataSource and then click deNwind. Then click DataMember and click Customers.
  • Important: To set the DataSource property to deNwind, the Data Environment designer must be open. If it is closed, press CTRL+R to display the Project window, then double-click the data environment icon.
  • From the Data Environment designer, drag the ContactName field (under the Customers command) onto the Report Header section of the report designer.
  • Delete the Text Box control (rptTextBox) named txtContactName and rename the Label control (rptLabel) named Label1 to lblContactName. Do the same for the CompanyName, Address, City, Region, Postal Code and Country fields. Finally add a picture control that will be used to display the bar code. Name the Picture control (rptImage) ImgBc.
  • Ordinarily the controls would be inserted into the Details section, the labels deleted and the text boxes left behind so that when you run the report all the records would show up on the report, but for this example we only want the report to display a single record at a time, and will set the caption properties of these labels using code when we display the report. We only want to display one record at a time because the Picture control can only be set once and if we displayed all the records they would all have the same bar code!
  • It's important to resize the height of the report sections to be as short as possible because the height will be multiplied for any extra space below or above the section will result in unneeded space in the final report. After inserting all the Labels and the Picture control your report should look something like this:

Adding Barcodes to the Report

  • Open frmShowReport and make it the Startup Object for the Project. Resize it to be about 5700 High x 7000 Wide. From the Data Environment, drag the Customers Command onto the form. This will create text boxes and lables for all of the fields in the Customers table. Move them into position on the form.
  • Next add the TAL Bar Code control to the project and the form. To add it to the project click on Components and select it from the list:

  • Then draw it onto the bottom of the form. Set the Symbology Property to 8 - bcPostNET. Set the Data Bindings properties as follows, then click OK:

  • Next we need to add some buttons to allow us to navigate through the records and to display the report. Add 7Command Buttons and a text box with the following Properties:
    Control Name Caption
    cmdFirst |<
    cmdLast >|
    cmdNext >
    cmdPrevious <
    cmdExit Exit
    cmdReport Preview
    cmdPrintAll PrintAll
    txtPosition  
  • Your form should now look something like this:

  • Now it needs some code:

    Sub Position()
    '******** Code in this section pertains to updating position display **********
      frmShowReport.txtPosition.Text = "Record " & deNwind.rsCustomers.AbsolutePosition & _
    " of " & deNwind.rsCustomers.RecordCount
    End Sub
    
    Private Sub cmdPrevious_Click()
    '******** Code in this section pertains moving to previous record **********
      deNwind.rsCustomers.MovePrevious
      If deNwind.rsCustomers.BOF Then
        deNwind.rsCustomers.MoveNext
      End If
      Position
    End Sub
    
    Private Sub cmdNext_Click()
    '******** Code in this section pertains moving to next record **********
      deNwind.rsCustomers.MoveNext
      If deNwind.rsCustomers.EOF Then
        deNwind.rsCustomers.MovePrevious
      End If
      Position
    End Sub
    
    Private Sub cmdLast_Click()
    '******** Code in this section pertains moving to last record **********
      deNwind.rsCustomers.MoveLast
      Position
    End Sub
    
    Private Sub cmdFirst_Click()
    '******** Code in this section pertains moving to first record **********
      deNwind.rsCustomers.MoveFirst
      Position
    End Sub
    
    Private Sub cmdExit_Click()
    '******** Code in this section pertains to closing the application **********
      Unload Me
    End Sub
    
    Private Sub cmdReport_Click()
    
      ' Make sure we have the latest info
      deNwind.rsCustomers.Requery
    
      ' Assign values to the labels in the report based on the current record
      ' displayed on the form
      With rptNwind
        .Sections("ReportHeader").Controls("lblContact").Caption = frmShowReport.txtContactName.Text
        .Sections("ReportHeader").Controls("lblCompany").Caption = frmShowReport.txtCompanyName.Text
        .Sections("ReportHeader").Controls("lblAddress").Caption = frmShowReport.txtAddress.Text
        .Sections("ReportHeader").Controls("lblCity").Caption = frmShowReport.txtCity.Text
        .Sections("ReportHeader").Controls("lblRegion").Caption = frmShowReport.txtRegion.Text
        .Sections("ReportHeader").Controls("lblZip").Caption = frmShowReport.txtPostalCode.Text
        .Sections("ReportHeader").Controls("lblCountry").Caption = frmShowReport.txtCountry.Text
    
    'set the picture box to the picture property of the ActiveX
        Set .Sections("ReportHeader").Controls("ImgBc").Picture = frmShowReport.TALBarCd1.Picture
    '.Orientation = rptOrientPortrait 'rptOrientLandscape
    '.PrintReport True
        .Show 'show the report
      End With
    End Sub
    
    Private Sub Form_Load()
      Position
    End Sub

Conclusions

Does this mean that I have to print each label one at a time?

Yes and No. The image control in a Data Report cannot be data bound to an OLE field in a table, and if you try to display all your records on the report you will get the same bar code image for all the records. However, you can write a routine that uses a loop to print out a number of single record reports. By adding a button called cmdPrintAll with the following code you could print a report for every record in your table:

Sub cmdPrintAll_Click()

'Loop until the end of the table
Do Until deNwind.rsCustomers.EOF

' Assign values to the labels in the report based on the current record

With rptNwind
.Sections("ReportHeader").Controls("lblContact").Caption = frmShowReport.txtContactName.Text
.Sections("ReportHeader").Controls("lblCompany").Caption = frmShowReport.txtCompanyName.Text
.Sections("ReportHeader").Controls("lblAddress").Caption = frmShowReport.txtAddress.Text
.Sections("ReportHeader").Controls("lblCity").Caption = frmShowReport.txtCity.Text
.Sections("ReportHeader").Controls("lblRegion").Caption = frmShowReport.txtRegion.Text
.Sections("ReportHeader").Controls("lblZip").Caption = frmShowReport.txtPostalCode.Text
.Sections("ReportHeader").Controls("lblCountry").Caption = frmShowReport.txtCountry.Text

'set the picture box to the picture property of the ActiveX
Set .Sections("ReportHeader").Controls("ImgBc").Picture = frmShowReport.TALBarCd1.Picture
'.Orientation = rptOrientPortrait 'rptOrientLandscape
.PrintReport True
End With

'Move to the next Record
deNwind.rsCustomers.MoveNext

Loop

End Sub

If you are printing labels then this will not be the tool for you - all your reports would print on a new label sheet in the same label position and would be extremely wasteful, but for printing full page reports with a single bar code on them then this could be a feasable solution.

Does this mean that I can only have one bar code on my report?

If you need two or three bar codes on your report, then you could conceivably insert 3 picture controls, and in the On Click Event of the button that displays or prints the report, use code to set the message property of the ActiveX control, save the image to disk as a Windows Metafile, then set the first image property to that image, reset the message, save another file, set the second image... etc.:-

Sub cmdPreviewMulti_Click()

' Assign values to the labels in the report based on the current record

With rptNwind
.Sections("ReportHeader").Controls("lblContact").Caption = frmShowReport.txtContactName.Text
.Sections("ReportHeader").Controls("lblCompany").Caption = frmShowReport.txtCompanyName.Text
.Sections("ReportHeader").Controls("lblAddress").Caption = frmShowReport.txtAddress.Text
.Sections("ReportHeader").Controls("lblCity").Caption = frmShowReport.txtCity.Text
.Sections("ReportHeader").Controls("lblRegion").Caption = frmShowReport.txtRegion.Text
.Sections("ReportHeader").Controls("lblZip").Caption = frmShowReport.txtPostalCode.Text
.Sections("ReportHeader").Controls("lblCountry").Caption = frmShowReport.txtCountry.Text

'set the message property of the activex
TALBarCd1.Message = frmShowReport.txtCompanyID.Text

'Save the Image to disk
TALBarCd1.SaveBarCode "C:\BarCode1.wmf"

'set the picture box to display the image
Set .Sections("ReportHeader").Controls("ImgBc1").Picture = LoadPicture("C:\BarCode1.wmf")

'reset the message property of the activex
TALBarCd1.Message = frmShowReport.txtCompanyName.Text

'Save the Image to disk
TALBarCd1.SaveBarCode "C:\BarCode2.wmf"

'set the picture box to display the image
Set .Sections("ReportHeader").Controls("ImgBc2").Picture = LoadPicture("C:\BarCode2.wmf")

'.Orientation = rptOrientPortrait 'rptOrientLandscape
'.PrintReport True
.Show 'show the report
End With

End Sub

Using the Sample Project

System Requirements

  • Microsoft Visual Basic 6.0
  • TAL Bar Code ActiveX Control 1.0/2.0/Demo
  • Microsoft Windows 95 or later
  • Winzip or other archive extractor to unzip the project files.
  • Download the Sample Project

Some Points to consider

  • You may need to adjust the loaction of the Nwind Database in the Data Environment if it was not installed in the default location on your PC (C:\Program Files\Microsoft Visual Studio\Vb98).
  • There are probably several other ways to do this!

Categories: Tutorial / How-To, Visual Basic 6.0, C/C++, .NET, Barcode ActiveX

Last Updated: 2011.07.06

Need more help?

Don't hesitate to call or email us with your questions

Our office is open 9AM - 5PM Monday Through Friday (E.S.T.)

Technical Support: 215-496-0202

Toll-Free: 1 (800) 722-6004
Skype: taltech1 (Voice only)
Email: support@TALtech.com