ODBCTimeout and QueryTimeout Properties Example

This example uses the ODBCTimeout and QueryTimeout properties to show how the QueryTimeout setting on a Database object sets the default ODBCTimeout setting on any QueryDef objects created from the Database object.

Sub ODBCTimeoutX()

    Dim dbsCurrent As Database
    Dim qdfStores As QueryDef
    Dim rstStores As Recordset

    Set dbsCurrent = OpenDatabase("Northwind.mdb")

    ' Change the default QueryTimeout of the Northwind 
    ' database.
    Debug.Print "Default QueryTimeout of Database: " & _
        dbsCurrent.QueryTimeout
    dbsCurrent.QueryTimeout = 30
    Debug.Print "New QueryTimeout of Database: " & _
        dbsCurrent.QueryTimeout

    ' Create a new QueryDef object.
    Set qdfStores = dbsCurrent.CreateQueryDef("Stores", _
        "SELECT * FROM stores")
    qdfStores.Connect = _
        "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers"

    ' Change the ODBCTimeout setting of the new QueryDef 
    ' object from its default setting.
    Debug.Print "Default ODBCTimeout of QueryDef: " & _
        qdfStores.ODBCTimeout
    qdfStores.ODBCTimeout = 0
    Debug.Print "New ODBCTimeout of QueryDef: " & _
        qdfStores.ODBCTimeout

    ' Execute the query and display the results.
    Set rstStores = qdfStores.OpenRecordset()

    Debug.Print "Contents of recordset:"
    With rstStores
        Do While Not .EOF
            Debug.Print , .Fields(0), .Fields(1)
            .MoveNext
        Loop
        .Close
    End With

    ' Delete new QueryDef because this is a demonstration.
    dbsCurrent.QueryDefs.Delete qdfStores.Name
    dbsCurrent.Close

End Sub