# Synching a Query Database with an Open Database Viewer

A query database can be synched with an open database viewer. This is a powerful construct as you can write actions which employ query database functionality where the results are posted to an instance of the database open in the UI.

The synch capability is enabled by specifying a desire to grab an already open database via an Open Query Database statement. When a database is grabbed it is disabled as long as the grab persists. When the query database is closed, all modifications appear in the open databases's viewer.

There are special sematics when closing a query database which was opened with a grab. See the Close/Save Query Database statement for more information.

A query database which is grabbing an open database does not have a copy of the database data. It is always using the data store in the open database. As such, the database cannot save or discard changes by the Close/Save Query Database statement. The statement can only be used to close the query database which will release the grab. Note that all query databases are automatically closed when action execution terminates.

The non query Database Statements can be used to perform typical database functionality while the grab is in place. The only exception is that the Close Database statement will not work while the database is grabbed.

The Open Query Database statement sets two interesting named variables when a database is grabbed:

prefix-Grabbing
  If a grab was successful this item will be created with a value of 1. If you test the numeric value of this named variable you can determine if a query database successfully performed a grab
prefix-Selected Rows
  If a grab was successful this item will contain a list of indexes representing the rows selected in the grabbed UI database. If the open database was filtered, the row indexes will be adjusted to represent the indexes in the unfiltered database. Each index is preceded by a # so that the list represents absolute row references. The list is delimited by the default list delimiter (\~).

If an action is directly run from a Database Viewer, the action will automatically grab the database and open the query database. The opened query database has an identifying prefix of UI, is keyless and is opened as grabbing. Running an action this way eliminates the need of locating and opening the database in the action. Additionally two other named varables will be created:

UI-Clicked Column
  The name of the column which was clicked to display the context menu.
UI-Clicked Row
  The number of the row which was clicked to display the context menu. The row number is preceded by a # so that it is valid to use as a key. Note that this row will always be included in the UI-Selected Rows list.