codebreaker
asked on
About String
I have one TextBox where i will put a person's name(e.g. Michael A. Surname). Then i have 3 Command Buttons and 3 TextBox. In the first TextBox, when I click the first CommandButton the FirstName(e.g. Michael) should be in the first TextBox. And in the second, the MiddleInitial, and in the third the Surname.
Should I use (Len(), InStr(),etc..) in doing that.
Show me the code if this posible.
Should I use (Len(), InStr(),etc..) in doing that.
Show me the code if this posible.
If this is needed:
Private Sub Command1_Click() 'First Name
Text1.Text = myArray(0)
End Sub
Private Sub Command1_Click() 'Middle Name
Text2.Text = myArray(1)
End Sub
Private Sub Command1_Click() 'Last Name
Text3.Text = myArray(2)
End Sub
Private Sub Command1_Click() 'First Name
Text1.Text = myArray(0)
End Sub
Private Sub Command1_Click() 'Middle Name
Text2.Text = myArray(1)
End Sub
Private Sub Command1_Click() 'Last Name
Text3.Text = myArray(2)
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for MiddleInitial you should better use this code line after setting myarray by Split function
myarray(1)=Left(myarray(1) ,1)
If you have no VB6 then you cannot use that great functions of VB like Split and Replace then you can use this code or some else.
Code needs 3 command1 buttons (control arrays) and 3 text1 textboxes (control arrays), Text2 textbox (entry textbox for full name) and then paste this code in form module.
Private Function GetVal(Index As Integer, fullname As String) As String
Dim i As Integer, j As Integer
fullname = Trim(fullname) & " "
For i = 0 To Index
fullname = LTrim(Right(fullname, Len(fullname) - j))
j = InStr(1, fullname, " ")
Next i
If j = 0 Then Exit Function
GetVal = IIf(Index = 1, Left(Left(fullname, j - 1), 1), Left(fullname, j - 1))
End Function
Private Sub Command1_Click(Index As Integer)
Text1(Index).Text = GetVal(Index, Text2.Text)
End Sub
regards
suat
myarray(1)=Left(myarray(1)
If you have no VB6 then you cannot use that great functions of VB like Split and Replace then you can use this code or some else.
Code needs 3 command1 buttons (control arrays) and 3 text1 textboxes (control arrays), Text2 textbox (entry textbox for full name) and then paste this code in form module.
Private Function GetVal(Index As Integer, fullname As String) As String
Dim i As Integer, j As Integer
fullname = Trim(fullname) & " "
For i = 0 To Index
fullname = LTrim(Right(fullname, Len(fullname) - j))
j = InStr(1, fullname, " ")
Next i
If j = 0 Then Exit Function
GetVal = IIf(Index = 1, Left(Left(fullname, j - 1), 1), Left(fullname, j - 1))
End Function
Private Sub Command1_Click(Index As Integer)
Text1(Index).Text = GetVal(Index, Text2.Text)
End Sub
regards
suat
problem you are going to face will be people who put in Jnr or have 2 first names, which as far as I can see will not be handled by above...
What I would do is read to the first space in the string to get the firstname...
iFirstEnd = instr(1 strFullName, " ")
strFirstName = mid(strFullName, 1, iFirstEnd - 1)
Then read from the end of the string for the surname...
iSurStart = instrrev(strFullName, " ")
strSurname = mid(strFullName, iSurStart+1)
strMiddle = mid(strfullname, iFirstEnd+1, iSurStart-iFirstEnd)
you will probably have to fool around with the +1 and -1 above as it will probably be pulling back a space in some cases and dropping a char in others and I haven't compiled it. But you should get the idea anyway...
eosu
What I would do is read to the first space in the string to get the firstname...
iFirstEnd = instr(1 strFullName, " ")
strFirstName = mid(strFullName, 1, iFirstEnd - 1)
Then read from the end of the string for the surname...
iSurStart = instrrev(strFullName, " ")
strSurname = mid(strFullName, iSurStart+1)
strMiddle = mid(strfullname, iFirstEnd+1, iSurStart-iFirstEnd)
you will probably have to fool around with the +1 and -1 above as it will probably be pulling back a space in some cases and dropping a char in others and I haven't compiled it. But you should get the idea anyway...
eosu
hi Codebreaker,
the following code will work for names of type u have specified in the question.(Michael A. Surname). This does not Advanced String functions like Instr or Split.
I have just used Mid and Len function, which i hope, do work for previous versions of visual basic too. I have called the Procedure in the Form load event, but it should have been called in lost_Focus of the userInput text box.
All the best
KCM
************************
Dim strName As String
Dim strArray(2) As String
Private Sub Command1_Click()
Text1.Text = strArray(0)
End Sub
Private Sub Command2_Click()
Text2.Text = strArray(1)
End Sub
Private Sub Command3_Click()
Text3.Text = strArray(2)
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = "Michael A. Surname"
SplitRecord
End Sub
Sub SplitRecord()
Dim strTemp As String, intCtr As Integer
intCtr = 0
strTemp = ""
For i = 1 To Len(Text4)
If Mid(Text4.Text, i, 1) = " " Then
strArray(intCtr) = strTemp
strTemp = ""
intCtr = intCtr + 1
If intCtr > 1 Then
strArray(intCtr) = Mid(Text4.Text, i + 1)
Exit For
End If
Else
strTemp = strTemp & Mid(Text4.Text, i, 1)
End If
Next i
End Sub
the following code will work for names of type u have specified in the question.(Michael A. Surname). This does not Advanced String functions like Instr or Split.
I have just used Mid and Len function, which i hope, do work for previous versions of visual basic too. I have called the Procedure in the Form load event, but it should have been called in lost_Focus of the userInput text box.
All the best
KCM
************************
Dim strName As String
Dim strArray(2) As String
Private Sub Command1_Click()
Text1.Text = strArray(0)
End Sub
Private Sub Command2_Click()
Text2.Text = strArray(1)
End Sub
Private Sub Command3_Click()
Text3.Text = strArray(2)
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = "Michael A. Surname"
SplitRecord
End Sub
Sub SplitRecord()
Dim strTemp As String, intCtr As Integer
intCtr = 0
strTemp = ""
For i = 1 To Len(Text4)
If Mid(Text4.Text, i, 1) = " " Then
strArray(intCtr) = strTemp
strTemp = ""
intCtr = intCtr + 1
If intCtr > 1 Then
strArray(intCtr) = Mid(Text4.Text, i + 1)
Exit For
End If
Else
strTemp = strTemp & Mid(Text4.Text, i, 1)
End If
Next i
End Sub
MyName = "Michael A. Surname"
myArray = Split(myName," ")
For i = Lbound(myArray) To Ubound(myArray)
Debug.Print myArray(i)
Next i