spyrit Posted November 28, 2003 Posted November 28, 2003 Hi Please help I want to be able to enter a line of text into a text box in the format Surname Firstname SUBJECT1......SUBJECT5 DOB I need to enter it in this format with 5 subjects containing 3 characters each. Now each line will always have a surname and firstname but the number of subjects can vary. The DOB may or may not be at the end of the line. It then needs to be rearranged in the format Firstname Surname DOB SUBJECT1.....SUBJECT5 I have done this so far but it wont work!!! Please help words = Split(TextBox1.Text, " ") If Microsoft.VisualBasic.Right(TextBox1.Text, 2) = number And Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 And Len(words(6)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(7) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) + " " + words(6) ElseIf Microsoft.VisualBasic.Right(TextBox1.Text, 2) = number And Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(6) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) ElseIf Microsoft.VisualBasic.Right(TextBox1.Text, 2) = number And Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(5) + " " + words(2) + " " + words(3) + " " + words(4) ElseIf Microsoft.VisualBasic.Right(TextBox1.Text, 2) = number And Len(words(2)) = 3 And Len(words(3)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(4) + " " + words(2) + " " + words(3) ElseIf Microsoft.VisualBasic.Right(TextBox1.Text, 2) = number And Len(words(2)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(3) + " " + words(2) ElseIf Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 And Len(words(6)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(6) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) ElseIf Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(5) + " " + words(2) + " " + words(3) + " " + words(4) ElseIf Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(4) + " " + words(2) + " " + words(3) ElseIf Len(words(2)) = 3 And Len(words(3)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(3) + " " + words(2) ElseIf Len(words(2)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(2) End If Please help me. I need to know whats wrong. Please Please Please. Quote
Administrators PlausiblyDamp Posted November 28, 2003 Administrators Posted November 28, 2003 Does it have to be in a single textbox? Could you not impose some order i.e. Surname, Firstname, DOB, subject ...... ? If not if may be easier to split the text into an array then work on the individual items. dim vals() as string vals = TextBox2.text.split(" ") 'vals will now contain an array of strings with val(0) being Surname and vals(1) being Firstname. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
spyrit Posted November 28, 2003 Author Posted November 28, 2003 No I couldn't do that because the number of subjects changes. So it wou;dn't work would it because it could be 8 'words' long or it could be as low as 3 'words' long. Quote
Administrators PlausiblyDamp Posted November 28, 2003 Administrators Posted November 28, 2003 would the array idea not work though? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
spyrit Posted November 28, 2003 Author Posted November 28, 2003 Please help as I am new to VB. But I can't see how it would. Please explain. I wrote a split by space but i don't think it will work for any amount of words. If I do, words = split(TextBox1.Text, " ") and then array. Because it needs rearranging in an order The DOB is optional and the Subjects vary between 1 and 5 not 1 and 5 char actual 1 - 5 subjects. Quote
JumpsInLava Posted November 28, 2003 Posted November 28, 2003 I'm kind of bored today.... ' Dim words() As String = Split(TextBox1.Text, " ") Dim x As Integer = 0 Dim strDOB As String = String.Empty Dim strFName As String = String.Empty Dim strSName As String = String.Empty If words.Length > 2 Then TextBox2.Text = "" strSName = words(0) strFName = words(1) If words.Length > 3 Then If words(words.Length - 1).Length > 3 Then strDOB = words(words.Length - 1) End If For x = 2 To words.Length - 1 If words(x).Length = 3 Then TextBox2.Text &= " " & words(x) End If Next TextBox2.Text = TextBox2.Text.Trim If strDOB <> String.Empty Then TextBox2.Text = strDOB & " " & TextBox2.Text TextBox2.Text = strFName & " " & strSName & " " & TextBox2.Text Else MessageBox.Show("Bad Data") End If Quote
spyrit Posted November 28, 2003 Author Posted November 28, 2003 Hi That is GREAT!!!!!. Thanks alot. Could you just do one more thing for me please. Could you explain the code. i.e. annotate it so I know what it is all about please. Thanks Quote
JumpsInLava Posted November 28, 2003 Posted November 28, 2003 I changed how some of it works, to make it friendlier. Note, this code makes assumptions (which we all know what that can mean): 1. Firstname and Surname WON'T have spaces in them. If they do, then you would have to split on something else, or find a different way to do it. 2. All subjects are exactly three characters. 3. The DOB (if it exists at the end) is NOT three characters. ' 'Get the words in a string array Dim words() As String = Split(TextBox1.Text, " ") '"For" loop counter Dim x As Integer = 0 'To hold Firstname and Surname Dim strFName As String = String.Empty Dim strSName As String = String.Empty 'If we have more than one item in "words()" If words.Length > 1 Then 'Clear "TextBox2" TextBox2.Text = String.Empty 'Hold the Surname and the Firstname. strSName = words(0) strFName = words(1) 'If we have more than two items in "words()" then... If words.Length > 2 Then 'For each item in "words()" starting with the 3rd word... 'Note: arrays are 0 based, so position 2 is actually the 3rd 'word, and "words.Length - 1" is the last item.) For x = 2 To words.Length - 1 'If the length of the word is exactly three then it is a subject... If words(x).Length = 3 Then 'Add "[space] & [word]" to TextBox2 TextBox2.Text &= " " & words(x) End If Next 'Because the "For" loop above will always add "[space] & [word]" we have 'an extra space at the front. Use .Trim to remove spaces from the 'front and back of TextBox2. TextBox2.Text = TextBox2.Text.Trim 'If the last word's length is NOT 3 assume it is the DOB... If words(words.Length - 1).Length <> 3 Then 'Add "[last word] & [space]" to the front of TextBox2 TextBox2.Text = words(words.Length - 1) & " " & TextBox2.Text End If End If 'Add the Firstname and Surname to the front of "TextBox2" TextBox2.Text = strFName & " " & strSName & " " & TextBox2.Text Else 'We didn't have more than one item in "words()" so the data must be bad. MessageBox.Show("Bad Data") End If Quote
spyrit Posted November 28, 2003 Author Posted November 28, 2003 Thats perfect but... How could I then do it so it takes the length of firstname takes that away from 15 and adds the remainder as that number of spaces. The same for surname. Also It needs to add 2 spaces to the end of DOB. How could I do this. Thanks Quote
JumpsInLava Posted November 29, 2003 Posted November 29, 2003 Change: strFName = words(1) to strFName = words(1).PadRight(15, " ") Change: TextBox2.Text = words(words.Length - 1) & " " & TextBox2.Text to TextBox2.Text = words(words.Length - 1) & " " & TextBox2.Text Note: It only gets the DOB if it finds a word that is not three characters at the end of the word list. If you need the DOB position in TextBox2 to always be the same length regardless of it's existence in the word list, then you would hold it in a variable, and pad it when you added it to TextBox2. Quote
spyrit Posted November 29, 2003 Author Posted November 29, 2003 Thanks Also do you know what could be wrong in this just for interest. words = Split(TextBox1.Text, " ") If Microsoft.VisualBasic.Right(TextBox1.Text, 8) Then ElseIf Microsoft.VisualBasic.Right(TextBox1.Text, 8) And Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 And Len(words(6)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(7) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) + " " + words(6) ElseIf Microsoft.VisualBasic.Right(TextBox1.Text, 8) And Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(6) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) ElseIf Microsoft.VisualBasic.Right(TextBox1.Text, 8) And Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(5) + " " + words(2) + " " + words(3) + " " + words(4) ElseIf Microsoft.VisualBasic.Right(TextBox1.Text, 8) And Len(words(2)) = 3 And Len(words(3)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(4) + " " + words(2) + " " + words(3) ElseIf Microsoft.VisualBasic.Right(TextBox1.Text, 8) And Len(words(2)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(3) + " " + words(2) ElseIf Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 And Len(words(6)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(6) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) ElseIf Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(5) + " " + words(2) + " " + words(3) + " " + words(4) ElseIf Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(4) + " " + words(2) + " " + words(3) ElseIf Len(words(2)) = 3 And Len(words(3)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(3) + " " + words(2) ElseIf Len(words(2)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(2) End If Quote
spyrit Posted November 29, 2003 Author Posted November 29, 2003 Also in that code you gave me. How can I add 9 spaces to the end of the surname in output if there is no date of birth Please Reply Thanks for all your help Regards Quote
JumpsInLava Posted November 29, 2003 Posted November 29, 2003 I see things that are wrong with it, but I can't see an easy way to get it to work: 1. You can't access items in an array that don't exist. This would return an error if there aren't seven or more items in the array: Len(words(6)) = 3 (That is the main problem with it programmatically, because to keep it in the same multiple "If" structure you would have to evaluate if each item exists before testing for the length.) 2. "If" "Then" statments require something that can be evaluated as True or False. I don't know what this should do: If Microsoft.VisualBasic.Right(TextBox1.Text, 8) Then "Microsoft.VisualBasic.Right(TextBox1.Text, 8)" doesn't return True or False, it returns the first eight characters of TextBox1.Text. 3. You aren't declaring "words" ("Dim words() As String"?) 4. The code doesn�t account for the last word to be a non three character DOB. Quote
JumpsInLava Posted November 29, 2003 Posted November 29, 2003 Also in that code you gave me. How can I add 9 spaces to the end of the surname in output if there is no date of birth Please Reply Thanks for all your help Regards So.....I take it you are working with field size. Can you give me an example of what you want the following inputs to look like after processing (with space padding): "sName sName abc def ghi jkl mno" "sName fName abc def ghi jkl mno 11282003" (What if the name is longer than the field?) "sNameNameNameName fNameNameNameName abc def ghi jkl mno" "sNameNameNameName fNameNameNameName abc def ghi jkl mno 11282003" Quote
spyrit Posted November 29, 2003 Author Posted November 29, 2003 Basically It needs to be.... firstname 1-15 surname 1-15 DOB 1-10 SUBJECT1 1-4 " " 1-4 " " 1-4 " " 1-4 " " 1-4 I have edited you code up to the point that it needs ten more spaces after surname to account for the DOB not being there and outputting it under correct headings in the output file. I hope you can help. Regards spyrit Quote
spyrit Posted November 29, 2003 Author Posted November 29, 2003 Also do you think you could really help with making my other code work because I feel I've failed if I can't. I understand what you mean when you say it doesn't return true or false but how can I check to see if the last char is a number to do the if then statement. I need a line that checks the end char and returns true or flase to do the rest of the code. I understand about dim the words() as string Also I don't really understand the other thing you said about arrays because If the correct IF STATEMENT is in place will it not just go to the second line. Could I not do it like If the last char is numeric Then Count number of words If number of words is 3 Then ... If 4 Then ... If 5 Then .... If 6 Then ... If 7 Then ... Else (IF IT ISN't NUMERIC THEN) If 3 Then ... If 4 Then ... If 5 Then .... If 6 Then ... Please reply Quote
JumpsInLava Posted December 1, 2003 Posted December 1, 2003 ' Dim words() As String words = Split(TextBox1.Text, " ") 'If the last eight chars is numeric... If IsNumeric(Microsoft.VisualBasic.Right(TextBox1.Text, 8)) = True Then 'If the length of "words()" is eight... If words.Length = 8 Then 'If each of the words except the last are three chars long... If Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 And Len(words(6)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(7) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) + " " + words(6) End If ElseIf words.Length = 7 Then If Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(6) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) End If ElseIf words.Length = 6 Then If Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(5) + " " + words(2) + " " + words(3) + " " + words(4) End If ElseIf words.Length = 5 Then If Len(words(2)) = 3 And Len(words(3)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(4) + " " + words(3) + " " + words(2) End If ElseIf words.Length = 4 Then If Len(words(2)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(3) + " " + words(2) End If ElseIf words.Length = 3 Then If Len(words(2)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(2) End If End If Else 'No date on the end... 'If the length of "words()" is seven (one less because no date)... If words.Length = 7 Then 'If each of the words are three chars long... If Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 And Len(words(5)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) + " " + words(6) End If ElseIf words.Length = 6 Then If Len(words(2)) = 3 And Len(words(3)) = 3 And Len(words(4)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(2) + " " + words(3) + " " + words(4) + " " + words(5) End If ElseIf words.Length = 5 Then If Len(words(2)) = 3 And Len(words(3)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(2) + " " + words(3) + " " + words(4) End If ElseIf words.Length = 4 Then If Len(words(2)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(2) + " " + words(3) End If ElseIf words.Length = 3 Then If Len(words(2)) = 3 Then TextBox2.Text = words(1) + " " + words(0) + " " + words(2) End If End If End If ' Quote
spyrit Posted December 1, 2003 Author Posted December 1, 2003 Thanks for the reply, For some reason the above code won't work that you posted, Do you know why? Did it work on yours? Please get back to me. Regards spyrit Quote
bri189a Posted December 2, 2003 Posted December 2, 2003 how about using a Select statement... god I hated long if...then's! and some modular programing... I replaced your monstorous if...then with a select and two function which are a heck of alot easier to read: Select Case words.Length Case 8: If LenSame(words, new Long(){2, 3, 4, 5, 6}, 3) Then TextBox2.Text = BuildWords(words, new Long(){1, 0, 7, 2, 3, 4, 5, 6}); End If Case 7: If LenSame(words, new Long(){2, 3, 4, 5}, 3) Then TextBox2.Text = BuildWords(words, new Long(){1, 0, 6, 2, 3, 4, 5}); End If Case 6: If LenSame(words, new Long(){2, 3, 4}, 3) Then TextBox2.Text = BuildWords(words, new Long(){1, 0, 5, 2, 3, 4}); End If Case 5: If LenSame(words, new Long(){2, 3}, 3) Then TextBox2.Text = BuildWords(words, new Long(){1, 0, 4, 3, 2}); End If Case 4: If LenSame(words, new Long(){2}, 3) Then TextBox2.Text = BuildWords(words, new Long(){1, 0, 3, 2}); End If Case 3: If LenSame(words, new Long(){2}, 3) Then TextBox2.Text = BuildWords(words, new Long(){1, 0, 2}); End If End Select Private Function BuildWords(ByVal WordArray() As String, ByVal args() As Long) As String Dim ReturnString As String Dim i As Long For Each i In args ReturnString += WordArray(i - 1) Next Return ReturnString End Function Private Function LenSame(ByVal WordArray() As String, ByVal args() As Long, ByVal TargetLen As Long) as Boolean Dim i As Long Dim noMatch As Boolean For Each i in args If WordArray(i - 1).Length != TargetLen noMatch = True break End If Next i Return !noMatch End Function Maybe not your preferance but in my experiance it's easier like this. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.