Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

'Search Next' function in an array?

Posted on 2004-03-23
11
Medium Priority
?
215 Views
Last Modified: 2010-05-01
I'm populating an array from a text file..

I'm performing a search on this array... with:
Open MyFile For Input As #1
   Do Until EOF(1)
   If StrArray(1) = Text1.text then
          ....
   End if
Loop
Close

however, there can possibly be more than one match in the column, how can I perform a "Find Next"?





0
Comment
Question by:NO_CARRIER
11 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 10659889
Greetings NO_CARRIER!
Can you provide an example of the file?
Odds are you can use
 Line Input #1, strArray(1)
to pull in the full string, but I may be missing your point here..? :D
~sirbounty
0
 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10659967
Sure...

code:

MyFile = "c:\test.txt"

Open MyFile For Input As #1
   Do Until EOF(1)
      Line Input #1, LineText
      strArray=Split(LineText, "/")
   If StrArray(1) = Text1.text then
          ....
   End if
Loop
Close


file (test.txt)

1/Hello
2/Marker
3/Pencil
4/Dog
5/Cat
1/World



--- notice the number "1" is there twice.  (once with Hello, again with World).  When I perform a search for the number "1" .. it returns "Hello".  If I click the Search button a second time, I'd like it to return "World" ... or any other instances of number "1" each time I click Search.


I hope this helps?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10660002
How about this way instead?

 Do Until EOF(1)
      Line Input #1, LineText
      if instr(1,LineText,Text1.text) > 0 then

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:NO_CARRIER
ID: 10660004
sorry, in that code...

the .... should be: MsgBox strArray(2)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10660022
NO_CARRIER,
Didn't have my window opened up enough to read the tail-end of that...sorry!
But, just slightly modify the above:


Do Until EOF(1)
      Line Input #1, LineText
      FullText=LineText & ","
Loop
      if instr(1,FullText,Text1.text) > 0 then...
0
 
LVL 5

Accepted Solution

by:
jayeshshah earned 375 total points
ID: 10660063
what u could do i search for the string after the file reading is completed.

see below

    Dim x As String
    Dim x1() As String
    Dim x2() As String
   
  Open "c:\test.txt" For Input As #1
    Do Until EOF(1)
       Line Input #1, LineText
        '' code for data population in array x1
    Loop
  Close

    x2 = Filter(x1, "1/")


In the below example let's assume that u have your contents in array x1.  let's assume u want to search  the word '1/'. It shall give u all the text of the line where the word '1/' is occuring.

Regards

JBS
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10660236
Open ("C:\test.txt") For Input As #1
Do Until EOF(1)
    Line Input #1, LineText
    fulltext = LineText & "," & fulltext
Loop
x = 1
Do While x <> 0
    strFind = Mid(fulltext, x)
    strFind = Mid(fulltext, x, (InStr(strFind, ",")) - 1)
    strreply = strFind & vbCrLf & strreply
    x = x + 1
    x = InStr(x, fulltext, Text1.Text)
Loop
'And if you didn't want the Msgbox in the format of
'   1/Hello
'   1/World
' You can always split it...
'strResults = Split(strreply, "/")

End Sub
0
 
LVL 4

Expert Comment

by:learning_t0_pr0gram
ID: 10660500
this is simple.. all you need to do is:


Dim xArr() As String
Dim xText As String
Open TheFile For Input As #1
While Not EOF(1)
Line Input #1, xText
xArr() = Split(xText, "/")
If xArr(0) = Text1.Text Then
'whatever u wanna do with it
MsgBox xArr(1)
End If
Wend
Close #1
0
 
LVL 15

Expert Comment

by:SRigney
ID: 10660546
There are a few ways you can handle this.

1. You can read the entire file and have the search actually looking through a cached string of the entire file, from which you can easily move forward and backwards.

2.  You can open the file for random access and keep track of where you were last, thus allowing you to seek back to that starting point and continue searching from there.

3.  You can move your file open to a module level and have the search only deal with searching from where it left off last.

If you need to start over you could close the file if your search field were changed, or you could provide a button that would allow the user to choose refresh and it would close the file and reopen it at the beginning to handle the next search.
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 10660851
You should just buffer the file (sArray) and keep a track on where the last number was found (lPos). Example (Form1):

Option Explicit

Private sArray() As String, lPos As Long
Private Function ArrayLoad(ByVal sFile As String) As Boolean
    Dim sBuff As String, iFF As Integer
    If Dir(sFile, vbNormal + vbHidden) <> vbNullString Then
        iFF = FreeFile
        Open sFile For Binary As iFF
            sBuff = Space(LOF(iFF))
            Get #iFF, 1, sBuff
        Close iFF
        sArray() = Split(sBuff, vbCrLf)
        ArrayLoad = True
    End If
End Function
Private Function ArraySearch(ByVal bFindNext As Boolean, ByVal lFindNumber As Long) As Long
    Dim lLoop As Long, sSearchArray() As String
    If bFindNext = False Then lPos = 0
    For lLoop = lPos To UBound(sArray)
        sSearchArray = Split(sArray(lLoop), "/")
        If UBound(sSearchArray) = 1 Then
            If sSearchArray(0) = lFindNumber Then
                ArraySearch = lLoop
                lPos = lLoop + 1
                Exit Function
            End If
        End If
    Next lLoop
    ArraySearch = -1
End Function
Private Sub Command1_Click(Index As Integer)
    Dim lRet As Long
    '2 Command buttons.. Index 0 and Index 1
    'Index 0 = Find
    'Index 1 = Find Next
    lRet = ArraySearch(IIf(Index = 0, False, True), 1)
    If lRet > -1 Then
        MsgBox sArray(lRet)
    Else
        MsgBox "Could not find that number."
    End If
End Sub
Private Sub Form_Load()
    If ArrayLoad("c:\test.txt") = False Then
        Call MsgBox("Could not load file.")
    End If
End Sub
0
 
LVL 6

Expert Comment

by:___XXX_X_XXX___
ID: 10668979
NO_CARRIER:

Look at VB function Filter. It searches for some match in string array.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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

571 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