Automating Database Printing

BarTender documents can be designed to read data from one or more database connections, such as text files or OLE DB and ODBC data sources. Database connection settings are configured by using the Databases collection object, which is a list of individual databases. Each Database object in the collection contains settings that are specific to the database type.

BarTender database connections can be controlled and modified by using ActiveX Automation. Many common methods for controlling database connections are supported, including the following:

Setting a File Name for a Text File Database Connection

The following example shows how to set a file name for a Text File database connection.

ClosedIn VB.NET

'Declare a BarTender application variable

Dim btApp As BarTender.Application

'Declare a BarTender document variable

Dim btFormat As BarTender.Format

'Declare a BarTender database variable

Dim btDb As BarTender.Database

'Create a new instance of BarTender

btApp = New BarTender.Application

'Set the BarTender application visible

btApp.Visible = True

'Open a BarTender document

btFormat = btApp.Formats.Open("c:\Format1.btw", False, "")

'Select the database

btDb = btFormat.Databases.GetDatabase(1)

'Set the database to demo.dat

btDb.TextFile.FileName= "c:\Demo.dat"

'End the BarTender process

btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges)

ClosedIn C#

// Declare a BarTender application variable

BarTender.Application btApp;

// Declare a BarTender document variable

BarTender.Format btFormat;

// Declare a BarTender database variable

BarTender.Database btDb;

// Create a new instance of BarTender

btApp = new BarTender.Application();

// Set the BarTender application visible

btApp.Visible = true;

// Open a BarTender document

btFormat = btApp.Formats.Open("c:\\Format1.btw", false, "");

// Select the database

btDb = btFormat.Databases.GetDatabase(1);

// Set the database to demo.dat

btDb.TextFile.FileName = "c:\\Demo.dat";

// End the BarTender process

btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);

Changing the SQL Statement

Custom SQL statements can be run on a connected database to perform record filtering when data is browsed or to select database records at print time. To apply a custom SQL statement, use the Database.SQLStatement property.

The following example shows how to set a custom SQL statement.

ClosedIn VB.NET

'Declare a BarTender application variable

Dim btApp As BarTender.Application

'Declare a BarTender document variable

Dim btFormat As BarTender.Format

'Declare a BarTender database variable

Dim btDb As BarTender.Database

'Create a new instance of BarTender

btApp = New BarTender.Application

'Set the BarTender application visible

btApp.Visible = True

'Open a BarTender document

btFormat = btApp.Formats.Open("c:\Format1.btw", False, "")

'Select the database

btDb = btFormat.Databases.GetDatabase(1)

'Set the SQL statement

btDb.SQLStatement = "SELECT * FROM 'Customers' WHERE 'Name'='Frank Wild'"

'End the BarTender process

btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges)

ClosedIn C#

// Declare a BarTender application variable

BarTender.Application btApp;

// Declare a BarTender document variable

BarTender.Format btFormat;

// Declare a BarTender database variable

BarTender.Database btDb;

// Create a new instance of BarTender

btApp = new BarTender.Application();

// Set the BarTender application visible

btApp.Visible = true;

// Open a BarTender document

btFormat = btApp.Formats.Open("c:\\Format1.btw", false, "");

// Select the database

btDb = btFormat.Databases.GetDatabase(1);

// Set the SQL statement

btDb.SQLStatement = "SELECT * FROM 'Customers' WHERE 'Name'='Frank Wild'";

// End the BarTender process

btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);

Setting Query Prompts

A query prompt, which is defined as part of the attached database, is used to select the records that are used when a print job runs. The Format object contains a list of all of the query prompts that are defined for a BarTender document. These are stored in the QueryPrompts collection object.

The following example shows the use of query prompts via ActiveX Automation.

ClosedIn VB.NET

'Declare a BarTender application variable

Dim btApp As BarTender.Application

'Declare a BarTender document variable

Dim btFormat As BarTender.Format

'Declare a BarTender query prompt variable

Dim btQueryPrompt As BarTender.QueryPrompt

'Create a new instance of BarTender

btApp = New BarTender.Application

'Set the BarTender application visible

btApp.Visible = True

'Open a BarTender document

btFormat = btApp.Formats.Open("c:\Format1.btw", False, "")

'Select the query prompt

btQueryPrompt = btFormat.Databases.QueryPrompts.GetQueryPrompt("Name")

'Set the value of the query prompt

btQueryPrompt.Value = "Jane Doe"

'End the BarTender process

btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges)

ClosedIn C#

// Declare a BarTender application variable

BarTender.Application btApp;

// Declare a BarTender document variable

BarTender.Format btFormat;

// Declare a BarTender query prompt variable

BarTender.QueryPrompt btQueryPrompt;

// Create a new instance of BarTender

btApp = new BarTender.Application();

// Set the BarTender application visible

btApp.Visible = true;

// Open a BarTender document

btFormat = btApp.Formats.Open("c:\\Format1.btw", false, "");

// Select the query prompt

btQueryPrompt = btFormat.Databases.QueryPrompts.GetQueryPrompt("Name");

// Set the value of the query prompt

btQueryPrompt.Value = "Jane Doe";

// End the BarTender process

btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);

Related Topics