General VBA Class Question

Fuse3k

Registered User.
Local time
Today, 02:41
Joined
Oct 24, 2007
Messages
74
So, I've created a custom class to scan a folder for new files, open them, scrap the text and add the data to a table. The directory has several files so the process can take 5+ minutes.

I am trying to have a procedure in the class to provide a status report, ultimately do so something like this:

Code:
Dim fs as new clsFileScanner

fs.scan

do until fs.complete
     debug.print fs.progress
loop

Problem is, the Scan procedure seems to lock up the class until it's complete. Using doevents doesn't seem to release the thread.

Any ideas on how I can get progress of a procedure as it's running?

TIA!
-Brian
 
Get the Scan routine to raise events. Then handle those events in the class that hosts your Scanner. So add an event to your scanner class . . .

Code:
Public Event ScanProgress(PercentComplete As Single)

Then your scan routine needs to calculate percent complete and periodically raise this event . . .

Code:
Sub Scan
[COLOR="Green"]'   this is the Scan method of you custom class
'   which periodically raises a ScanProgress event, which other
'   consumers may, or may not, handle[/COLOR]
    Do while scanning
[COLOR="Green"]        'do scanning process[/COLOR]
        Raise ScanProgress(Me.PercentComplete)
    Loop
End Sub

Then declare the clsScanner in a WithEvents variable, create an instance, and handle the events . .
Code:
[COLOR="Green"]'this code appears on the form from which you run the scan[/COLOR]
Private WithEvents cfs As clsFileScanner

Private Sub cmdStartScan_Click()
[COLOR="Green"]'  creates an instance of your class and runs its Scan method[/COLOR]
   set cfs = new clsFileScanner
   cfs.Scan
End Sub

Private Sub cfs_ScanProgress(PercentComplete As Single)
[COLOR="Green"]'   handles your custom ScanProgress event and displays it in a textbox[/COLOR]
    Me.tbMessage = PercentComplete & "% Complete"
End Sub

Hope this helps,
 
That's what I was looking for. Thanks, Mark!

Small correction on the first code snippet for those that come across this thread:

Instead of:
Code:
Raise ScanProgress(Me.PercentComplete)

Use:
Code:
Raise[B]Event[/B] ScanProgress(Me.PercentComplete)
 
Thanks for noting that correction!!!
Cheers,
 

Users who are viewing this thread

Back
Top Bottom