Question & Answer
Start by opening a blank worksheet and selecting the Data ribbon, then 'Get Data', 'From Other Sources' and select 'From ODBC' as shown in Fig. 1a
Select the data source to connect to Db2 on IBM i and expand the 'Advanced options' twistie and type in your select statement in the 'SQL statement (optional)' section as shown in Fig. 1b.
Click OK and it makes an ODBC connection, run the query and display the results in a table as shown in Fig. 1c
Press 'Load' and it loads the data into the spreadsheet and saves the query in it as shown in Fig. 1d
Note the 'Queries & Connections' item to the right of the data. If you hover your mouse over the spreadsheet icon just to the left of the query name (it was given a default name of Query1), you get a pop-up window as shown in Fig. 1e. The options in this window provide functions to edit the query and do several other things that are beyond the scope of this technote. Note, there is also a refresh icon to the right of the query name and clicking it refreshes the data in spreadsheet. If you close the 'Queries & Connections' pane, you can reopen it using the 'Queries & Connections' item on the Data ribbon.
Selecting the EDIT option opens the dialog box shown in Fig. 1f. With this dialog you can modify the query, change the query name, and use the 'Advanced Editor' function (near the upper left) to edit the SQL statement.
Connect Without A DSN
In the previous example, a DSN was selected to define the connection. A DSN or data source Name can be thought of as a saved collection of default values for an ODBC connection to use. Ultimately, the application might override most any of the values you specify in a DSN so they aren't strict controls on the settings. While DSNs are a great way to get consistent behavior from one or more applications, they do require the step of creating the DSN. You can freely share a spreadsheet with an embedded data connection defined in it (like our first example). But the recipient must also have that DSN defined with the same name (and connecting to the same system). So you can see how a DSN is also a hindrance to the portability of the solution. Fortunately, we can define the connection without a data source. Then, we can share the spreadsheet with anyone with the ODBC driver installed on their system. They can refresh that data as needed.
We follow the same steps of selecting the option to get data from ODBC. But this time, select '(None)' for the data source and enter a connection string that defines the ODBC driver to use and the system to connect to at a minimum. The ODBC connection string keywords and their default values are defined in the IBM Documentation. Using this interface, you do not need to provide values for anything more that the Driver and System keywords because we are not using any catalog features to browse for files or columns. If you have data in character fields defined with CCSID 65535, add the Translate keyword value pair. This value causes that data to be converted from EBCDIC by using the job CCSID so that's in the example in Fig. 2a.
You might be prompted for an ID and password after you press OK and then you are presented with the query results as in Fig. 1c from the first example. Select the option to load the data in the spreadsheet
Let's move on to something a little more dynamic. Frequently an entire table is transferred to a spreadsheet and then several rows are removed because they weren't wanted in the first place. In other words, a selection criteria set on the query ensures that only certain rows are selected and returned. The value for that criteria can be specified dynamically to provide a user with the flexibility to define the limiting values at run time. This next example shows how to create a parameterized query.
For this case, the accounting team is sending refund checks to customers with large credit balances. I need to find data for every customer that has a credit due to them that is greater than the limit that the accounting team set. Presently the limit is $10 so I really don't want to see the accounts that do not have more than a $10 credit due. I might change the query and add a hardcoded selection criteria, 'Where CDTDUE >= 10'. But the amount is known to vary from month to month and at the end of the fiscal year, anyone with a credit due is paid back. I don't want to edit the query every time it changes. I'd want the query to prompt me for a value when it is refreshed. To do accomplish this goal, we have to create the query a bit differently, by using Microsoft Query.
To start, open a new spreadsheet and select the Data ribbon, select 'Get Data', 'From Other Sources', 'From Microsoft Query'. That opens the dialog box shown in Fig. 3a. Select your DSN and clear the box for 'Use the Query Wizard to create and edit queries'.
In this case, it helps to have the library with the table you are querying in the library list of the DSN. That way you can select the schema (library) that the table is in, select the table and click 'Add' then 'Close' in the dialog box shown in Fig. 3b.
Double-click the asterisk to populate the query with all of the columns, or select the columns you want in the order you want them. See Fig. 3c.
Press 'Properties' to decide how you want the parameter to be set. The 'Connection Properties' window opens. Check the 'Refresh data when opening the file' option on the 'Usage' tab (Fig 3f). This setting causes a prompt for a new value every time the spreadsheet is opened. Then, select the 'Definition' tab, press 'Parameters' to open the 'Parameters' window shown in Fig. 3g.
Another Way To Enter Parameters - In The Spreadsheet Itself
OK that works great but there are a couple of potential problems. When that spreadsheet opens, it prompts for the minimum credit due value but it doesn't suggest a default. If someone new is using this spreadsheet, it would be nice to see what the last value used was. The other issue is that the minimum credit values required for a refund check change only at the end of my fiscal year and even then, only for one month. How can I keep the flexibility of the parameterized query but not have to enter a value every time? One way is to pull the value from a cell on the spreadsheet itself and refresh the data only when that that value is changed or when I click 'Refresh All' on the Data ribbon.
To start, open a new spreadsheet and create an input cell and a description of what is expected in that cell. Excel also has some default format colors you can apply to the cells to alert the user that a cell is an input value. See Fig. 4a for details.
Proceed as in the previous example and this time when you return the data to Excel, the target location is under the input area (see Fig. 4b) and click 'Properties' again.
Click the 'Definition' tab in the 'Connection Properties' dialog box and click the radio button titled 'Get the value from the following cell:'. Click the arrow button under that text (right side of the input field) then click your input field in the spreadsheet and Excel automatically adds the proper syntax to refer to the cell. In my example that would be =Sheet1!$B$1 meaning the value from Sheet1 of the workbook, Cell B1 (see Fig. 4c)
30 June 2021