0% found this document useful (0 votes)
35 views6 pages

VB6 - Accessing Records

Uploaded by

Arther Traver
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

VB6 - Accessing Records

Uploaded by

Arther Traver
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Downloads

www.vb123.com.au Smart Access

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):

14 Smart Access February 2001 http://www.smartaccessnewsletter.com


the Internet Publishing provider if your connection string Dim rc As Record
Set rc = New Record
contains a URL parameter like this one: rc.Open "/","URL=http://www.mserver.com"

URL=http://www.mserver.com;
User ID=phv;Password=phvpwd;

To use the Internet Publishing provider to retrieve a


file called MyFile.txt at the URL www.mserver.com, you’d
use this code:

Dim rc As Record
Set rc = New Record
rc.Open "MyFile.txt","URL=http://www.mserver.com"

You can’t, however, specify both the URL and the


Provider property. If you do specify the Provider, then
you must specify the URL that you want to access by
using the Data Source parameter.
You’re not restricted to working with individual files.
Assuming that you have the appropriate security, you can
retrieve a directory from a Web site. This code retrieves Figure 1. Component Checker showing the summary report from
the root directory for the mserver site: its analysis.

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.

http://www.smartaccessnewsletter.com Smart Access February 2001 15


The Open method accepts five other parameters in You can check to see whether a Record object
addition to the filename and connection. The third represents a file or a directory (leaf or non-leaf node) by
parameter, Mode, determines whether the resource can be checking the Record object’s RecordType property. If that
updated and whether the resource is opened exclusively. property is set to adSimpleRecord, you can’t use the
The fourth parameter, CreateOptions, will cause the file GetChildren method. Using GetChildren with a Record
or directory to be created if it doesn’t already exist. The of adSimpleRecord generates the message “Operation is
fifth parameter, Options, controls how and when the data not allowed in this context.” If the RecordType property is
will actually be retrieved (you can defer retrieving the set to any value other than adSimpleRecord, you can at
contents of a file while still receiving information about least try to retrieve the node’s children. The other three
it, for instance). The Options parameter is supposed to values are adCollectionRecord (a node with children),
support a setting of adOpenExecuteCommand that would adRecordUnknown (a node of unknown type), and
allow you to execute a SQL command to retrieve a single adStructDoc (a COM structured document).
row, but that doesn’t appear to be implemented yet. The The following code cycles through all of the children
final two parameters allow you to specify a user name of the mserver root directory and retrieves the children
and password. of those nodes. After retrieving the root directory, the
code retrieves a Recordset of all of the root’s children by
Navigating using GetChildren on the root Record. Using standard
One of the differences between retrieving a single file Recordset processing, the code moves through the rows
and retrieving a directory is that you can retrieve the of that Recordset, opening a Record object for each row
children of a directory by using the Record object’s and checking its RecordType property. Whenever a non-
GetChildren method. The GetChildren method returns adSimpleRecord is found, the code retrieves a Recordset
a Recordset that lists all of the files and subdirectories for of the children of that node:
the Record object. This code, for instance, retrieves into a
Recordset all of the files and subdirectories of the mserver Dim rc As Record
Dim rcChild As Record
root directory: Dim rs As Recordset
Dim rsChildren As Recordset
Dim rc As Record
Dim rs As Recordset Set rc = New Record
Set rc = New Record rc.Open "/","URL=http://www.mserver.com"
rc.Open "/","URL=http://www.mserver.com" Set rs = rc.GetChildren
Set rs = rc.GetChildren Do While Not rs.EOF
Set rcChild = New Record
rcChild.Open rs
The Recordset produced by the GetChildren method If rcChild.RecordType <> adSimpleRecord Then
Set rsChildren = rcChild.GetChildren
is a forward-only, server-side Recordset. Because it’s a End If
server-side Recordset, it can’t be bound to the Recordset rs.MoveNext
Loop
property of an Access 2000 Form. If the Record object is
updateable, the Recordset will be updateable also.
A recursive routine to process all of the directories
With this Recordset, built using the Internet
on the site would look like the following code (and is
Publishing provider, you can actually retrieve a Record
included in the sample database with this month’s Source
object from the Recordset as promised in the Microsoft
Code files at www.smartaccessnewsletter.com). Passed a
documentation. If you do retrieve a Record from the
URL, the TraverseSite routine will work through all of the
Recordset, and if the row in the Recordset represents a
files and directories on the site, displaying the file or
subdirectory, you can then use the GetChildren method
directory name:
to retrieve all of the subdirectory’s files and directories.
Navigating through “semi-structured” information, Sub TraverseSite (strURL As String)
then, consists of retrieving Recordsets of files and Dim rc As Record
Set rc = New Record
subdirectories and then retrieving the children of rc.Open "/","URL=http://" & strURL
GetChildren rc
the directories. rc.Close
Some terminology is probably appropriate at this Set rc = Nothing
End Sub
stage. All of the items retrieved by GetChildren or
the Open method are referred to as “nodes.” Nodes Sub GetChildren (rc As Record)
Dim rcChild As Record
without children are referred to as “leaf nodes” and Dim rs As Recordset
Dim rsChildren As Recordset
typically contain data (like the contents of a file retrieved
from a Web site). Non-leaf nodes (like directories) Set rs = rc.GetChildren
Do While Not rs.EOF
have other nodes as their content but may also contain Set rcChild = New Record
data themselves. rcChild.Open rs

16 Smart Access February 2001 http://www.smartaccessnewsletter.com


If rcChild.RecordType <> adSimpleRecord Then absolute URL. For the file retrieved by this code:
GetChildren rcChild
End If
rs.MoveNext rc.Open "MyFile.txt", "http://www.mserver.com"
Loop

End Sub the RESOURCE_ABSOLUTEPARSENAME field


would contain:
Record fields
So what do you do with a Record? Like a Recordset, the http:// www.mserver.com / MyFile.txt
first thing that you can do with a Record is access its
fields. A Record has a Fields collection that works in the The two fields defined by the Record object aren’t
same way as a Recordset. What appears in the Fields guaranteed to appear in the same position for every
collection has more to do with the provider that you use provider. The Record object provides two enumerated
than the data that you access. values that you can use to retrieve those two fields, as
The Record object guarantees only that two fields shown in Table 1. Using adRecordURL, even though it
will be present in the Fields collection: has a value of -2, actually retrieves the third field,
• A field containing the content of the node (for the RESOURCE_ABSOLUTEPARSENAME, from the Record
Internet Publishing provider, that’s the content of a object’s Fields collection. The adDefaultStream field
file from a Web site) doesn’t appear in the Record object’s Fields collection
• A field containing the absolute URL of the node (for unless you retrieve it with the enumerated value
the Internet Publishing provider, that’s the URL of the adRecordURL or an index of -1.
file from the Web site)
Table 1. The Record object’s enumerated values for retrieving the
However, the provider can add any additional fields two defined fields.
that it wants to the Record’s Fields collection. The Internet
Publishing provider puts 18 fields in the Record object’s Enumerated value Value Field retrieved
Fields collection, for instance. One of those fields (the adDefaultStream -1 Node contents
adRecordURL -2 Node’s absolute URL
third field in the collection, with an index of 2, called
RESOURCE_ABSOLUTEPARSENAME) contains the Continues on page 21

http://www.smartaccessnewsletter.com Smart Access February 2001 17


was to warn you that it’s harder to work with a chart on a should inspire you to experiment with the MS Graph
report than on a form. I say that because (are you ready?) library’s Chart object in your own applications. ▲
the chart’s behavior changes. Surprised? So was I! So to
spare you some frustration, I’ve summarized three of my GANTT.ZIP at www.smartaccessnewsletter.com
most disconcerting discoveries in Table 1.
You can use my techniques to add professional-
Doug Den Hoed is a founder of Lumina Systems Delivery in Calgary,
looking Gantt charts to your Access (and Excel) Canada, which specializes in customized software solutions using
applications that will impress your users. I find that Access, Visual Basic, InterDev, SQL Server, and Oracle. Doug drew this
getting a chart to look just right is fun and satisfying, article from The KB™ (http://www.thekb.com), his Access-based
and it speaks volumes to my users (at least a thousand commercial package for managing software development projects.
words!). The tips and workarounds I’ve shared here [email protected].

Table 1. Chart properties differ on forms and reports.

Gotcha Details, advice, and workarounds


Property imparity After several hours of denial, I finally confirmed the awful truth: A chart painted on a form has more properties (61)
than the exact same chart painted on a report (42). My emphasis is on the differences. If you’re counting on
changing some properties at runtime on a report, I recommend that you enumerate the properties in design mode
(for example, for each prp in objChart.Properties debug.print prp.name…), note what’s available, and test with
Access’s /runtime startup option.

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.

Accessing Records... method, at which point, presumably, the underlying


physical entity will be updated with the new field and
Continued from page 17 its content. The Internet Publishing provider won’t
update a file or directory, so it will only let you add fields
The other fields provided by the Internet Publishing temporarily. Calling the Update method of the Fields
provider include the name of the parent directory collection for the Internet Publishing provider will
(RESOURCE_PARENTNAME) and flags indicating generate the message “Current Provider does not support
whether the file is hidden or read-only, among adding and deleting columns on the Record object.”
other information.
You can add new fields to the collection just by Working with files
referring to them. For instance, this code adds a new In addition to browsing the structure of the Web site
field called Handled to a Record’s Fields collection and and retrieving files, you can use the Record object to
sets it to True: change the files on the site by using the Record object’s
CopyRecord, DeleteRecord, and MoveRecord methods.
rc!Handled = True The DeleteRecord method is the simplest. This
method, used without a parameter, deletes the resource
The field that’s created will have the Variant data
specified by the Record object. This code deletes the
type, which Microsoft’s documentation says isn’t yet
physical entity pointed to by the Record object:
supported by ADO. The fields that are added are only
temporary unless you use the Fields collection’s Update rc.DeleteRecord

http://www.smartaccessnewsletter.com Smart Access February 2001 21


The DeleteRecord method also accepts a parameter Peter Vogel (MBA, MCSD) is the editor of Smart Access and a principal in
that specifies the resource to be deleted. PH&V Information Services. PH&V specializes in system design and
The MoveRecord method copies the resource to the development for COM/COM+ based systems. Peter has designed, built,
location specified by the second parameter and then and installed intranet and component-based systems for Bayer AG,
Exxon, Christie Digital, and the Canadian Imperial Bank of Commerce.
deletes the resource. If the first parameter is omitted, the
He’s also the editor of Pinnacle’s XML Developer newsletter, wrote The
MoveRecord method copies the resource that’s pointed
Visual Basic Object and Component Handbook (Prentice Hall), and is
to by the Record object. If the first parameter is provided,
currently working on a book on user interface design (Apress). Peter
that’s the resource that will be copied. The MoveRecord teaches for Learning Tree International, wrote its Web application
method also accepts as its third and fourth parameters development course, is technical editor of its COM+ course, and is
a user name and password to access the location where currently developing its Technical Writing course. His articles have
the resource will be copied. The fifth parameter to the appeared in every major magazine devoted to VB-based development
method controls what happens if the target of the and in the Microsoft Developer Network libraries. Peter also presents at
move already exists. The CopyRecord method acts conferences in North America and Europe. [email protected].
like MoveRecord but doesn’t delete the resource after
making the new version.
Many of the methods of the Record object have
an Async parameter that allows you to execute the
method asynchronously. However, the Record object
doesn’t fire events, so you’ll have to monitor the
progress of the methods by checking the Record object’s
State property.
Manipulating Internet files can be useful, but XML • Web Development • SQL Server • Visual Basic • MS Access • Oracle • Visual C++ • Delphi
what you really want to do is get at the content of those • FoxPro • XML • Web Development • SQL Server • Visual Basic • MS Access • Oracle

files that you’re retrieving. The field retrieved with the


Sign up now for Pinnacle’s FREE eNewsletters!
adDefaultStream value is actually a Stream object, another
Get tips, tutorials, and news from gurus in the field
of the new ADO objects added in ADO 2.5. Next month,
delivered straight to your Inbox.
I’ll show you how the Stream object works and how to
integrate it both with XML and the Record object. ▲ http://www.FREEeNewsletters.com
XML • Web Development • SQL Server • Visual Basic • MS Access • Oracle • Visual C++ • Delphi
• FoxPro • XML • Web Development • SQL Server • Visual Basic • MS Access • Oracle
ADORCRD.ZIP at www.smartaccessnewsletter.com

Downloads February 2001 Source Code


• DTSHEETS.ZIP—Michael Kaplan’s sample database includes URL (security permitting). (Access 97)
a number of routines for manipulating datasheets. The
code in this MDB file can be used to show and hide columns, • GANTT.ZIP—In his sample database, Doug Den Hoed has
prevent your users from repositioning columns, and resize included all of the code and forms to generate Gantt
columns to fit the data that they contain. (Access 97, charts from Access data. You’ll need to have MS Chart
Access 2000) installed to use this code. Doug has also provided the
sample Excel spreadsheet that he used in developing his
• IDENTITY.ZIP—This Access add-in not only includes all of code. (Access 97)
Russell Sinclair’s code for working with AutoNumber fields, it
also includes a user interface that allows you to modify the • SA0102.ZIP and SA0102.CHM—The latest update to the
AutoNumber fields in your MDB file. (Access 2000) cumulative Smart Access index comes in Access 2.0, Access
95, and Access 97 versions. It includes full descriptions of
• ADORCRD.ZIP—Peter Vogel has provided a sample database every article since the January 1999 issue of Smart Access, as
that shows how to use the ADO Recordset object. The well as an index to each month’s Source Code files. (The
routines TraverseSite and GetChildren will provide a index is available in “SA0201.ZIP—The Complete February
complete file listing for any Web site for which you have the 2001 Source Code.”)

22 Smart Access February 2001 http://www.smartaccessnewsletter.com

You might also like