tchous
asked on
Fetching Data Asynchronously and progress bar
This is a two part question but related:
1. WHat is the difference between fetching data asynchronously as opposed to synchronously.
2. Which brings me to this question: I bought a code snippet on using a progress bar for two things: one to show how long it takes the records to be brought back from the database, adn the other is processing those records.. this is what I came up with.
Private WithEvents cnnSQL As ADODB.Connection
Private WithEvents rs As ADODB.Recordset
Private Sub Command1_Click()
Dim maxrecords As Long
Set cnnSQL = New ADODB.Connection
Set rs = New ADODB.Recordset
mydsn = "DSN=ARSystem3;uid=myid;pw d=mypwd"
cnnSQL.Open mydsn
rs.Open "data_count_sel(965520000, 966124799)", cnnSQL
maxrecords = rs("recordcount")
rs.Close
ProgressBar1.Max = maxrecords
rs.CursorLocation = adUseClient
rs.Open "data_sel(965520000, 966124799)", cnnSQL, adOpenStatic, adLockReadOnly, adAsyncFetch
Do While rs.State <> adStateOpen
DoEvents
Loop
ProgressBar1.Value = 0
Dim i As Long
i = 0
Do Until rs.EOF
i = i + 1
ProgressBar1.Value = i
rs.MoveNext
Loop
rs.Close
cnnSQL.Close
Set rs = Nothing
Set cnnSQL = Nothing
End Sub
Private Sub Command2_Click()
rs.Cancel
End Sub
Private Sub rs_FetchProgress(ByVal Progress As Long, ByVal MaxProgress As Long, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
ProgressBar1.Value = Progress
'lblprogress.Caption = "Records Fetched: " & Progress & " / " & MaxProgress
End Sub
The progress bar works for both .. I just want to make sure that this is the most efficient way of processing this code. I don't wnat to just copy the work, i want to learn it.. Here are some code snippets I would like to understand what is happening..
'WHat I think is happening is it is the point in time
'where the data is being fetched, up until the recordset
'is actually populated..
Do While rs.State <> adStateOpen
DoEvents
Loop
How and where does this get called. And what other things could this be used for...
Private Sub rs_FetchProgress(ByVal Progress As Long, ByVal MaxProgress As Long, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
ProgressBar1.Value = Progress
'lblprogress.Caption = "Records Fetched: " & Progress & " / " & MaxProgress
End Sub
I think this is a fairly easy question, but because of the length of it, I will post as moderate..
thanks for the help
1. WHat is the difference between fetching data asynchronously as opposed to synchronously.
2. Which brings me to this question: I bought a code snippet on using a progress bar for two things: one to show how long it takes the records to be brought back from the database, adn the other is processing those records.. this is what I came up with.
Private WithEvents cnnSQL As ADODB.Connection
Private WithEvents rs As ADODB.Recordset
Private Sub Command1_Click()
Dim maxrecords As Long
Set cnnSQL = New ADODB.Connection
Set rs = New ADODB.Recordset
mydsn = "DSN=ARSystem3;uid=myid;pw
cnnSQL.Open mydsn
rs.Open "data_count_sel(965520000,
maxrecords = rs("recordcount")
rs.Close
ProgressBar1.Max = maxrecords
rs.CursorLocation = adUseClient
rs.Open "data_sel(965520000, 966124799)", cnnSQL, adOpenStatic, adLockReadOnly, adAsyncFetch
Do While rs.State <> adStateOpen
DoEvents
Loop
ProgressBar1.Value = 0
Dim i As Long
i = 0
Do Until rs.EOF
i = i + 1
ProgressBar1.Value = i
rs.MoveNext
Loop
rs.Close
cnnSQL.Close
Set rs = Nothing
Set cnnSQL = Nothing
End Sub
Private Sub Command2_Click()
rs.Cancel
End Sub
Private Sub rs_FetchProgress(ByVal Progress As Long, ByVal MaxProgress As Long, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
ProgressBar1.Value = Progress
'lblprogress.Caption = "Records Fetched: " & Progress & " / " & MaxProgress
End Sub
The progress bar works for both .. I just want to make sure that this is the most efficient way of processing this code. I don't wnat to just copy the work, i want to learn it.. Here are some code snippets I would like to understand what is happening..
'WHat I think is happening is it is the point in time
'where the data is being fetched, up until the recordset
'is actually populated..
Do While rs.State <> adStateOpen
DoEvents
Loop
How and where does this get called. And what other things could this be used for...
Private Sub rs_FetchProgress(ByVal Progress As Long, ByVal MaxProgress As Long, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
ProgressBar1.Value = Progress
'lblprogress.Caption = "Records Fetched: " & Progress & " / " & MaxProgress
End Sub
I think this is a fairly easy question, but because of the length of it, I will post as moderate..
thanks for the help
1. async lets your program go about its business while it is getting the records while sync makes you wait until all records come back before doing anything else
in the great words of msdn it says that the fetchprogress gets called "periodically" while fetching records to report progress...You would think they could be a little more specific.
<ping>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the further information..