Opening Connections Asynchronously

In some cases, opening connections to data sources can take a long time, making it necessary for users to wait until the connection completes or an error occurs. To reduce the amount of time users must wait, you can open a connection asynchronously. This means that your application can complete other tasks while the connection is being established. To open a connection asynchronously, specify the dbRunAsync constant for the options argument of the OpenConnection method, as shown in the following example.

Dim wrk As Workspace, cnn As Connection, strConnect As String

Set wrk = DBEngine.CreateWorkspace("NewODBCDirect", "Admin", "", dbUseODBC)
strConnect = "ODBC;DSN=Pubs;UID=SA;PWD=;DATABASE=Pubs"
Set cnn = wrk.OpenConnection("", dbDriverNoPrompt + dbRunAsync, False, strConnect)

You can use the StillExecuting property of the Connection object to see if the connection has been established, or use the Cancel method of the Connection object to cancel the connection attempt if it takes too long.