Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


July 02, 2008

Using ADO to Access Excel, Part 2

Query, sort, search, and modify your data
RSS
Subscribe to Windows IT Pro | See More ActiveX Data Objects (ADO) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

ActiveX Data Objects (ADO) is useful for accessing various types of databases. For example, you can use ADO within VBScript to connect to and access data contained in Microsoft Excel spreadsheets. In "Using ADO to Access Excel, Part 1," (InstantDoc ID 99123), I presented the script ADOExcel.vbs that demonstrates how to use ADO to access Excel. Now, I'll expand on that basic information to explain ADO cursor types, describe how to sort a recordset, and show you how to use a universal data link (.udl) file as your ADO connection.

Cursor Types

The ADO CursorType property specifies how you move through a recordset and whether changes made to a database are visible after you retrieve the recordset. The four main cursor types are adOpenDynamic, adOpenForwardOnly, adOpenKeyset, and adOpenStatic.

adOpenDynamic. This cursor type allows all types of movement through the recordset. In addition, it lets you view additions and changes made by other users. Use adOpenDynamic if multiple users will need to modify rows in the database at the same time. The adOpenDynamic constant value is 2.

adOpenForwardOnly. This cursor type behaves like a dynamic cursor type, except that you can only scroll forward through a recordset. In other words, you can only use the MoveNext method within a script. The adOpenForwardOnly cursor type is very efficient and can improve performance when carrying out sequential reads of a data source. Use this cursor type if you need to quickly run through a recordset from top to bottom—but realize that you're limited to a single pass through the recordset. The adOpenForwardOnly constant value is 0.

adOpenKeyset. This cursor type also behaves like a dynamic cursor type, except that you can't see records that other users add—nor can you access records that other users delete. Data changed by other users is still visible. The adOpenKeyset constant value is 1.

adOpenStatic. This cursor type returns a static copy of a recordset and allows for all types of movement through the recordset. However, additions, changes, and deletions by other users aren't visible. The adOpenStatic constant value is 3.

Not all cursor types are supported by all providers. In some cases, certain providers might even change the cursor type depending on the initial CursorLocation and CursorType property settings. As you experiment with various combinations of settings, you might want to set up a simple MsgBox function that tells you various properties' settings after you call your Open method. For instance, you could immediately follow your Recordset object Open call with the following statements to see the actual CursorLocation and CursorType values:

MsgBox "CursorLocation" & _
  oRS.CursorLocation
MsgBox "CursorType" & oRS.CursorType

Sorting a Recordset

The cursor types will be especially important if you decide to sort recordsets within your scripts. To use the Sort method of a Recordset object, either the Connection object's CursorLocation property or the Recordset object's CursorLocation property must be set to client-side. By default, CursorLocation is set to server-side, which doesn't allow sorting. The CursorLocation property defines where a recordset is created when it's opened. Using client-side cursor locations can be faster if you're simply reviewing data, because the script doesn't have to access the server after the recordset is created, except when the Update method is called. But using server-side cursor locations can be faster if you have poor network performance and you're making a lot of updates, because most of the processing takes place on the server (although you can't use the Sort method).

When you use a client-side cursor location, data is actually disconnected from the source database. ADO retrieves the selection query and copies the recordset into the client's memory. Then when you issue a Recordset object Update call, ADO interprets your changes into an action query and sends the query to update the database.

To use a client-side cursor location in VBScript, set up a Const statement like this:

Const adUseClient=3

Then, refer to the constant name when you set the CursorLocation property.

Just before you call the Recordset object's Open method, set the CursorLocation property with a statement like this:

oRS.CursorLocation=adUseClient

As I mentioned previously, the default cursor location is server-side. To specifically set the cursor location to server-side, you'd use a statement such as:

Const adUseServer=2 oRS.CursorLocation=adUseServer

Note that if you use a client-side cursor location, the cursor type is changed to adOpenStatic even if you previously set it to a different cursor type. In my experience, using a client-side cursor location changes the cursor type to adOpenStatic even if I've previously set it to another cursor type. I don't know whether different providers generate different behavior (e.g., automatically adjusting the cursor type, throwing an error message).

To actually sort a recordset, download the script ADOExcelPart2.vbs. (Click the Download the Code button at the top of the page.) This script is a modified version of the ADOExcel.vbs script from "Using ADO to Access Excel, Part 1," it includes additions to incorporate the CursorLocation property change, as well as code to demonstrate sorting a recordset. After you modify the cursor location to client-side, you can use ADOExcelPart2.vbs to perform a simple sort on a recordset.

Call the Recordset object's Sort method followed by an equal sign; within double quotes, specify the field name you want to sort on. If you want to sort the field in descending order, follow the field name with a space and the keyword DESC, as in the following code:

oRS.Sort = "Header0 DESC"

If you don't specify descending order, the default of ascending order will be followed. Alternatively, you can explicitly specify ascending order by using the keyword ASC, as in the following code:

oRS.Sort = "Header0 ASC"

You aren't limited to sorting on just one field. You can sort on multiple fields and specify ascending or descending order for each field. For instance, you could perform a sort like this:

oRS.Sort = "Header0 DESC, Header3 ASC"

The primary sort in this statement is on Header0 in descending order; the secondary sort is on Header3 in ascending order. Be sure to separate your sort definitions with a comma, as shown.

Listing 1 contains the ADOExcelPart2.vbs code that sorts and filters a recordset. Callout A shows how you can step through a recordset after calling the Sort method and display the sorted Header0 field values.

Universal Data Link

Rather than manually keying in all the ConnectionString properties within a script, you can set up a connection string outside of the script as a .udl file and reference it within your code. The script ADOExcelUDL.vbs demonstrates how to use this method. (To download this script, click the Download the Code button at the top of the page.) Callout A in Listing 2 shows the code for setting up a .udl file and creating an ADO connection. You must create a blank text file in the C:\temp folder and name it xls.txt. Then, rename the file with a .udl extension so that the name is xls.udl. Double-click the file to open it. The Data Link Properties screen will appear.

Select the Provider tab, and click Microsoft Jet 4.0 OLE DB Provider (if you're using a version of Excel prior to 2007) or Microsoft Office 12.0 Access Database Engine OLE DB Provider (if you're using Excel 2007), as Figure 1 shows. As Callout B in Listing 2 shows, the providers that I used in "Using ADO to Access Excel, Part 1" have been commented out of ADOExcelUDL.vbs.

Click Next to navigate to the Connection tab, where you can enter your Excel spreadsheet's path and filename in the Data Source text box, as Figure 2 shows. Leave the other sections blank, and select the Advanced tab.

In the Advanced tab, verify that the Share Deny None check box is selected, as Figure 3 shows. Then, select the All tab.

In the All tab, double-click Extended Properties (or highlight Extended Properties and click Edit Value). The Edit Property Value dialog box will open. In the Property Value text box, enter Excel 12.0;HDR=YES; for Excel 2007, as Figure 4 shows, or Excel 8.0;HDR=YES; for Excel versions prior to 2007. Click OK.

Now, you can test the connection that you've created. Select the Connection tab and click the Test Connection button near the bottom of the dialog box. If everything is set up correctly, you'll see a popup that says Test connection succeeded. Click OK to finish creating the .udl file.

In addition to using a .udl file as an ADO connection file, you can also use a UDL to generate a provider connection string. After you have a working .udl file, simply open a blank text file and drag the .udl file into it. This action will give you a textual provider string that you can plug into your code as a provider string. To achieve the same result, you could make a copy of the .udl file and rename it with a .txt extension, then open the file.

Note that if you edit the Extended Properties value, you must add a second set of quotations marks to the string. See the sample text file in Figure 5 as an example. You'd need to modify the Provider string in this text file to look like this:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Temp\~Test~Spread~Sheet.xlsx;Extended Properties=""Excel 12.0;HDR=YES;"";Persist Security Info=False

(Notice the double quotes around the Extended Properties parameter.)

Then, you'd need to copy this line and insert it into the code, like so:

oConn.connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
  "Data Source=c:\Temp\~Test~Spread~Sheet.xlsx;" & _
  "Extended Properties=""Excel 12.0;HDR=YES;"";Persist Security Info=False"

This technique provides an easy method for ensuring that your provider strings are correct. You can use this method for setting up provider strings in other providers as well—not just in Excel.

Take Advantage of ADO

Using ADO to access Excel spreadsheets provides numerous benefits. In effect, your spreadsheets become databases that you can programmatically access, query, sort, search, and modify.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

More fun TechEd 2005 Resources

Kevin points out some more TechEd resources ...

Living Virtually with Hyper-V, MDOP 2008 R2

These products mark a new generation of Microsoft's suddenly sophisticated and increasingly mature virtualization platform. ...


Related Articles Using ADO to Access Excel, Part 1

Introduction to ADO

Much Ado About ADO

ActiveX Data Objects (ADO) Whitepapers Next-Generation Signoff Analysis Tackles Electrical, Physical, and Manufacturing Challenges

Chip Design Suing 45nm Processes Requires a Holistic Approach to Planning and Implementation

Chip Design Suing 45nm Processes Requires a Holistic Approach to Planning and Implementation

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Microsoft Exchange & Windows Connections event returns to Las Vegas Nov 10 - 13
Connections returns to Las Vegas for this exciting event where each attendee will receive SQL Server 2008 standard with 1 CAL. Co-located with Microsoft ASP.NET, SQL Server, and SharePoint Connections with over 250 in-depth sessions.

Free Online Event! Virtualization:Get the Facts!
Register now and attend this free, live in-depth online conference on November 13 and 20, 2008, produced by Windows IT Pro. All registrants are eligible to receive a complimentary one-year digital subscription to Windows IT Pro (a $49.95 value)!

Check Out Hyper-V Video on ITTV
Watch Karen Forster's interview on Hyper-V's performance on ITTV.net.

Ease Your Scripting Pains with the Flexibility of PowerShell!
Join MVP Paul Robichaux on December 11, 2008 at 11:00 AM EDT as he equips you with PowerShell basics in 3 introductory lessons, each followed by a live Q&A session—all on your own computer!

Latest Advancements in SSL Technology
There are a variety of different kinds of SSL to explore to ensure customer data is kept confidential and secure. In this paper, we will discuss some of these SSL advances to help you decide which would be best for your organization.

PASS Community Summit 2008 in Seattle on Nov 18-21
The don’t-miss event for Microsoft SQL Server Professionals. Register now and you’ll enjoy top-notch Microsoft and Community speakers and more.



Solving PST Management Problems
In this white paper, read about the top PST issues and how to administer local/network PST Files.

Get Protected -- Data Protection Manager 2007
Protect your virtualized environment with Data Protection Manager

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Maximize Your SharePoint Investment: Get Your Data Moving
Watch this web seminar now to learn how to maximize your SharePoint investment! Join us as we take a look at the complex business of securing, accessing and managing vast amounts of information in a global network and various ways to get your data moving.

List Your Products in Our Technology Resource Directory
Don't miss the chance to post your free listing in this comprehensive directory for IT and developer professionals, powered by Windows IT Pro. But hurry! Deadline ends Oct. 9.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing