I'm starting to feel like a broken record saying this, but there are of course multiple ways to achieve this in QlikView. One method, and probably the most commonly used, would be to define the variables in a QlikView script file as follows:
SET vQVDPath = C:\QlikView\Data\QVDs\;
The script file can then be included in multiple QVWs by added the following line towards the start of each script:
This method is perfect, with one exception, to edit the variables a user would need at least some basic QlikView scripting experience. What if we could store the variables in a simple table, such as an Excel spreadsheet, and then load them into multiple QVWs from there. This would allow anyone to edit the spreadsheet and thus change where the QVDs are to be stored.
The method I'll show you below uses the QlikView Components (QVC) script library. If you are not familiar with QVC I highly recommend you take a look here. It provides a series of subroutines and functions intended to assist and speed up the development of QlikView applications. One of these subroutines, Qvc.PopulateVariables, does exactly what we require.
The first thing we need to do is to include the QVC script file into the application in the same way we included the script file above:
Next we load the variables into a table from the spreadsheet where they have been entered.
Variables: LOAD Name, Definition FROM [..\config\variables.xls] (biff, embedded labels, table is Sheet1$);
Whilst this example uses an Excel spreadsheet, the table can be loaded from any source such as an XML file, a CSV, a database, etc. What is important is that the table contains at least 2 fields, that the first contains the names of the variables to be created, and the second contains the values to be set to them. Once this table exists, you can call the Qvc.PopulateVariables routine to convert the table into variables.
CALL Qvc.PopulateVariables ('Variables', -1);
The first parameter is required and should be a string containing the name of the previously loaded table which contains the variable definitions. The second parameter is optional and defines whether the variables should be defined using SET (0) or LET (-1). With that complete, we can of course drop the variables table as it is now surplus to requirements.
DROP TABLE Variables;
We now have all our variables set in the application as we intended and can perform the same script snippet in each QVW where we need the same variables to be set.
One final trick, what if you wanted to load variables from another QVW file? Thankfully this can also be done. You might not be aware that a QVW file includes an XML footer. This XML contains many useful bits of information, including a list of the variables within the app and their values when the QVW was last saved. The following script can be used to load these variables from the XML of a QVW into a table.
Variables: LOAD Name, RawValue FROM [MyApp.qvw] (XmlSimple, Table is [DocumentSummary/VariableDescription]);
Once in a table, you can used the same QVC call as above to populate them into variables,