How to Change the Database Dynamically at Print Time
In this article, we will explore the set-up of BarTender using Form Actions to access a desired database to populate a template at print time.
Utilizing an example use-case, this article will be based on the following premises:
- Similar or the same database structure exists
- A text file database is used
BarTender 2019 and later
Applicable to Automation edition or higher
Setting up the document
To begin dynamically selecting the database to use at print time, we will need the following:
- To connect the BarTender document to a database -(see Video - Getting started with database connections. for additional information on this).
- As mentioned, we will use a text database in this example, allowing us to overwrite it.
- A Form to make the database selection at print time -(see How to Design a Data Entry Form for additional information on this)
- A Named Data Source that will store the value of the new database we want to use -(see Sharing Data between objects for additional information on this)
- Create the database files you would like to use. Ensure that the files have the same column structure. Database files should be .txt or .csv only for this procedure to be successful.
- Create a new .btw file and connect one of the new database files you created in step one.
- Add a text object to the template and connect its data source to a database field.
- Add a Named Data Source by right-clicking on Named Data Sources in the menu to the right, then select Create Named Data Source....
- Add a List Box Control and a Button Control to the Data Entry Form — access using the Data Entry Form icon in the toolbar.
- In the properties for Dropdown List 1, select List Item, then enable the choose File Names in Folder from the Source menu. Browse the location of the file you wish to use using the Select... button 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. (e.g., "*.txt" will only display TXT files). Activate the Use full path for item value option so that the whole path (including the name and file extension) will be used.
- Click Linked Data Source from the menu on the right and connect 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 Include Standard Buttons 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 will 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 will 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 selected 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:
- The 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.
Having trouble or receiving an error message?
Currently, some instances of BarTender may have difficulty with this process. We are aware and working to fix this issue as soon as possible. In the meantime, if you are having trouble or are experiencing an error message, please view our troubleshooting page to explore the workaround options.
- 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 file path for the btw file under Button properties > Button > Actions... > Print Document