Prepare Property

Applies To

QueryDef object.

Description

Sets or returns a value that indicates whether the query should be prepared on the server as a temporary stored procedure, using the ODBC SQLPrepare API function, prior to execution, or just executed using the ODBC SQLExecDirect API function (ODBCDirect workspaces only).

Settings and Return Values

The setting or return value is a Long value that can be one of the following constants.

Constant

Description

dbQPrepare

(Default) The statement is prepared (that is, the ODBC SQLPrepare API is called).

dbQUnprepare

The statement is not prepared (that is, the ODBC SQLExecDirect API is called).


Remarks

You can use the Prepare property to either have the server create a temporary stored procedure from your query and then execute it, or just have the query executed directly. By default the Prepare property is set to dbQPrepare. However, you can set this property to dbQUnprepare to prohibit preparing of the query. In this case, the query is executed using the SQLExecDirect API.

Creating a stored procedure can slow down the initial operation, but increases performance of all subsequent references to the query. However, some queries cannot be executed in the form of stored procedures. In these cases, you must set the Prepare property to dbQUnprepare.

If Prepare is set to dbQPrepare, this can be overridden when the query is executed by setting the Execute method's options argument to dbExecDirect.

Note   The ODBC SQLPrepare API is called as soon as the DAO SQL property is set. Therefore, if you want to improve performance using the dbQUnprepare option, you must set the Prepare property before setting the SQL property.

Example

This example uses the Prepare property to specify that a query should be executed directly rather than first creating a temporary stored procedure on the server.

Sub PrepareX()

    Dim wrkODBC As Workspace
    Dim conPubs As Connection
    Dim qdfTemp As QueryDef
    Dim rstTemp As Recordset

    ' Create ODBCDirect Workspace object and open Connection
    ' object.
    Set wrkODBC = CreateWorkspace("", _
        "admin", "", dbUseODBC)
    Set conPubs = wrkODBC.OpenConnection("Publishers", , , _
        "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers")

    Set qdfTemp = conPubs.CreateQueryDef("")

    With qdfTemp
        ' Because you will only run this query once, specify
        ' the ODBC SQLExecDirect API function. If you do
        ' not set this property before you set the SQL
        ' property, the ODBC SQLPrepare API function will
        ' be called anyway which will nullify any
        ' performance gain.
        .Prepare = dbQUnprepare
        .SQL = "UPDATE roysched " & _
            "SET royalty = royalty * 2 " & _
            "WHERE title_id LIKE 'BU____' OR " & _
            "title_id LIKE 'PC____'"
        .Execute
    End With
    Debug.Print "Query results:"

    ' Open recordset containing modified records.
    Set rstTemp = conPubs.OpenRecordset( _
        "SELECT * FROM roysched " & _
        "WHERE title_id LIKE 'BU____' OR " & _
        "title_id LIKE 'PC____'")

    ' Enumerate recordset.
    With rstTemp
        Do While Not .EOF
            Debug.Print , !title_id, !lorange, _
                !hirange, !royalty
            .MoveNext
        Loop
        .Close
    End With

    conPubs.Close
    wrkODBC.Close

End Sub