Microsoft Knowledge Base Email Alertz

(304323) - This article shows you how to create an SQL pass-through query in Microsoft Visual Basic for Applications (VBA) with Microsoft ActiveX Data Objects (ADO).

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 304323 - Last Review: March 26, 2007 - Revision: 8.2

How to use ADOX to create an SQL pass-through query in Access

This article was previously published under Q304323
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access database (.accdb and .mdb).

SUMMARY

This article shows you how to create an SQL pass-through query in Microsoft Visual Basic for Applications (VBA) with Microsoft ActiveX Data Objects (ADO).

MORE INFORMATION

You can write a function in Visual Basic for applications that creates an SQL pass-through query. An SQL pass-through query is made up of an SQL statement and a connection string. When you run the query, it sends commands directly to the database server for processing. This removes the overhead of the Microsoft Jet database engine.

With the Data Access Object (DAO) model, you could use SQL pass-through queries to improve performance when you accessed external data. With ADO, you can use the Microsoft OLE DB Provider for SQL Server to directly access a SQL Server without the overhead of Microsoft Jet or ODBC. You can also use the Microsoft OLE DB Provider for ODBC to access data in any ODBC data source.

Although you no longer have to create SQL pass-through queries in your Microsoft Jet database to improve performance, you can still do so by using ADOX and the Jet Provider. The following code shows you how to create an SQL pass-through query.

NOTE: The sample code in this article uses both ADO and ActiveX Data Objects Extensions for Data Definition Language and Security (ADOX). For this code to run properly, you must click References on the Tools menu in the Visual Basic Editor and make sure that the following two references are selected:
Microsoft ActiveX Data Objects 2.1 Library
Microsoft ADO Ext. 2.6 for DDL and Security
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. To create an SQL pass-through query in code, follow these steps:
  1. Open the sample Northwind database.
  2. Create a new module, and then type or paste the following code:
    Function CreateSPT(SPTQueryName As String, strSQL As String)
    
      Dim cat As ADOX.Catalog
      Dim cmd As ADODB.Command
    
      Set cat = New ADOX.Catalog
      Set cmd = New ADODB.Command
    
      cat.ActiveConnection = CurrentProject.Connection
    
      Set cmd.ActiveConnection = cat.ActiveConnection
    
      cmd.CommandText = strSQL
      cmd.Properties("Jet OLEDB:ODBC Pass-Through Statement") = True
      
     'Modify the following connection string to reference an existing DSN for 
     'the sample SQL Server PUBS database.
    
      cmd.Properties _
         ("Jet OLEDB:Pass Through Query Connect String") = _
           "ODBC;DSN=myDSN;database=pubs;UID=sa;PWD=;"
      cat.Procedures.Append SPTQueryName, cmd
    
      Set cat = Nothing
      Set cmd = Nothing
    
    End Function
    					
  3. To test this function, type the following line in the Immediate window, and then press ENTER:
    ?CreateSPT("MySptQuery", "Select * from Authors")

REFERENCES

For more information about Microsoft OLE DB properties, visit the MSDN Library at the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/default.aspx (http://msdn2.microsoft.com/en-us/library/default.aspx)
In the Contents tree in the left pane of the MSDN Library, browse to the following Help topic:
  \Data Access
    \Microsoft Data Access Components
      \ADO
        \SDK Documentation
          \Microsoft ActiveX Data Objects (ADO)
            \ADO Programmer's Guide
              \Section V: Appendixes
                \Appendix A: Providers    
                  \Microsoft OLE DB Provider for Microsoft Jet
				

APPLIES TO
  • Microsoft Office Access 2007
  • Microsoft Office Access 2003
  • Microsoft Access 2002 Standard Edition
Keywords: 
kbquery kbvba kbado kbhowto KB304323
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Dan - danbarbano NOSPAM-AT-NOSPAM anchorblue.com Report As Irrelevant  
Written: 5/8/2006 1:06 PM
This code works great as long as your query doesn't return any records. If you're running an Insert query, however, it fails because the Returns Records property is defaulted to Yes. I haven't been able to figure out how to set this property to No using ADOX.

Dan Report As Irrelevant  
Written: 5/8/2006 1:25 PM
Adding this line of code: cmd.Properties("Jet OLEDB:Pass Through Query Bulk-Op") = True Will allow you to run queries that do not return records.