State Property Example (VB)

Microsoft ActiveX Data Objects (ADO)

State Property Example (VB)

This example uses the State property to display a message while asynchronous connections are opening and asynchronous commands are executing.

Public Sub StateX()

    Dim cnn1 As ADODB.Connection
    Dim cnn2 As ADODB.Connection
    Dim cmdChange As ADODB.Command
    Dim cmdRestore As ADODB.Command
    Dim strCnn As String
    
    ' Open two asynchronous connections, displaying
    ' a message while connecting.
    Set cnn1 = New ADODB.Connection
    Set cnn2 = New ADODB.Connection
    strCnn = "Provider=sqloledb;" & _
        "Data Source=srv;Initial Catalog=Pubs;User Id=sa;Password=; "
        
    cnn1.Open strCnn, , , adAsyncConnect
    While (cnn1.State = adStateConnecting)
        Debug.Print "Opening first connection...."
    Wend
    
    cnn2.Open strCnn, , , adAsyncConnect
    While (cnn2.State = adStateConnecting)
        Debug.Print "Opening second connection...."
    Wend
    
    ' Create two command objects.
    Set cmdChange = New ADODB.Command
    cmdChange.ActiveConnection = cnn1
    cmdChange.CommandText = "UPDATE Titles SET type = 'self_help' " & _
        "WHERE type = 'psychology'"
    
    Set cmdRestore = New ADODB.Command
    cmdRestore.ActiveConnection = cnn2
    cmdRestore.CommandText = "UPDATE Titles SET type = 'psychology' " & _
        "WHERE type = 'self_help'"
    
    ' Executing the commands, displaying a message
    ' while they are executing.
    cmdChange.Execute , , adAsyncExecute
    While (cmdChange.State = adStateExecuting)
        Debug.Print "Change command executing...."
    Wend
    
    cmdRestore.Execute , , adAsyncExecute
    While (cmdRestore.State = adStateExecuting)
        Debug.Print "Restore command executing...."
    Wend
    
    cnn1.Close
    cnn2.Close

End Sub