20
Sat, Apr
5 New Articles

Microsoft Computing: Microsoft Query

Microsoft
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Many Microsoft Office users are not aware that a helper utility that came with Office, MS Query, can be used as a standalone utility. This month, I'll explain how MS Query works and how you can use it with iSeries data.

Several Office applications--like Excel, Word, and Access--are capable of working with external data. Excel can import external data into a spreadsheet, Word can bring iSeries data into a document, and Access can link to and update iSeries files. Beyond these capabilities, however, MS Office applications can also perform more complex data selection, grouping, and updating, thanks to the full-function services provided by MS Query. MS Query, with its own set of screens, can be a valuable tool for iSeries data handling, especially for developing SQL statements and for ad hoc reporting.

Accessing iSeries Data with MS Query

As with other database utilities, access to iSeries data using MS Query is accomplished through ordinary ODBC or OLE DB/ADO communication services. Since MS Query is SQL-based, the ODBC method delivers more efficient performance (ADO is better for record-level access). Therefore, before you can access iSeries data with MS Query, you must install and configure the IBM ODBC driver for the iSeries. For purposes of this discussion, it is assumed that you're familiar with ODBC services and have configured an ODBC data source for accessing your iSeries data.

Where Is Microsoft Query?

Installing MS Query with the other Office applications is optional, and even when it is installed, Office does not give MS Query an icon or an entry in the Office program group. Apparently, Microsoft perceives MS Query only as an adjunct to other Office applications, not as a standalone utility.

MS Query is accessed through the program named msqry32.exe. On a Windows XP system with Office 2003, this program is located in C:Program FilesMicrosoft OfficeOFFICE11, but other installations will differ, so you may have to run a search.

If you don't have msqry32.exe on your system, you'll have to install it from your Office media. In the Office 2003 setup program, MS Query is listed under "Choose advanced customization of applications" and then "Office Tools." Select those options (or the equivalent) to display a screen of installation options (Figure 1). Specify that you'll be running Microsoft Query from your computer and click the Update button.

http://www.mcpressonline.com/articles/images/2002/Microsoft-QueryV4--12270400.jpg

Figure 1: Select Microsoft Query in the Office setup program. (Click images to enlarge.)

Once MS Query is installed and the location of program msqry32.exe is determined, create a shortcut to the program and put it on your desktop or your Start menu.

Developing SQL Statements with MS Query

One of the most useful aspects of MS Query is its transparent use of SQL statements to get at iSeries data. When you develop a query with the utility, SQL statements are being created by MS Query. Those SQL statements can then be studied to gain an understanding of SQL syntax for basic data access. Additionally, when more complex SQL statements are in order, syntax can often be manually developed by using an additional feature of MS Query: direct SQL submission. To illustrate the first feature, we'll create a simple query using the Query Wizard that comes with MS Query.

When you launch MS Query, you'll be presented with the main interface screen (Figure 2). From here, you can create a query and save it for use in other Office applications (since MS Query is the data access engine used by Excel and Word,) or you can submit SQL statements to the iSeries interactively as you would for an ad hoc report.

http://www.mcpressonline.com/articles/images/2002/Microsoft-QueryV4--12270401.jpg

Figure 2: This is the main user interface in MS Query.

Click on "File" and then select "New...". You will be asked to specify the name of the ODBC data source you configured previously. Before you click the OK button, however, make sure the option labeled "Use the Query Wizard to create/edit queries" is checked.

When you click the OK button, MS Query will submit a request to the iSeries for a listing of the files contained in the libraries specified in your ODBC data source (Figure 3).

http://www.mcpressonline.com/articles/images/2002/Microsoft-QueryV4--12270402.jpg

Figure 3: Select iSeries files and fields in the Query Wizard.


Select a file in the left panel and click the plus sign (+) to list the fields. Select the fields you're interested in and click the arrow button (>) to move the fields to the right panel. If you select multiple files without matching key field names, you'll specify how they're to be linked in a moment.

Once you've made your selections, click the Next button. Depending on the names of your fields, you may be notified that MS Query can't automatically determine how the files should be linked, but just click OK to go on. You should then be greeted with a grid in the Query screen along with images representing your selected files (Figure 4.)

http://www.mcpressonline.com/articles/images/2002/Microsoft-QueryV4--12270403.jpg

Figure 4: MS Query shows your files and their associated data.

In Figure 4, two iSeries files are represented in the boxes: CUST and INVOIC. How these files are linked is shown by the black line that is drawn from CUNUMB in the CUST file to INCUST in the INVOIC file. This link had to be established manually by dragging the field in file 1 and dropping it onto the field name displayed in file 2. When you do so, the line will be drawn and the data in the grid will be updated to reflect the result of your effort.

Since your request for data is submitted to the iSeries in the form of an SQL statement in more-or-less plain English, let's take a look at the statement as generated by MS Query. Click the button labeled SQL in the Query toolbar. The SQL statement will be displayed in a box (Figure 5). In the SQL statement shown in the figure, you can see the basic structure of a data request over two files--how the fields are selected and how the files are linked.

http://www.mcpressonline.com/articles/images/2002/Microsoft-QueryV4--12270404.jpg

Figure 5: The MS Query Wizard generated this SQL statement.

To take the illustration one step further, click on the Criteria menu and then select Add criteria... Specify a field within your query and give it a selection criterion to be used to filter records. If you now click the SQL button, you'll see that the selection criteria clause has been added to your SQL statement.

Advanced SQL Statements

Sometimes, the default SQL statement produced by the Query Wizard is not correct for your needs and a more direct approach is in order. To this end, MS Query allows you to key your own SQL statement and submit it to the iSeries for execution. This technique is especially helpful for developing and testing complex forms joining files or doing selects on selects.

As an example, suppose I need to see all customer records and their invoices, but I also want to see customer records for those customers that do not have any invoices. By default, SQL will show only customers that have an invoice to link to. To remedy this shortcoming, a more advanced SQL statement that uses a left outer join technique is available.

To illustrate this method, start a new MS Query session, open the File menu, and select Execute SQL.... You'll be asked to identify your ODBC data source (click on the Data Sources... button). Once you're done, you may key an SQL statement for direct submission to your iSeries (Figure 6.)

http://www.mcpressonline.com/articles/images/2002/Microsoft-QueryV4--12270405.jpg

Figure 6: Submit a left outer join SQL statement directly to the iSeries.

Click the Execute button to submit the statement to the iSeries. If all is well, your data will populate the Query data grid.

Executing a Stored Procedure with MS Query

One more feature of MS Query merits presentation. Query allows you to test the result of running a stored procedure that is resident on your iSeries. For example, suppose I've created a stored procedure on my iSeries named CUSINQ. The stored procedure will return a record set in the form of a file cursor to my client application. To check the validity of my stored procedure, I can step through the process and look at the output with MS Query.

From the Execute SQL screen in MS Query, you can submit the name of a stored procedure for execution on the iSeries and then see the resulting record set. Note that the stored procedure must be in an iSeries library that is listed in your ODBC data source configuration.

In the SQL Execute screen, key "Call" and the stored procedure name inside curly braces (e.g., {Call CUSINQ}). Then click the Execute button. Your request will be sent to the iSeries, and the record set cursor will be returned. MS Query will accept the cursor and step through the records automatically, and the transferred data will be passed into the Query data grid for display.

All in all, MS Query can be a good friend of the iSeries developer--and with very little fuss. Further, since MS Query is the data access engine used in Excel and Word, an understanding of the services provided and the way SQL behaves can be a real benefit.

Chris Peters has 27 years of experience in the IBM midrange and PC platforms. Chris is president of Evergreen Interactive Systems, a software development firm and creators of the iSeries Report Downloader. Chris is the author of The OS/400 and Microsoft Office 2000 Integration Handbook, The AS/400 TCP/IP Handbook, AS/400 Client/Server Programming with Visual Basic, and Peer Networking on the AS/400 (MC Press). He is also a nationally recognized seminar instructor. Chris can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it..

Chris Peters has 32 years of experience with IBM midrange and PC platforms. Chris is president of Evergreen Interactive Systems, a software development firm and creators of the iSeries Report Downloader. Chris is the author of i5/OS and Microsoft Office Integration Handbook, AS/400 TCP/IP Handbook, AS/400 Client/Server Programming with Visual Basic, and Peer Networking on the AS/400. He is also a nationally recognized seminar instructor and a lecturer in the Computer Science department at Eastern Washington University. Chris can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it..


MC Press books written by Chris Peters available now on the MC Press Bookstore.

i5/OS and Microsoft Office Integration Handbook i5/OS and Microsoft Office Integration Handbook
Harness the power of Office while exploiting the i5/iSeries database.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: