How To: Querying a Database in Excel Document 2013 Thor Kolner. All rights reserved. Challenge: A project manager was wasting time entering information into Excel that had already been entered into his company s accounting software. Each week, he printed extensive reports, and then manually reentered the data into his workbooks. He was frustrated with the amount of his time it took to perform this manual task. Additional time was lost as he occasionally made mistakes when typing the information into Excel. Solution: Information in a database may be transferred to a new or existing Excel workbook through the use of a query. A query is initiated in Excel and draws information directly from the database into a worksheet. No additional data entry is required. In Microsoft Excel, the Query Wizard may be used to select specific rows and columns from a database table. The limited data will then be pulled directly to the workbook. If the project manager chooses to save the workbook containing a query, the workbook may be reopened at a later time and the query can be refreshed by clicking a single button. After learning how to create a query in his workbook, the project manager was able to eliminate the hours lost each week rekeying database information into Excel. The query also eliminated any chance of making a fat finger mistakes. Skills Required: A. Experience using Microsoft Excel. B. An ODBC connection to the accounting database. C. Security clearance to query data from a database into Excel. 1 P a g e t h o r k o l n e r. c o m
Try It! In the ConstructionWorksLT database, the Employee_Master tab contains basic information about each employee. Because this information is stored in a database, it may be queried into a workbook. 1. Open a new Excel workbook. Click the Data tab. 2. In the Get External Data group, click From Other Sources, and then From Microsoft Query. 2 P a g e t h o r k o l n e r. c o m
3. In the Choose Data Source window, click Excel Files*, and then click OK. 4. On the right side of the Select Workbook window, navigate to your Downloads folder. On the left side of the window, select the constructionworkslt.xls workbook, and then click OK. 3 P a g e t h o r k o l n e r. c o m
5. The Query Wizard displays. On the list of Available tables and columns, expand the Employee_Master table by clicking the plus sign (+) next to the table name. Note: If the list of Available tables and columns is blank, click Options and select the checkbox next to Tables, Views, System Tables, and Synonyms, and then click OK. 4 P a g e t h o r k o l n e r. c o m
6. Add the following columns to the list of Columns in your query by selecting the column name in the box on the left side of the window and dragging it to the box on the right. 7. In the box on the right, select the column Title and move it to the top of the list by clicking the up arrow button on the right side of the window. 5 P a g e t h o r k o l n e r. c o m
8. Click Next to advance to the Filter screen. In the Column to filter box, select Employment_Status. Only include rows where Employment_Status equals Active. 9. Click Next to advance to the Sort Order screen. In the first Sort By list, select Last_Name and Ascending. 6 P a g e t h o r k o l n e r. c o m
10. Click Next to display the Finish screen. Select the option to Return Data to Microsoft Excel, and then click Finish. 11. The Import Data window displays. Put the data in the Existing worksheet, click a cell in the worksheet where you wish the first field of the data set to display, and then click OK. 7 P a g e t h o r k o l n e r. c o m
The queried information displays in the worksheet. The information is limited to active employees and sorted by the Last_Name column. To update the information in the workbook, click the Data tab. In the Connections group, click Refresh All, and then Refresh 8 P a g e t h o r k o l n e r. c o m
Recap: A query is a method for drawing information directly from a database into an Excel worksheet. The database is called a data source in Excel. The Query Wizard guides you through the process of selecting one or more columns from a data file, filter out unwanted rows of information, and sorting the results. The workbook may be saved and then reopened at a later date. The information queried from the database may be updated at any time by refreshing the query. 9 P a g e t h o r k o l n e r. c o m