Solved

Search inside a loop

Posted on 2002-04-22
4
158 Views
Last Modified: 2010-05-02
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
 
0
Comment
Question by:smurray3
4 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 200 total points
ID: 6961566
The best choice here is to transform this into a timed loop using a timer *control*.

In other words, add a timer control which will check every, say 1/2 second for the desired condition, after which it will perform the loop code:

Private Sub Timer1_Timer()
  if (Condition) then
    Call SomeProcedure
    Timer1.Enabled = False
  endif
End sub

private sub SomeProcedure()
' pretty much the code in your question, minus the timed parts
end sub
0
 
LVL 1

Expert Comment

by:alfanhendro
ID: 6961875
-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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7660660
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
0
 

Expert Comment

by:SpideyMod
ID: 7755371
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now