Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Sample Code for Running Temporary SQL Pass-Through Query


View products that this article applies to.

Summary

This article lists sample code that you can use to call and run a temporary SQL pass-through query.

↑ Back to the top


More information

You can use the following sample Visual Basic code to call and run a temporary SQL pass-through query:

NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.

Function ExecuteSPT (sqltext As String, connectstring As String)

' Purpose: Run a temporary pass-through query.
' Accepts: sqltext: SQL string to run.
'          connectstring: Connection string, which must be at least
'          "ODBC;".
' Returns: nothing.

Dim mydb As DAO.Database, myq As DAO.QueryDef
Set mydb = DBEngine.Workspaces(0).Databases(0)

' Create a temporary QueryDef object that is not saved.
Set myq = mydb.CreateQueryDef("")

' Set the ReturnsRecords property to False in order to use the
' Execute method.
myq.returnsrecords = False

myq.connect = connectstring
myq.sql = sqltext

myq.Execute
myq.Close

End Function
				
To run a stored procedure called sp_example on your SQL Server, type the following line in the Immediate window:
?ExecuteSPT("sp_example","ODBC;")
Note that this code accepts SQL statements that are specific to the server that you are using. Please refer to your server's documentation to determine valid SQL statements for your server.

↑ Back to the top


References

For more information about pass-through queries, click Microsoft Access Help on the Help menu, type send commands to an sql database using a pass-through query in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

↑ Back to the top


Keywords: KB210323, kbusage, kbprogramming, kbinfo

↑ Back to the top

Article Info
Article ID : 210323
Revision : 2
Created on : 6/28/2004
Published on : 6/28/2004
Exists online : False
Views : 365