What is wrong with this simple function?

xcannon
10-13-2005, 03:28 PM
I made this function, and 50% of the time when I go to use it, it freezes up. My goal was to make a funcion that gets EVERYTHING between two strings. Here it is:

Public Function GrabItems(ByVal strSource As String, ByVal strStart As String, ByVal strEnd As String, ByVal listAdd As Object) As Boolean
Dim lnCurrentPos As Long
Dim lnTextStart As Long
Dim lnTextEnd As Long
Dim bolContinue As Boolean
Dim strFound As String

bolContinue = True

lnCurrentPos = InStr(1, strSource, strStart, vbTextCompare)
If lnCurrentPos > 0 Then
Do Until bolContinue = False
lnTextStart = lnCurrentPos + Len(strStart)
lnTextEnd = InStr(lnTextStart, strSource, strEnd, vbTextCompare)
If lnTextEnd > 0 And lnTextEnd > lnTextStart Then
strFound = Mid(strSource, lnTextStart, lnTextEnd - lnTextStart)
listAdd.AddItem strFound
lnCurrentPos = InStr(lnTextEnd + Len(strEnd), strSource, strStart, vbTextCompare)
If lnCurrentPos < lnTextStart Then
bolContinue = False
End If
Else
bolContinue = False
End If
Loop
End If
End Function

LaVolpe
10-13-2005, 03:53 PM
I don't think your routine is locking up, it is probably going into an infinite loop.

The only place that can happen is in the following location: add the following check and keep your debug/immediate window open. When the same value is constantly being written, hit Ctrl+Break to break your code. The end result is that you may find that your tracking calculations may need to be adjusted.

If lnCurrentPos < lnTextStart Then
bolContinue = False
Else
' Since there is no setting of bolContinue=False in this part of the IF, this is the
' place that most likely your infinite loop is happening.
Debug.Print "lnCurrentPos = "; lnCurrentPos
' ^^ When this line continually prints the same thing, you have an infinite loop
End If

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum