user3245060
user3245060

Reputation: 1

How to search for more than one string in VB6?

I want to search for more than one string in a file with vb6 using instr we can do it for single string but I don't know how to use instr for more than one string now how can I search for more than one and if find one of them we receive a message?

Open file For Binary As #1
    strData = Space$(FileLen(file))
    Get #1, , strData
Close #1
lngFind = InStr(1, strData, string)

Upvotes: 0

Views: 1816

Answers (2)

Affiliate Switchblade
Affiliate Switchblade

Reputation: 19

'-----------------------------------------------------------
'perform multiple instr on a string. returns true if all instr pass
'-----------------------------------------------------------

Function bMultiInstr(sToInspect As String, ParamArray sArrConditions()) As     Boolean
On Error GoTo err:
Dim i As Integer, iUpp As Integer

iUpp = UBound(sArrConditions) 'instr conditions

For i = 0 To iUpp ' loop them
       If InStr(1, sToInspect, sArrConditions(i)) <= 0 Then Exit Function '     if instr returns 0 then exit - [bPasses] will be left false
Next i

bPasses = True


Exit Function
err:
With err
     If .Number <> 0 Then
        'create .bas named [ErrHandler]  see http://vb6.info/h764u
        ErrHandler.ReportError Date & ": Strings.bMultiInstr." & err.Number     & "." & err.Description
        Resume Next
      End If
End With
End Function

That is from http://vb6.info/string/instr-multi-perform-instr-checks-multiple-inst-conditions-function/

Upvotes: 0

user1945782
user1945782

Reputation:

That's simply a case of introducing multiple tests for multiple strings...

Dim strArray(10) As String
DIm cntArray(10) As Integer
Dim strData As String
Dim c As Integer

'Set-up your search strings...
...

Open file For Binary As #1
Get #1, , strData
Close #1

For c = 1 to 10
    cntArray(c) = Instr(strData, strArray(c))
Next c

If all you want to do is show a true or false message box then we don't need to assign the value to the second array. The For loop could be replaced with...

For c = 1 to 10
    If Instr(strData, strArray(c)) > 0 Then
        MsgBox "'" & strArray(c) & "' found in file."
        'Remove the following line if you want everything to be searched for,
        'but leave it in if you only want the first string found...
        Exit For
    End If
Next c

Really this is a very basic piece of code. If you're looking to write code as anything but a novice then you need to research the commands, functions and structures included in this post. A good place to start, for a complete novice, would be somewhere like http://www.thevbprogrammer.com/classic_vbtutorials.asp or http://www.vb6.us/.

Upvotes: 2

Related Questions