Solved

'Search Next' function in an array?

Posted on 2004-03-23
11
193 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Accepted Solution

by:
jayeshshah earned 125 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

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…

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

12 Experts available now in Live!

Get 1:1 Help Now