How to change the database dynamically at print time
Overview
The main goal of this article is to show a way to set up BarTender using Form Actions, so that the user can select the database they want to use to populate their template at print time.
This article is based on the following premises:
- The database structure will be the same
- A text file database is used
Applicable to
BarTender 2019 and later
Applicable to Automation edition or higher
Information
Setting up the document
To start working on our requirement: dynamically selecting the database to use at print time, we will need the following:
- Connect our BarTender document to a database - Video - Getting started with database connections. As mentioned, we will be using a text database (so that we will be able to overwrite it)
- A Form (so that the user is able to make the database selection at print time) - How to Design a Data Entry Form
- A Named Data Source that we will use to store the value of the new database we want to use - Sharing Data between objects
Example
You can find example files at the end of this article
- Create the database files you want to use. Make sure that both files have the same column structure
The database files should be of type txt or csv for this procedure to work - Create a new btw file and connect one of the database files to it:
- Add a text object to the template and connect its datasource to a database field:
- Add a Named Data Source by right clicking on Named Data Sources in the right hand side menu and selecting Create Named Data Source. Name the Data source Database:
- Add a List Box Control and a Button Control to the Data Entry Form:
- Under the Dropdown List properties > List Item, enable the option "File Names in Folder" and add the folder containing the database files. This configures the Listbox to show all the objects within a Folder and, if required, it can be configured to filter out the type of objects available by specifying a file pattern. (ex.: "*.txt" will only display TXT files). Also activate the 'Use full path for item value' option, so that the whole path (including the name and file extension) will be used:
- Under Linked Data Source the dropdown control can be connected to the Named Data Source (in this example, it is called Database). Click Close to apply the changes:
- Since the action will be bound to a custom button control we need to disable the default buttons for the form by unchecking the following option in the Form Properties:
- Define the set of actions that we want to execute whenever the button is pressed from Button Properties > Button > Actions...:
- Add a 'Print Document' action and add the name of the btw file as the one to be printed (this functions similarly to integrations):
- Using the Print action enables us to use the Database Overrides section from within the Print Options tab. Here we configure the new database that we want to use:
- Make sure that the Data Source is set to FILE and NOT to VARIABLE
- The new database is going to be stored in the %Database% variable we are using on our form to store the value of the Dropdown selection. (since the 'Use full path for item value' option was used in the dropdown control the exact path is already stored in the variable)
- At this point the action should work as intended. However, when executed the form does not provide feedback on whether the program has finished processing the print job and is ready to start a new one. For this reason, we can configure an additional action in our PRINT button:
- This 'Show Form' action calls the form we are using at print time (in this case Form 1), so that it emulates the 'close and re-open' behavior we usually expect when printing:
Additional resources
Additional articles
Files
- Database files: Chairs.txt Sofas.txt
- BTW file: Furniture.btw
You will need to adjust the following file paths after downloading these files:
- The Database folder scanned under Dropdown Properties > List Items > Folder
- The Database file connected to the template under Database Setup > Filename
- The filepath for the btw file under Button properties > Button > Actions... > Print Document