Image of Navigational Map linked to Home / Contents / Search Monitoring progress using RaiseEvent

by Matt Gray - GUI Computing
Image of Line Break

I decided to wrap up some export code into a DLL the other day, for the sole reason that it might be reused a lot and it is just more fun that way. Anyway all this needed to do was to export a recordset to a separated file (either comma-separated or tab or whatever takes your fancy).

I would just need to set properties like recordset, filename, FS (file-separator string) and some others that aren't important in this example. It turned out to be quite an extensive Class that took all of 5 minutes to write, run and test - but it was the fact that I did it this way, instead of writing it as a module, that got me thinking. On large recordsets it would naturally take some time to run - but how much time?

Eventually I decided that the only logical way that my local code would know how far I'd gone with the export was to use an event - cool. I would just fire an event every time through the main loop in my export method and pass it a percentage value so that my local code could know the value and maybe display a progress indicator. All you need to do to achieve this is :

Now when you run the code to do the export, and event will be raised every time through the loop in the class method.

Here is some code to do this :

The class code - altered to show only where event code is used.

Option Explicit

'Declare the event publicly so external modules can see it
Public Event Progress(ByVal nPercent As Double)

'set , let and get properties go here (left out in this example so as not
'to distract from the use of RaiseEvent
'NB properties could include filename, recordset, etc.

Public sub ExportToFile
'This method exports a recordset to a separated file
Dim nPos as long

nPos = 1
'exporting rs  to file code goes here

	'raise the event Progress passing a Percentage value as a Parameter
	'to indicate how much is done so farů
	RaiseEvent Progress(nGetPercentValue(nPos))

	nPos = nPos + 1

Loop	'end exporting code loop

End sub
The local module code looks like this :
'declare the object globally (private to the form) - this ensures the event can be
'seen on the form.  NB the WithEvents statement - without this you can't declare
'an event with mobjExport
Private WithEvents mobjExport As GUIExport.Export

Private Sub cmdExport_Click()
'set properties of mobjExport (not shown here)
'use ExportToFile method to do the work

'Set progress bar to 0 and mousepointer to default
End Sub

Private Sub mobjExport_Progress(ByVal nPercent As Double)
'This is the event Progress - which will be raised everytime
'the RaiseEvent statement is used in the class/dll

'Do stuff while export is in progress - like showing progress bar & hourglass
Busy "Exporting data...", CInt(nPercent)

End Sub

There you have it. A piece of pie, a walk in the park.

Written by: Matt Gray
August '98

Image of Arrow linked to Previous Article Image of Arrow linked to Next Article
Image of Line Break