how to stop search in the middle

R

ravindar thati

hi......
i am using ms access 2003,vb6

i have a form.
in that i have 2 buttons
1. start search
2 stop search


when i click the "start search" button the fucntion SearchSystem() is
called, it will search for a particular file in the computer(searches
entire drives).

the button "stop search" is intended to stop the search.

but while the search is going on, i could not click "stop search", it
is not enabled to click while the search is going on?

how do i approach to stop the search?

Function SearchSystem(DriveTypes As Integer, FileSpec As String,
Optional oFolder = Empty) As Integer

Dim fs As FileSystemObject
Dim dc As Drives
Dim oDrive As Drive
Dim ofld As Folder
Dim file
Dim subdirs As New Collection
Dim subdir
Dim st As String

Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives

On Error GoTo errorhandler:


If oFolder <> "" Then
If cap = 1 Then
Label11.caption = " The applicaion is being searced for in " & oFolder
Me.Refresh
Me.Repaint
Me.Refresh
ElseIf cap = 2 Then
Label11.caption = " The application is being searched for in " &
oFolder
Me.Refresh
Me.Repaint
Me.Refresh

End If

file = Dir$(oFolder & FileSpec)
Do While Len(file)
'File found
file = oFolder & file

If InStr(1, file, "IFs.exe") Then
st = Replace(file, "IFs.exe", "")
ChDir st
Shell file, vbNormalFocus
glob = glob + 1

Exit Function


ElseIf InStr(1, file, "Wdi32.exe") Then
st = Replace(file, "Wdi32.exe", "")
ChDir st
Shell file, vbNormalFocus
glob = glob + 1

Exit Function
End If
'Exit Sub



Debug.Print file
file = Dir$()
Loop
file = Dir$(oFolder & "*.*", vbDirectory)
Do While Len(file)
' we've found a new directory
If file = "." Or file = ".." Then
' exclude "." and ".."
ElseIf (GetAttr(oFolder & file) And vbDirectory) = 0 Then

' ignore regular files
Else
' this is a directory, include the path in the collection
subdirs.Add oFolder & file
End If
' get next directory
file = Dir$()
Loop

For Each subdir In subdirs
If glob = 0 Then
Call SearchSystem(DriveTypes, FileSpec, subdir & "\")
Else

Exit Function
End If
Next

End If



If oFolder = "" Then
For Each oDrive In dc




'Use bitwise expression to see if Drivetype is one of the Drivetypes
we wish to search
If (oDrive.DriveType And DriveTypes) = oDrive.DriveType Then

'MsgBox ("searching")
'oDrive is a drive type we wish to search
Debug.Print "Searching: " & oDrive.path
If oDrive.IsReady Then Call SearchSystem(DriveTypes, FileSpec,
oDrive.path & "\")
End If
Next 'oDrive
Debug.Print "finished"

End If

Set fs = Nothing
Set dc = Nothing

'Exit Sub


errorhandler:
If Err.Number = 75 Then
'Path/File access error (file locked for reading or doesn't exist?)
Debug.Print Err.Description
Resume Next

ElseIf Err.Number = 52 Then

End If


End Function
 
S

Stefan Hoffmann

hi Ravindar,

ravindar said:
when i click the "start search" button the fucntion SearchSystem() is
called, it will search for a particular file in the computer(searches
entire drives).
but while the search is going on, i could not click "stop search", it
is not enabled to click while the search is going on?

how do i approach to stop the search?
You have some loops which needed to be terminated. Use a module varible
to control it:

Option Compare Database
Option Explicit

Private m_SearchStop As Boolean

Private Sub cmdSearchStart()

m_SearchStop = False
SearchSystem()

End Sub

Private Sub cmdSearchStop()

m_SearchStop = True

End Sub

Private Function SearchSystem() As Boolean

DoEvents
Do While Len(file) And Not m_SearchStop
...
DoEvents
Loop

For Each subdir In subdirs
DoEvents
If m_SearchStop Then
Exit For
End If
Next subdir

End Function


mfG
--> stefan <--
 
R

ravindar thati

hi Ravindar,




You have some loops which needed to be terminated. Use a module varible
to control it:

Option Compare Database
Option Explicit

Private m_SearchStop As Boolean

Private Sub cmdSearchStart()

m_SearchStop = False
SearchSystem()

End Sub

Private Sub cmdSearchStop()

m_SearchStop = True

End Sub

Private Function SearchSystem() As Boolean

DoEvents
Do While Len(file) And Not m_SearchStop
...
DoEvents
Loop

For Each subdir In subdirs
DoEvents
If m_SearchStop Then
Exit For
End If
Next subdir

End Function

mfG
--> stefan <--

many thanks friend, it worked perfectly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top