|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Searching a stringHi
I want to search a string for the first showing of a minus sign eg asc(45) place all the text to the left of the sign in a textbox1 and all the rest in textbox2. can you indicate the best way to do this please Ron wrote:
> Hi Take a look at the InStr, Left, Mid and Right string functions, e.g.> I want to search a string for the first showing of a minus sign eg asc(45) > place all the text to the left of the sign in a textbox1 and all the rest in > textbox2. > can you indicate the best way to do this please Dim Text As String Dim pos As Integer ' Read Text in from wherever ' Text = .... pos = InStr(1, Text, "-") TextBox1.Text = Left$(Text, pos-1) TextBox2.Text = Mid$(Text, pos+1)
Show quote
Hide quote
"S. I. Becker" <stewart@becker.nospam> wrote in message also check for pos = -1news:440D8093.1000005@becker.nospam... > Ron wrote: >> Hi I want to search a string for the first showing of a minus sign eg >> asc(45) place all the text to the left of the sign in a textbox1 and all >> the rest in textbox2. >> can you indicate the best way to do this please > > Take a look at the InStr, Left, Mid and Right string functions, e.g. > > Dim Text As String > Dim pos As Integer > > ' Read Text in from wherever > ' Text = .... > > pos = InStr(1, Text, "-") > TextBox1.Text = Left$(Text, pos-1) > TextBox2.Text = Mid$(Text, pos+1) "Michael C" <nospam@nospam.com> wrote Why bother?> > pos = InStr(1, Text, "-") > > also check for pos = -1 Larry Serflaten wrote:
> "Michael C" <nospam@nospam.com> wrote Why bother indeed, when InStr never returns -1. A "not found" result is > >>> pos = InStr(1, Text, "-") >> also check for pos = -1 > > > Why bother? > > 0. Michael is right in that there should be a test for this though, as Left$(x, -1) will yield an error, but this was lunch-break code. Stewart > >>> pos = InStr(1, Text, "-") You could always use this expression for filling in TextBox1 and eliminate> >> also check for pos = -1 > > > > Why bother? > > > > > Why bother indeed, when InStr never returns -1. A > "not found" result is 0. Michael is right in that there > should be a test for this though, as Left$(x, -1) will > yield an error, but this was lunch-break code. the separate, external error check... TextBox1.Text = Left$(Text, pos + (pos > 0)) Rick "Larry Serflaten" <serfla***@usinternet.com> wrote in message As becker said to avoid an error, although I was incorrect in saying -1.news:OpF9$neQGHA.1868@TK2MSFTNGP09.phx.gbl... > > "Michael C" <nospam@nospam.com> wrote > >> > pos = InStr(1, Text, "-") >> >> also check for pos = -1 > > > Why bother? Show quoteHide quote > > HI,
You can also use the split function dim tstr as string, arry 'get input string tstr = ... arry = split (tstr,"-",2) then arry(0) contains the part before the first minus and arry(1) conatin the rest of the string. HTH Martin Show quoteHide quote "S. I. Becker" <stewart@becker.nospam> wrote in message news:440D8093.1000005@becker.nospam... > Ron wrote: >> Hi I want to search a string for the first showing of a minus sign eg >> asc(45) place all the text to the left of the sign in a textbox1 and all >> the rest in textbox2. >> can you indicate the best way to do this please > > Take a look at the InStr, Left, Mid and Right string functions, e.g. > > Dim Text As String > Dim pos As Integer > > ' Read Text in from wherever > ' Text = .... > > pos = InStr(1, Text, "-") > TextBox1.Text = Left$(Text, pos-1) > TextBox2.Text = Mid$(Text, pos+1) Hi All
I have not tried all your suggested posts as yet, but I have created the following code and I am getting good results but you may be able to offer some better options so I post my code here:- Function myUpdate_Music() Dim pos, myStop As Integer Dim myHold, myHold1, myHold2, myHold3 As String On Error GoTo upDateError With mdiMusic With List_Input myHold = txtTrackName ' txtTrackName contains the string 'search string with InStr pos = InStr(1, myHold, "-") ' search for asc 45 myHold1 = Left$(myHold, pos - 1) myHold2 = Mid$(myHold, pos + 1) myStop = InStr(1, myHold2, "V") ' search for asc 86 myHold3 = Left$(myHold2, myStop - 2) 'exit search post data Music.strTrackNo = .txtTrackNo .txtArtists = myHold3 .txtTrackName = myHold1 Music.strArtists = myHold3 '.txtArtists Music.strTrackName = myHold1 '.txtTrackName If .txtArtists = "" Then .txtArtists = InputBox("Enter Artists Name", "Artists Name Not Shown", , 5000, 3000) Music.strArtists = .txtArtists End If End With .adoMusic.Recordset.MoveLast .adoMusic.Recordset.AddNew .dbgDataBase.Columns(0) = Music.strTrackNo .dbgDataBase.Columns(1) = Music.strTrackName .dbgDataBase.Columns(2) = Music.strArtists End With Call Music.Update_Tracks upDateError: Exit Function End Function '**** I do not see any errors when I run the code, but if I step through the code the step trip gives the correct answers but then the code loops to *** and drops out to my error trap why does it loop? It appears to me that you cannot use asc characters for this search is that correct? Show quoteHide quote "Martin Walke" wrote: > HI, > > You can also use the split function > > dim tstr as string, arry > 'get input string > tstr = ... > > arry = split (tstr,"-",2) > > then arry(0) contains the part before the first minus and arry(1) conatin > the rest of the string. > > HTH > Martin > > "S. I. Becker" <stewart@becker.nospam> wrote in message > news:440D8093.1000005@becker.nospam... > > Ron wrote: > >> Hi I want to search a string for the first showing of a minus sign eg > >> asc(45) place all the text to the left of the sign in a textbox1 and all > >> the rest in textbox2. > >> can you indicate the best way to do this please > > > > Take a look at the InStr, Left, Mid and Right string functions, e.g. > > > > Dim Text As String > > Dim pos As Integer > > > > ' Read Text in from wherever > > ' Text = .... > > > > pos = InStr(1, Text, "-") > > TextBox1.Text = Left$(Text, pos-1) > > TextBox2.Text = Mid$(Text, pos+1) > > >
Show quote
Hide quote
"Martin Walke" wrote: I like "split" and have used it with great success on various strings that > HI, > > You can also use the split function > > dim tstr as string, arry > 'get input string > tstr = ... > > arry = split (tstr,"-",2) > > then arry(0) contains the part before the first minus and arry(1) conatin > the rest of the string. > > HTH > Martin > > "S. I. Becker" <stewart@becker.nospam> wrote in message > news:440D8093.1000005@becker.nospam... > > Ron wrote: > >> Hi I want to search a string for the first showing of a minus sign eg > >> asc(45) place all the text to the left of the sign in a textbox1 and all > >> the rest in textbox2. > >> can you indicate the best way to do this please > > > > Take a look at the InStr, Left, Mid and Right string functions, e.g. > > > > Dim Text As String > > Dim pos As Integer > > > > ' Read Text in from wherever > > ' Text = .... > > > > pos = InStr(1, Text, "-") > > TextBox1.Text = Left$(Text, pos-1) > > TextBox2.Text = Mid$(Text, pos+1) > > > "Martin Walke" wrote: > HI, > > You can also use the split function > > dim tstr as string, arry > 'get input string > tstr = ... > > arry = split (tstr,"-",2) > > then arry(0) contains the part before the first minus and arry(1) conatin > the rest of the string. > > HTH > Martin > Hi, Ron, needed parsing into sections. Once you "split" the string, you can take the components and manipulate them or store them in many ways. As long as you know a "-" will always be there, and that's how you want to divide your string, it's clean, it eliminates the "-" automatically, and the code is easy to read. -- That's my two cents, Lesley On Tue, 7 Mar 2006 10:47:28 -0800, =?Utf-8?B?TGVzbGV5IFJlZ2Fu?=
<regan@miller[donotspam]canfield.com> wrote: Show quoteHide quote >"Martin Walke" wrote: I would check teh LBound and the UBound to see if the number after the> >> HI, >> >> You can also use the split function >> >> dim tstr as string, arry >> 'get input string >> tstr = ... >> >> arry = split (tstr,"-",2) >> >> then arry(0) contains the part before the first minus and arry(1) conatin >> the rest of the string. >> >> HTH >> Martin >> >> "S. I. Becker" <stewart@becker.nospam> wrote in message >> news:440D8093.1000005@becker.nospam... >> > Ron wrote: >> >> Hi I want to search a string for the first showing of a minus sign eg >> >> asc(45) place all the text to the left of the sign in a textbox1 and all >> >> the rest in textbox2. >> >> can you indicate the best way to do this please >> > >> > Take a look at the InStr, Left, Mid and Right string functions, e.g. >> > >> > Dim Text As String >> > Dim pos As Integer >> > >> > ' Read Text in from wherever >> > ' Text = .... >> > >> > pos = InStr(1, Text, "-") >> > TextBox1.Text = Left$(Text, pos-1) >> > TextBox2.Text = Mid$(Text, pos+1) >> >> >> >"Martin Walke" wrote: > >> HI, >> >> You can also use the split function >> >> dim tstr as string, arry >> 'get input string >> tstr = ... >> >> arry = split (tstr,"-",2) >> >> then arry(0) contains the part before the first minus and arry(1) conatin >> the rest of the string. >> >> HTH >> Martin >> >Hi, Ron, > >I like "split" and have used it with great success on various strings that >needed parsing into sections. Once you "split" the string, you can take the >components and manipulate them or store them in many ways. > >As long as you know a "-" will always be there, and that's how you want to >divide your string, it's clean, it eliminates the "-" automatically, and the >code is easy to read. -- That's my two cents, > >Lesley "-" is there. Brain-fart...Come to think of it if teh "-" is the first character, the error is compounded. In this case I would use Mid$ with Trim$ on both fragments. ********************************************************************** Richm***@sympatico.ca Dog thinks: they feed me, they take care of me: they are gods. Cat thinks: they feed me, they take care of me: I am god. http://www3.sympatico.ca/richmann/ http://www.geocities.com/richmannsoft/ ********************************************************************** Hi All
I have not tried all your suggested posts as yet, but I have created the following code and I am getting good results but you may be able to offer some better options so I post my code here:- Function myUpdate_Music() Dim pos, myStop As Integer Dim myHold, myHold1, myHold2, myHold3 As String On Error GoTo upDateError With mdiMusic With List_Input myHold = txtTrackName ' txtTrackName contains the string 'search string with InStr pos = InStr(1, myHold, "-") ' search for asc 45 myHold1 = Left$(myHold, pos - 1) myHold2 = Mid$(myHold, pos + 1) myStop = InStr(1, myHold2, "V") ' search for asc 86 myHold3 = Left$(myHold2, myStop - 2) 'exit search post data Music.strTrackNo = .txtTrackNo .txtArtists = myHold3 .txtTrackName = myHold1 Music.strArtists = myHold3 '.txtArtists Music.strTrackName = myHold1 '.txtTrackName If .txtArtists = "" Then .txtArtists = InputBox("Enter Artists Name", "Artists Name Not Shown", , 5000, 3000) Music.strArtists = .txtArtists End If End With .adoMusic.Recordset.MoveLast .adoMusic.Recordset.AddNew .dbgDataBase.Columns(0) = Music.strTrackNo .dbgDataBase.Columns(1) = Music.strTrackName .dbgDataBase.Columns(2) = Music.strArtists End With Call Music.Update_Tracks upDateError: Exit Function End Function '**** I do not see any errors when I run the code, but if I step through the code the step trip gives the correct answers but then the code loops to *** and drops out to my error trap why does it loop? It appears to me that you cannot use asc characters for this search is that correct?
MS, Enough is Enough!
Can someone please check my code? No Intellisense for items About The Hard Disk Serial Number Convert Variant String to Double Form.Printform Creating an ActiveX control Lock Word window? WHERE clause in ADO query of Excel data Launch VB App with Arguments via Hyperlink Programmatically set reference to ActiveX dll |
|||||||||||||||||||||||