Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Search inside a loop

Posted on 2002-04-22
4
Medium Priority
?
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 800 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 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