I would like to delete everything after a "-" in a textbox. How would I do this? Please help if you can. Thank You.
Printable View
I would like to delete everything after a "-" in a textbox. How would I do this? Please help if you can. Thank You.
Depending on the event you want this to occur in, you would use something like this.
Also, you would want to place some error trapping in there too in case there is no "-" found.VB Code:
Text1.Text = Mid$(Text1.Text, 1, InStr(1, tText1.Text, "-") - 1)
VB Code:
text1.text = Left(text1.text,instr(text1.text,"-")-1)
Will work as long as there is a - in the text. it will fail otherwise, so you should maybe use an error trap.
You need to do a -1 if you dont want the dash.
Quote:
Originally Posted by dglienna
Ok I think I messed up what I wanted to say I want to take everything before the "-" and put it in text2 and leave text1 the way it is. so if text1 is yay - text
text2 will become yay and text1 will stay the same
IT's even simpler then.
VB Code:
Text2.Text = Mid$(Text1.Text, 1, InStr(1, tText1.Text, "-") - 1)
ROB - I realizzed that when we both posted at the same time. Instead of blindly following, I pasted it into the IDE to see that I had made an error, and I changed it. We posted at the same time.
Andy, this will do it.
VB Code:
text2.text = Left(text1.text,instr(text1.text,"-")-1)
Oh, you must of edited it quite quick. It doesnt show "Last edited by dglienna at xx:xx:xx" :D :thumb:
Down to the same minute :p
I've noticed that it doesn't show up if you edit the post right away. I tend to do that a lot. I thought the difference may have been using MID as opposed to left, and had the IDE open anyways. Just created a textbox and a button, pasted in the code, added "- asfhhl" to the word "Text" and hit the button. Should have tested first, once again.
Thank You all I got it to work with dglienna's post of
:)VB Code:
text2.text = Left(text1.text,instr(text1.text,"-")-1)
Glad to hear it. Keep coming back! :wave: