From: WB on
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
From: Karl E. Peterson on
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

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


From: DanS on
=?Utf-8?B?V0I=?= <WB(a)discussions.microsoft.com> wrote in
news:A967A23F-6FFD-4964-8C3C-C952174FCA1F(a)microsoft.com:

> 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 ?
From: DanS on
Karl E. Peterson <karl(a)exmvps.org> wrote in
news:elxwBa8mKHA.5692(a)TK2MSFTNGP04.phx.gbl:

> 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".
From: Karl E. Peterson on
DanS presented the following explanation :
> Karl E. Peterson <karl(a)exmvps.org> wrote in
> news:elxwBa8mKHA.5692(a)TK2MSFTNGP04.phx.gbl:
>
>> 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


 |  Next  |  Last
Pages: 1 2
Prev: ASASAS
Next: Passing an array of strings from VB to C