XL: Visual Basic Example to Open a RecordSet Using DAO

Last reviewed: February 27, 1998
Article ID: Q148361
The information in this article applies to:
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95, version 7.0

SUMMARY

This article provides a Visual Basic for Applications example of how to create a Data Access Object (DAO) record set that contains the results of an SQL Query string.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Visual Basic Code Example

  1. Insert a Visual Basic module into a new Microsoft Excel workbook.

  2. Create a reference to the Microsoft DAO object library. In Microsoft Excel 97 the DAO library will be version 3.5. In Microsoft Excel 7.0 the DAO library will be version 3.0.

  3. Type the following code into the module sheet:

       ' This code assumes you have Microsoft Access 7.0 installed and have
       ' the Northwind.mdb sample database installed in the default
       ' location.
    
       Sub CreateRecordSet()
    
         Dim oldDbName As String
         Dim wspDefault As Workspace
         Dim dbsNorthwind As Database
         Dim strSQL As String
         Dim rstFromQuery As Recordset
    
         'Set the path to the database
         oldDbName = "C:\Msoffice\access\samples\Northwind.mdb"
    
         'Create a default workspace Object
         Set wspDefault = DBEngine.Workspaces(0)
    
         'Create a Database object
         Set dbsNorthwind = wspDefault.OpenDatabase(oldDbName)
    
         'The SQL statement
         strSQL = "SELECT Employees.LastName, Employees.FirstName, " & _
            "Employees.Country FROM Employees Employees " & _
            "WHERE (Employees.Country='USA')"
    
         'Create a Snapshot Type Recordset from the SQL query
         Set _
         rstFromQuery = dbsNorthwind.OpenRecordset(strSQL,dbOpenSnapshot)
    
         'Show the number of fields returned
         MsgBox "there are " & rstFromQuery.Fields.Count & _
         " fields that were returned"
    
         'Move to the last record in the recordset
         rstFromQuery.MoveLast
    
         'Show the number of records returned
         MsgBox "there are " & rstFromQuery.RecordCount & _
         " records that were returned"
    
      End Sub
    
    

REFERENCES

For more information about the OpenRecordset method, click the Index tab in Microsoft Excel 7.0 Help, type the following text

   OpenRecordset Method

and then double-click the selected text to go to the "OpenRecordsetMethod" topic.


Additional query words: 7.00 8.00 97 xl97 DAO Database RecordSet
visualbasic editor
Keywords : kbcode kbprg kbhowto
Version : WINDOWS:7.0,97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.