I want to put part of txt1.text (everything before the /) into txt2.
e.g. txt1 is 123/abc, txt2 should be 123
so I wrote:
pos = InStr(1, txt1, "/")
txt2.Text = Left(txt1, pos - 1)
but I got run-time error: 5
but if I use '+' instead of '-' (i.e. txt2.Text = Left(txt1, pos + 1)
then it works fine.
any idea?
thanks!
kate
Gary Nelson - 30 Dec 2004 10:02 GMT
Kate,
Probably txt1 does not contain a "/" and pos is returning a 0, and therefore
pos-1 = -1 and Left is producing the error 5.
stop the code before the line txt2.Text = Left(txt1, pos - 1) and check the
values of pos and txt1.
Gary
>I want to put part of txt1.text (everything before the /) into txt2.
> e.g. txt1 is 123/abc, txt2 should be 123
[quoted text clipped - 11 lines]
> thanks!
> kate
Jan Hyde - 30 Dec 2004 10:05 GMT
"Kate" <Kate@discussions.microsoft.com>'s wild thoughts were
released on Thu, 30 Dec 2004 01:43:03 -0800 bearing the
following fruit:
>I want to put part of txt1.text (everything before the /) into txt2.
>e.g. txt1 is 123/abc, txt2 should be 123
[quoted text clipped - 6 lines]
>but if I use '+' instead of '-' (i.e. txt2.Text = Left(txt1, pos + 1)
>then it works fine.
There is no "/" in the string hence pos = 0
Although '+' may make the code run, it should not provide
you with the correct data.
Jan Hyde (VB MVP)

Signature
Meditation is not what you think.
[Abolish the TV License - http://www.tvlicensing.biz/]
Rick Rothstein - 30 Dec 2004 15:29 GMT
> I want to put part of txt1.text (everything before the /) into txt2.
> e.g. txt1 is 123/abc, txt2 should be 123
[quoted text clipped - 8 lines]
>
> any idea?
As others have pointed out, your txt1 variable does not contain a slash
character in it. One possible way to handle this problem (assuming you
want to use the entire value of txt1 when no slash appears in it) is to
provide a trailing slash for InStr to find when your user provides none.
Change the first line of code you posted to this (leave the second line
as you have it - with the minus sign)...
pos = InStr(1, txt1 & "/", "/")
Then, if your user provides a slash, that one will be found first
(before the one that you added to the end of the string) and your code
will work as expected. If the user doesn't provide a slash character,
InStr will find the one you added at the end of the string (it will be
the only one in there) and, again, your code will work correctly. Give
it a try.
Rick - MVP
Kate - 31 Dec 2004 02:57 GMT
thanks everyone!
pos = InStr(1, txt1 & "/", "/")
works!!
but I still don't understand... still using 123/abc example
when I used:
pos = InStr(1, txt1, "/")
txt2.Text = Left(txt1, pos + 1)
I got the correct result: 123/a
and I also tried:
pos = InStr(1, txt1, "a")
txt2.Text = Left(txt1, pos - 1)
I still got the run-time error 5.
happy new year!
kate
> > I want to put part of txt1.text (everything before the /) into txt2.
> > e.g. txt1 is 123/abc, txt2 should be 123
[quoted text clipped - 26 lines]
>
> Rick - MVP
NickHK - 31 Dec 2004 04:14 GMT
Kate,
Just a guess, but are you sure you are not setting txt1 by mistake, instead
of txt2 the first time around;
i.e. txt1.Text = Left(txt1, pos - 1)
Then the second time there is now no "a" to find, hence the error.
By the way, any reason why you use "txt2.Text", but "txt1" ?
NickHK
> thanks everyone!
>
[quoted text clipped - 46 lines]
> >
> > Rick - MVP