Connection.Cancel 方法 (DAO)

适用于:Access 2013、Office 2013

语法

表达式 。取消

表达式 一个表示 Connection 对象的变量。

说明

使用 Cancel 方法可终止异步 ExecuteOpenConnection 方法调用的执行, (即使用 dbRunAsync 选项) 调用该方法。 如果在尝试终止的方法中未使用 dbRunAsync,取消将返回运行时错误。

如果在调用 Cancel 方法之后,您试图引用由异步 OpenConnection 调用创建的对象(即,您从其处调用 Cancel 方法的 Connection 对象),将会发生错误。

示例

以下示例使用 StillExecuting 属性和 Cancel 方法异步打开 Connection 对象。

    Sub CancelConnectionX() 
     
     Dim wrkMain As Workspace 
     Dim conMain As Connection 
     Dim sngTime As Single 
     
     Set wrkMain = CreateWorkspace("ODBCWorkspace", _ 
     "admin", "", dbUseODBC) 
     ' Open the connection asynchronously. 
     
     ' Note: The DSN referenced below must be configured to 
     ' use Microsoft Windows NT Authentication Mode to 
     ' authorize user access to the Microsoft SQL Server. 
     Set conMain = wrkMain.OpenConnection("Publishers", _ 
     dbDriverNoPrompt + dbRunAsync, False, _ 
     "ODBC;DATABASE=pubs;DSN=Publishers") 
     
     sngTime = Timer 
     
     ' Wait five seconds. 
     Do While Timer - sngTime < 5 
     Loop 
     
     ' If the connection has not been made, ask the user 
     ' if she wants to keep waiting. If she does not, cancel 
     ' the connection and exit the procedure. 
     Do While conMain.StillExecuting 
     
     If MsgBox("No connection yet--keep waiting?", _ 
     vbYesNo) = vbNo Then 
     conMain.Cancel 
     MsgBox "Connection cancelled!" 
     wrkMain.Close 
     Exit Sub 
     End If 
     
     Loop 
     
     With conMain 
     ' Use the Connection object conMain. 
     .Close 
     End With 
     
     wrkMain.Close 
     
    End Sub