InStr issue 
Author Message
 InStr issue

I'm trying to handle multiple exit codes when running an external app in VB6.
InStr doesn't work like I would expect it to. It says "-1" exists in the
string "1 1030", and also says "(-1)" exists in "(1) (1030)":

' Hard-coded the return value for this sample code:
strReturnValue = "-1"
strAlternateExitCodes = "1 1030"
If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
  ' This says it found "-1" in "1 1030"
End If

' With parens:
strReturnValue = Chr(40) & "-1" & Chr(41)
strAlternateExitCodes = "(1) (1030)"
If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
  ' This says it found "(-1)" in "(1) (1030)"
End If

Am I missing something here? Is there a better way?

--
Bill Baker



Wed, 11 Jul 2012 08:34:01 GMT  
 InStr issue
WB formulated on Friday :

Quote:
> I'm trying to handle multiple exit codes when running an external app in VB6.
> InStr doesn't work like I would expect it to. It says "-1" exists in the
> string "1 1030", and also says "(-1)" exists in "(1) (1030)":

> ' Hard-coded the return value for this sample code:
> strReturnValue = "-1"
> strAlternateExitCodes = "1 1030"
> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>   ' This says it found "-1" in "1 1030"
> End If

> ' With parens:
> strReturnValue = Chr(40) & "-1" & Chr(41)
> strAlternateExitCodes = "(1) (1030)"
> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>   ' This says it found "(-1)" in "(1) (1030)"
> End If

> Am I missing something here? Is there a better way?

I think we need to see some real code, to say for sure.  What you
posted won't run "as is."  Modified to look like this:

   Public Sub test()
      Dim strReturnValue As String
      Dim strAlternateExitCodes As String
      ' Hard-coded the return value for this sample code:
      strReturnValue = "-1"
      strAlternateExitCodes = "1 1030"
      Debug.Print InStr(1, strReturnValue, strAlternateExitCodes)

      ' With parens:
      strReturnValue = Chr(40) & "-1" & Chr(41)
      strAlternateExitCodes = "(1) (1030)"
      Debug.Print InStr(1, strReturnValue, strAlternateExitCodes)
   End Sub

I get the expected results:

   0
   0

--
.NET: It's About Trust!
http://vfred.mvps.org



Wed, 11 Jul 2012 08:53:38 GMT  
 InStr issue


Quote:
> I'm trying to handle multiple exit codes when running an external app
> in VB6. InStr doesn't work like I would expect it to. It says "-1"
> exists in the string "1 1030", and also says "(-1)" exists in "(1)
> (1030)":

> ' Hard-coded the return value for this sample code:
> strReturnValue = "-1"
> strAlternateExitCodes = "1 1030"
> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>   ' This says it found "-1" in "1 1030"
> End If

> ' With parens:
> strReturnValue = Chr(40) & "-1" & Chr(41)
> strAlternateExitCodes = "(1) (1030)"
> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>   ' This says it found "(-1)" in "(1) (1030)"
> End If

> Am I missing something here? Is there a better way?

Putting this in a command button procedure never says it found it.

You've got the order of the strings wrong, but neither way said it found
it. It's......Instr(1,[StringToSearch],[FindThisString]

Private Sub Command1_Click()

        Dim strReturnValue As String
        Dim strAlternateExitCodes As String
        ' Hard-coded the return value for this sample code:
        strReturnValue = "-1"
        strAlternateExitCodes = "1 1030"
        If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
            Debug.Print "-1 found"
            ' This says it found "-1" in "1 1030"
        End If

End Sub

Possibly your hard-coded value shown here as "-1" is *not* "-1" in the
production code ?



Wed, 11 Jul 2012 09:08:32 GMT  
 InStr issue


Quote:
> WB formulated on Friday :
>> I'm trying to handle multiple exit codes when running an external app
>> in VB6. InStr doesn't work like I would expect it to. It says "-1"
>> exists in the string "1 1030", and also says "(-1)" exists in "(1)
>> (1030)":

>> ' Hard-coded the return value for this sample code:
>> strReturnValue = "-1"
>> strAlternateExitCodes = "1 1030"
>> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>>   ' This says it found "-1" in "1 1030"
>> End If

>> ' With parens:
>> strReturnValue = Chr(40) & "-1" & Chr(41)
>> strAlternateExitCodes = "(1) (1030)"
>> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>>   ' This says it found "(-1)" in "(1) (1030)"
>> End If

>> Am I missing something here? Is there a better way?

> I think we need to see some real code, to say for sure.  What you
> posted won't run "as is."  Modified to look like this:

>    Public Sub test()
>       Dim strReturnValue As String
>       Dim strAlternateExitCodes As String
>       ' Hard-coded the return value for this sample code:
>       strReturnValue = "-1"
>       strAlternateExitCodes = "1 1030"
>       Debug.Print InStr(1, strReturnValue, strAlternateExitCodes)

>       ' With parens:
>       strReturnValue = Chr(40) & "-1" & Chr(41)
>       strAlternateExitCodes = "(1) (1030)"
>       Debug.Print InStr(1, strReturnValue, strAlternateExitCodes)
>    End Sub

> I get the expected results:

>    0
>    0

Of course Karl.......

.......you will *never* find the string "(1) (1030)" contained within the
string "-1".



Wed, 11 Jul 2012 09:13:39 GMT  
 InStr issue
DanS presented the following explanation :

Quote:


>> WB formulated on Friday :
>>> I'm trying to handle multiple exit codes when running an external app
>>> in VB6. InStr doesn't work like I would expect it to. It says "-1"
>>> exists in the string "1 1030", and also says "(-1)" exists in "(1)
>>> (1030)":

>>> ' Hard-coded the return value for this sample code:
>>> strReturnValue = "-1"
>>> strAlternateExitCodes = "1 1030"
>>> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>>>   ' This says it found "-1" in "1 1030"
>>> End If

>>> ' With parens:
>>> strReturnValue = Chr(40) & "-1" & Chr(41)
>>> strAlternateExitCodes = "(1) (1030)"
>>> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>>>   ' This says it found "(-1)" in "(1) (1030)"
>>> End If

>>> Am I missing something here? Is there a better way?

>> I think we need to see some real code, to say for sure.  What you
>> posted won't run "as is."  Modified to look like this:

>>    Public Sub test()
>>       Dim strReturnValue As String
>>       Dim strAlternateExitCodes As String
>>       ' Hard-coded the return value for this sample code:
>>       strReturnValue = "-1"
>>       strAlternateExitCodes = "1 1030"
>>       Debug.Print InStr(1, strReturnValue, strAlternateExitCodes)

>>       ' With parens:
>>       strReturnValue = Chr(40) & "-1" & Chr(41)
>>       strAlternateExitCodes = "(1) (1030)"
>>       Debug.Print InStr(1, strReturnValue, strAlternateExitCodes)
>>    End Sub

>> I get the expected results:

>>    0
>>    0

> Of course Karl.......

> .......you will *never* find the string "(1) (1030)" contained within the
> string "-1".

As I said, the *expected* results... <eg>

--
.NET: It's About Trust!
http://vfred.mvps.org



Wed, 11 Jul 2012 09:34:27 GMT  
 InStr issue
What happens if you change the <> to >? What's the return value that
you are getting from InStr?


Quote:
> I'm trying to handle multiple exit codes when running an external app in VB6.
> InStr doesn't work like I would expect it to. It says "-1" exists in the
> string "1 1030", and also says "(-1)" exists in "(1) (1030)":

> ' Hard-coded the return value for this sample code:
> strReturnValue = "-1"
> strAlternateExitCodes = "1 1030"
> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
> ? ' This says it found "-1" in "1 1030"
> End If

> ' With parens:
> strReturnValue = Chr(40) & "-1" & Chr(41)
> strAlternateExitCodes = "(1) (1030)"
> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
> ? ' This says it found "(-1)" in "(1) (1030)"
> End If

> Am I missing something here? Is there a better way?

> --
> Bill Baker



Wed, 11 Jul 2012 09:47:58 GMT  
 InStr issue


Quote:
> DanS presented the following explanation :


>>> WB formulated on Friday :
>>>> I'm trying to handle multiple exit codes when running an external
>>>> app in VB6. InStr doesn't work like I would expect it to. It says
>>>> "-1" exists in the string "1 1030", and also says "(-1)" exists in
>>>> "(1) (1030)":

>>>> ' Hard-coded the return value for this sample code:
>>>> strReturnValue = "-1"
>>>> strAlternateExitCodes = "1 1030"
>>>> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>>>>   ' This says it found "-1" in "1 1030"
>>>> End If

>>>> ' With parens:
>>>> strReturnValue = Chr(40) & "-1" & Chr(41)
>>>> strAlternateExitCodes = "(1) (1030)"
>>>> If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>>>>   ' This says it found "(-1)" in "(1) (1030)"
>>>> End If

>>>> Am I missing something here? Is there a better way?

>>> I think we need to see some real code, to say for sure.  What you
>>> posted won't run "as is."  Modified to look like this:

>>>    Public Sub test()
>>>       Dim strReturnValue As String
>>>       Dim strAlternateExitCodes As String
>>>       ' Hard-coded the return value for this sample code:
>>>       strReturnValue = "-1"
>>>       strAlternateExitCodes = "1 1030"
>>>       Debug.Print InStr(1, strReturnValue, strAlternateExitCodes)

>>>       ' With parens:
>>>       strReturnValue = Chr(40) & "-1" & Chr(41)
>>>       strAlternateExitCodes = "(1) (1030)"
>>>       Debug.Print InStr(1, strReturnValue, strAlternateExitCodes)
>>>    End Sub

>>> I get the expected results:

>>>    0
>>>    0

>> Of course Karl.......

>> .......you will *never* find the string "(1) (1030)" contained within
>> the string "-1".

> As I said, the *expected* results... <eg>

Yes, that is what you said, and that is in fact correct.

Touche'.



Wed, 11 Jul 2012 11:10:56 GMT  
 InStr issue
Sorry, this was a combination of the reversed InStr parameters that DanS
mentioned, and also a logic error on my part.

Thanks DanS (and everyone else).
--
Bill Baker

Quote:



> > I'm trying to handle multiple exit codes when running an external app
> > in VB6. InStr doesn't work like I would expect it to. It says "-1"
> > exists in the string "1 1030", and also says "(-1)" exists in "(1)
> > (1030)":

> > ' Hard-coded the return value for this sample code:
> > strReturnValue = "-1"
> > strAlternateExitCodes = "1 1030"
> > If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
> >   ' This says it found "-1" in "1 1030"
> > End If

> > ' With parens:
> > strReturnValue = Chr(40) & "-1" & Chr(41)
> > strAlternateExitCodes = "(1) (1030)"
> > If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
> >   ' This says it found "(-1)" in "(1) (1030)"
> > End If

> > Am I missing something here? Is there a better way?

> Putting this in a command button procedure never says it found it.

> You've got the order of the strings wrong, but neither way said it found
> it. It's......Instr(1,[StringToSearch],[FindThisString]

> Private Sub Command1_Click()

>         Dim strReturnValue As String
>         Dim strAlternateExitCodes As String
>         ' Hard-coded the return value for this sample code:
>         strReturnValue = "-1"
>         strAlternateExitCodes = "1 1030"
>         If InStr(1, strReturnValue, strAlternateExitCodes) <> 0 Then
>             Debug.Print "-1 found"
>             ' This says it found "-1" in "1 1030"
>         End If

> End Sub

> Possibly your hard-coded value shown here as "-1" is *not* "-1" in the
> production code ?
> .



Sun, 15 Jul 2012 08:40:01 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Office XP Mail Merge issue Insertdatabase issue and problems with copying a macro

2. Issue 6 and Issue 7 fanzine notes

3. BASIX Fanzine Issue 9 **NEW ISSUE** - basix09.txt (1/1)

4. InStr(cC(i), '/') vs InStr(cC(i), "/")

5. Instr()

6. InStrRev & InStr

7. InStr Help Needed

8. InStr Function Help

9. Instr function

10. Bug in INSTR function

11. InStr question

 

 
Powered by phpBB® Forum Software