解决由于SQL查询时间长导致程序假死状态

1 首先要引用 MDAC 2.5 以上版本。使用 VB 6.0 以上平台。
2 声明变量如 Dim WithEvents rs As ADODB.Recordset
3 必须使用客户游标。
4 打开记录集时必须指定 adAsyncFetch

Dim WithEvents rs As ADODB.Recordset

Set rs = New ADODB.Recordset
With rs
     .CursorLocation = adUseClient
     
     .Properties("Initial Fetch Size") = 2
     .Properties("Background Fetch Size") = 4
     .Open strSQL, cn, , , adAsyncFetch
End With

事件:(当然你可以使用进度条)
Private Sub rs_FetchProgress(ByVal Progress As Long, ByVal MaxProgress As Long, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
      Debug.Print "Fetch: " & Progress & _
                  "  Max: " & MaxProgress
End Sub

Private Sub rs_FetchComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
      If adStatus <> adStatusOK Then
         Debug.Print "Failed"
         Debug.Print "Error: " & pError.Number & " - " & pError.Description
      Else
         Set DataGrid1.DataSource = pRecordset
         Debug.Print "Done"
      End If
End Sub
 

你可能感兴趣的:(VB)