VB6 - Accessing Records
VB6 - Accessing Records
Accessing Records
2000
Peter Vogel
In this article, Peter Vogel looks at one of the newer features Dim rc As Record
Set rc = New Record
in ADO: the Record object. Peter outlines the object’s future rc.Open rs
and shows how you can use it right now from your Access
application to work with files over the Internet. After this code executes, the Record object rc should
then be populated with the current record in the
T
RYING to keep up with ActiveX Data Objects (ADO) Recordset rs. In practice, for almost every provider that
isn’t as easy as it was with DAO. ADO is still an you try, you’ll get the message “Object or provider is not
evolving technology, with new features being added capable of performing requested operation.” The only
to the existing ADO objects and whole new objects being provider that currently supports the Record object seems
added to the ADO package. In this article, I’ll review to be the Microsoft OLE DB provider for Internet
one of the newer objects in the ADO library: the Record Publishing (also installed with MDAC 2.5 or later).
object. While the Record object has a bright future ahead Microsoft’s documentation suggests that there are
of it, there’s currently only one ADO provider that bigger plans for the Record object than this single
supports it. However, using that provider you can retrieve provider. In the future, Record objects may be used
any file, as long as you know the file’s URL, or navigate instead of the Recordset object for commands that return
through the directory structure of any site. You can also a single record. The benefit would be a lighter object
copy, move, add, or delete any file for which you know (since scrolling wouldn’t have to be supported) and faster
the URL. processing. As yet, though, you can’t use the Record
A word of warning: In any application, before taking object to access standard data sources.
advantage of any of the newer features of ADO (anything The Record object’s current purpose in life is to be
after ADO 2.0), you should make sure that those features used with “semi-structured” resources. A semi-structured
will be available on the computer where your application resource is a data source that lacks the rigid format of a
will be installed. While all of the original features of ADO relational data model. Data in a semi-structured resource
are still available in the latest versions of the technology, is organized (if it’s organized at all) as a hierarchy or tree.
the reverse isn’t true (see the sidebar “Mutating ADO” on Hierarchical data sources include file systems and, as in
page 15 for more on this topic). the examples in this article, Web sites. While the Record
To take advantages of the technology described object documentation suggests that the Record object is
in this article, you’ll need to have ADO 2.5 or later ready to be used to process a file system, no indication
installed on your computer (I actually used ADO 2.6 for of how that’s to be done is provided. With the Internet
my testing). You can download the latest version of ADO, Publishing provider, though, you can access any file that
along with a variety of supporting technologies, as part you can reach using a URL, security permitting.
of the Microsoft Data Access Components (MDAC)
from www.microsoft.com/data. Once you’ve downloaded Opening a Record
and installed it, you’ll need to check off Microsoft ActiveX Opening a Record object looks very much like opening
Data Objects 2.x Library in your References list before a Recordset: You just pass a command and a connection
you can use them. This is required even if you’re using string to the Open method of the object. The Record
Access 2000, as the ADO library that’s checked off by object, however, shows a lot of flexibility in the format
default for Access 2000 is 2.1, which doesn’t include the of the connection string. You can, for instance, specify
Record object. a provider as you would with a Recordset. To use the
Internet Publishing provider to access a site called
The Record object www.mserver.com, you’d use this text in your
The Record object sounds like it should be associated with connection string:
the Recordset object, and, to a certain extent, it is. In
Provider=MSDAIPP.DSO;Data Source=www.mserver.com;
theory, the Record object can be used to represent one User ID=phv;Password=phvpwd;
record in a Recordset. The code to create a Record object
from a Recordset object looks like this (where rs is an As a shortcut, the Record object will default to using
open Recordset):
URL=http://www.mserver.com;
User ID=phv;Password=phvpwd;
Dim rc As Record
Set rc = New Record
rc.Open "MyFile.txt","URL=http://www.mserver.com"
Mutating ADO
Much of what you can do with ADO depends on which version example, drops support for the Jet database engine. If your
of ADO is installed on your computer. And, if you distribute application depends on Jet but the client computer has only
applications, much depends on what you’ve installed on your ever had the latest version of MDAC installed, you’re going to
clients’ computers. So how do you determine what version of have problems.
ADO you have? Given the possible variations, the only safe thing to do when
The first thing you need to do is discriminate between the distributing an application that depends on ADO is to include
version of MDAC (Microsoft Data Access Components) and the your version of the MDAC components that you need as part of
version of ADO installed. The version of ADO is relatively easy to your setup routine.
determine: Just check the Version property of the Connection While not a solution that you can use from your
object. In Access 2000, you can use the Connection object from application, Microsoft’s Component Checker (available at
the Application object’s CurrentProject: www.microsoft.com/data/download.htm#CCinfo) will analyze
your installation (see Figure 1) and report on the state of the
Msgbox Application.Connection.Version
MDAC components. Component Checker makes a “best guess”
effort to determine what version of MDAC you have installed. The
In earlier versions of Access, you’ll need to create a
report that Component Checker makes on your version illustrates
Connection object:
the difficulty of this effort: On my Windows 2000 Professional
Dim conn As Connection installation, it reads “The MDAC version that is closest to the
Set conn = New Connection
Msgbox conn.Version version on your computer is ‘2.5 RTM (2.50.4403.12).’”
Component Checker then lists under the headings File
The problem is more complicated if you’re depending on Details, COM Details, and Registry Details all of the problems that
the rest of the MDAC components—the data providers, for it found while analyzing your installation. I’ve never seen an
instance. Installing MDAC will, of course, upgrade/add/replace all installation without problems. My Windows 2000 laptop with
of the components that make up MDAC. However, subsequent ADO installed as part of the operating system had three errors
installations of other applications (for instance, SQL Server 2000, and three warnings. On my Windows 98 desktop computer with
which installs ADO 2.6) can overwrite portions of the MDAC MDAC 2.0 installed as part of Visual Studio, I had 11 errors and
installation, resulting in a mixture of components that relates three warnings. As a test, I did a clean install of MDAC 2.5 on my
to no particular package. In addition, the contents that make desktop and reran Component Checker. Component Checker
up MDAC change over time. The latest version of MDAC, as an then found six errors and four warnings.
Can’t see/change Rowsource One of the main features in my real application lets users change the chart’s criteria on the fly. On a form’s chart, it
was trivial: I changed the chart’s Rowsource, and the chart obligingly repainted itself with the new data. On a
report’s chart, however, I couldn’t even see the Rowsource property, let alone set it! I spent several hours
superstitiously repainting in different ways to see whether I could get the chart to “keep” its properties. No luck. I
did find a workaround: Bind the chart to a working query (for example, qtmpChart), and manipulate the query’s
SQL property at runtime. It’s a fine line between a cool technique and cheating.
Can’t change ChartType The ChartType is another powerful property that disappears when you paint the chart on a report. One
workaround I can offer is to paint one chart for each type you might need, and use a Select Case to set the Visible
property of the chart that you want to display. This solution has its own problems, as the invisible charts take up
resources even if you use separate temporary queries and try to minimize their impact by setting them to
impossible conditions (for example, giving the query a WHERE clause of 1=2). Ultimately, I abandoned this
approach and created a separate report for each chart type.