Link to home
Start Free TrialLog in
Avatar of smurray3
smurray3

asked on

Search inside a loop

I have an application that waits for a string of text prior to continuing to the next step. In the code below I would like to wait for the text(itfind) but have the RegExp object (re1.Pattern)looping through the text also. The only problem is when it finds the text it will print the findings until it exits the loop.

I need to set a counter (ScreenImage.Count) and advance to the next word without searching the beginning.

(ScreenImage.Count will give you which line you are on. )


 Dim itFind As String
   Dim itString As String
   Dim itCounter As Long
   Dim itSearch As String
   itFind = False
   Match = "0"

Do While itFind = False
   
    itSearch = Mid$(line_command$, 10)
    itCounter = InStr(Term.Text, itSearch)
    FrmMain.StatusBar.Panels(2).Text = "Verifying Text: " + itSearch
   
   re1.Pattern = "\b(Failure to connect|ping failed:)\b" ' case isn't significant
   re1.IgnoreCase = True   ' we want all occurrences
   re1.Global = True   ' we assume that the string to be parsed is in the sourceText variable
   re1.MultiLine = True
   
For Each ma1 In re1.Execute(Term.Text)
  txtNumberofLines.Text = ScreenImage.Count
If txtNumberofLines.Text >= Text1.Text Then
 For Match = 1 To 1
  FrmMain.StatusBar.Panels(4).Text = ScreenImage.Count
  FrmMain.StatusBar.Panels(2).Text = "Found '" & ma1.Value & "' at index " & ScreenImage.Count
  Print #1, "Found '" & ma1.Value & "' at index " & ScreenImage.Count
   itFind = True
   Local_flag = True
   Global_flag = True
   Fail_flag = True
   Fail_Command$ = line_command$
   FrmMain.Check3D1.Value = 1
 Next Match
End If

 Next
   
     If itCounter Then  'found it
   
     Term.SelStart = itCounter - 1
     Term.SelLength = Len(itSearch)
     itFind = True
                               
      Do While itFind = False
       DoEvents    ' Yield to other processes.
      Loop
                       
      Exit Do
     End If
     'wait for 1 second
     DoEvents
   
    If Timer - lStartTime > 960 Then
      Local_flag = True
     Global_flag = True
     fail_flag = True
     Fail_Command$ = line_command$
     FrmMain.Check3D1.Value = 1
     FrmMain.StatusBar.Panels(2).Text = "Verification Failure"
    Exit Do
    Exit Sub
    End If
    Loop
 
ASKER CERTIFIED SOLUTION
Avatar of rspahitz
rspahitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alfanhendro
alfanhendro

-For match = 1 to 1
 what is it for?
-When do you update the ScreenImage.count?
-What is Text1.Text for?
-Do While itFind = False
     DoEvents    ' Yield to other processes.
 Loop
 This block will never be executed
 Just one line above it you specify that itFind=True
-FrmMain.StatusBar.Panels(4).Text = ScreenImage.Count
 FrmMain.StatusBar.Panels(2).Text = "Found '" & ma1.Value & "' at index " & ScreenImage.Count
 Are you sure that you need these two lines? If I am not mistaken, your eyes will not be fast enough to able to see it anyway.
-If Timer - lStartTime > 960 Then
 Where do you set the value of Timer?
 And why don't you put it as condition of your loop?
 i.e.
 Do While (itFind = False)And(Timer - lStartTime <= 960)
 .
 .
 .
 Loop
 Local_flag = True
    Global_flag = True
    fail_flag = True
    Fail_Command$ = line_command$
    FrmMain.Check3D1.Value = 1
    FrmMain.StatusBar.Panels(2).Text = "Verification Failure"
 Exit Sub'Why do you need this?
-Even if you still want to use the If statement,
 you shouldn't put the Exit Sub right after the Exit Do.
 The Exit Sub will not be executed, since you have exit the loop.(it will skip everything until the line after "Loop")
-As well, why don't you put the block
 If itCounter Then
 .
 .
 .
 End If
 after the loop? Except the line itFind = True to as condition of the loop. Anyway, when your itCounter is True, you will finish the loop. It will make your code more readable.
-Are you aware that you put itFind=True inside the
 "For Each...Next" loop?
 Doesn't it mean that you will always have itFind=True as long as re1.Execute(Term.Text) returns at least one ma1?

I would rewrite the code as following:

Dim itFind As String
  Dim itString As String
  Dim itCounter As Long
  Dim itSearch As String
  itFind = False
  Match = "0"

FrmMain.StatusBar.Panels(2).Text = "Verifying Text: " + itSearch

Do While (itFind = False) And (Timer - lStartTime > 960)
   
   itSearch = Mid$(line_command$, 10)
   itCounter = InStr(Term.Text, itSearch)
    If itCounter Then itFind = True
   
  re1.Pattern = "\b(Failure to connect|ping failed:)\b" ' case isn't significant
  re1.IgnoreCase = True   ' we want all occurrences
  re1.Global = True   ' we assume that the string to be parsed is in the sourceText variable
  re1.MultiLine = True
 
For Each ma1 In re1.Execute(Term.Text)
 txtNumberofLines.Text = ScreenImage.Count
If txtNumberofLines.Text >= Text1.Text Then
 Print #1, "Found '" & ma1.Value & "' at index " & ScreenImage.Count
  itFind = True
  Local_flag = True
  Global_flag = True
  Fail_flag = True
  Fail_Command$ = line_command$
  FrmMain.Check3D1.Value = 1
End If

Next
 

    DoEvents
 
   Loop

    If itCounter Then  'found it
   
    Term.SelStart = itCounter - 1
    Term.SelLength = Len(itSearch)
    itFind = True

    Else

     Local_flag = True
    Global_flag = True
    fail_flag = True
    Fail_Command$ = line_command$
    FrmMain.Check3D1.Value = 1
    FrmMain.StatusBar.Panels(2).Text = "Verification Failure"

                       
    End If


Anyway, I am not sure that this is what you want, but I hope it helps.

Regards
Avatar of DanRollins
Hi smurray3,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept rspahitz's comment(s) as an answer.

smurray3, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange