Cara menggunakan excel odbc connection vba

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


Cara menggunakan excel odbc connection vba

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.


Cara menggunakan excel odbc connection vba

Click OK and it makes an ODBC connection, run the query and display the results in a table as shown in Fig. 1c


Cara menggunakan excel odbc connection vba

Press 'Load' and it loads the data into the spreadsheet and saves the query in it as shown in Fig. 1d


Cara menggunakan excel odbc connection vba

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.

Cara menggunakan excel odbc connection vba

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.

Cara menggunakan excel odbc connection vba

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.


Cara menggunakan excel odbc connection vba

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

Parameterized Query

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'.

Cara menggunakan excel odbc connection vba

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.

Cara menggunakan excel odbc connection vba

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.

Cara menggunakan excel odbc connection vba

Click 'Add Criteria...' option on the 'Criteria' menu. Add the criteria for your query. In my case, I want to be prompted for a value that the CDTDUE column must be greater than or equal to. So I select the CDTDUE field, the proper comparison operator from the list, and an open and closed bracket for the value as shown in Fig. 3d.

Cara menggunakan excel odbc connection vba

Click add and it prompts for a value, type one in and then press 'Close' and then select 'Return Data to Microsoft Excel' from the 'File' menu. I am presented with the 'Import Data' window shown in Fig. 3e.

Cara menggunakan excel odbc connection vba

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.

Cara menggunakan excel odbc connection vba
Cara menggunakan excel odbc connection vba

Note, by default, it selected the value you entered when Microsoft Query prompted you for a value to initially run the query. I want the query to prompt the user for a value every time. So I select the first option for how the parameter is to be obtained, 'Prompt for value using the following string:'. I enter a meaningful prompt string that the user is presented with. Finally, click 'OK' on this window and the preceding two windows to embed your query into Excel. Excel prompts you for a value and then it populates your spreadsheet with the query data (See Fig. 3h).

Cara menggunakan excel odbc connection vba

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.

Cara menggunakan excel odbc connection vba

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.

Cara menggunakan excel odbc connection vba

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)

Cara menggunakan excel odbc connection vba

Click the 'OK' buttons to close these windows and embed the data into the spreadsheet as shown in Fig. 4c

Cara menggunakan excel odbc connection vba

This solution allows me to easily view what the parameter value is and I can update it and rerun the query by typing the new value into the input cell and pressing enter. At the end of the year, I put in the value 0.01 and press enter to get rows for everyone that has a credit due. (See Fig. 4e).

Cara menggunakan excel odbc connection vba

Related Information

IBM i Access Client Solutions Data Transfer Download to Excel Spreadsheet

Transferring Data From Excel with Access Client Solutions

[{"Product":{"code":"SWG60","label":"IBM i"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Component":"Access Client Solutions","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"Version Independent","Edition":"","Line of Business":{"code":"LOB57","label":"Power"}}]