Microsoft KB Archive/148361: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - "&" to "&")
 
(One intermediate revision by the same user not shown)
Line 71: Line 71:


     'Set the path to the database
     'Set the path to the database
     oldDbName = "C:\Msoffice\access\samples\Northwind.mdb"
     oldDbName = "C:\Msoffice\access\samples\Northwind.mdb"


     'Create a default workspace Object
     'Create a default workspace Object
Line 80: Line 80:


     'The SQL statement
     'The SQL statement
     strSQL = "SELECT Employees.LastName, Employees.FirstName, " & _
     strSQL = "SELECT Employees.LastName, Employees.FirstName, " & _
         "Employees.Country FROM Employees Employees " & _
         "Employees.Country FROM Employees Employees " & _
         "WHERE (Employees.Country='USA')"
         "WHERE (Employees.Country='USA')"


     'Create a Snapshot Type Recordset from the SQL query
     'Create a Snapshot Type Recordset from the SQL query
Line 89: Line 89:


     'Show the number of fields returned
     'Show the number of fields returned
     MsgBox "there are " & rstFromQuery.Fields.Count & _
     MsgBox "there are " & rstFromQuery.Fields.Count & _
     " fields that were returned"
     " fields that were returned"


     'Move to the last record in the recordset
     'Move to the last record in the recordset
Line 96: Line 96:


     'Show the number of records returned
     'Show the number of records returned
     MsgBox "there are " & rstFromQuery.RecordCount & _
     MsgBox "there are " & rstFromQuery.RecordCount & _
     " records that were returned"
     " records that were returned"


   End Sub
   End Sub
Line 108: Line 108:
== REFERENCES ==
== REFERENCES ==


For more information about the OpenRecordSet method in Microsoft Excel 97, from the Visual Basic Editor, click the Office Assistant, type <span class="kbd userinput"> OpenRecordSet</span>, click Search, and then click to view &quot;OpenRecordSet method.&quot;<br />
For more information about the OpenRecordSet method in Microsoft Excel 97, from the Visual Basic Editor, click the Office Assistant, type <span class="kbd userinput"> OpenRecordSet</span>, click Search, and then click to view "OpenRecordSet method."<br />
<br />
<br />
NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:
NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:
Line 124: Line 124:


</div>
</div>
and then double-click the selected text to go to the &quot;OpenRecordsetMethod&quot; topic.
and then double-click the selected text to go to the "OpenRecordsetMethod" topic.


</div>
</div>

Latest revision as of 12:28, 21 July 2020

Article ID: 148361

Article Last Modified on 10/11/2006



APPLIES TO

  • Microsoft Excel 97 Standard Edition



This article was previously published under Q148361

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 programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

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 in Microsoft Excel 97, from the Visual Basic Editor, click the Office Assistant, type OpenRecordSet, click Search, and then click to view "OpenRecordSet method."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:

176476 OFF: Office Assistant Not Answering Visual Basic Questions


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: 8.00 97 xl97 DAO Database RecordSet visualbasic editor XL

Keywords: kbdtacode kbhowto kbprogramming KB148361