Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textbox question

Status
Not open for further replies.

cannonenter

Programmer
Mar 27, 2003
20
GB
I have a Text box that has a first name and Surname in it.

How do i get the app to find the First name place it into a string and then find the Surname and again place into a string

Many thanks

Stephen
 
You would probably be better off with two text boxes, one for each value. To use one box, you would probably need to use the InStr and Mid functions to find the space that seperates the two names.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Assuming the first and last name separated with single space:

Private Sub Command1_Click()
DisplayTheName Text1.Text, " "
End Sub

Private Sub DisplayTheName(ByVal pstrSomeTextToParse As String, ByVal pstrDelimiter As String)
Dim strName() As String
Dim i As Integer

strName = Split(pstrSomeTextToParse, pstrDelimiter)

For i = LBound(strName) To UBound(strName)
MsgBox strName(i)
Next i

End Sub
 
You could try this:

Private Sub Command1_Click()
Dim text_in_textbox As String
Dim length_of_string As Integer
Dim first_name As String
Dim second_name As String
Dim i As Integer
Dim temp_char As String
text_in_textbox = Text1.text
length_of_string = Len(text_in_textbox)
MsgBox length_of_string
For i = 1 To length_of_string
temp_char = Mid$(text_in_textbox, i, 1)
If temp_char = " " Then
second_name = Mid$(text_in_textbox, i + 1, Len(text_in_textbox))
Label2.Caption = second_name
first_name = Mid$(text_in_textbox, 1, i)
Else
End If
Next
Label1.Caption = first_name
End Sub

but the option above would be better :)
 
To do it properly you will have to take into consideration different ways the user may input the name along with multi word names, such as:

Mr John De La Cruz
John De La Cruz Phd
De La Cruz, John
Mrs John De La Cruz-Smith Phd
etc.

or just use multiple textboxes as Tom mentioned.



List of Tsunami Relief Donations Sites

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Well, this approach takes care of spaces and commas. It will display multi word names like Cruz-Smith as one entity:

Option Explicit

Private Sub Command1_Click()
DisplayTheName Text1.Text, " ", ","
End Sub

Private Sub DisplayTheName(ByVal pstrSomeTextToParse As String, _
ByVal pstrDelimiter1 As String, _
ByVal pstrDelimiter2 As String)
Dim strName() As String
Dim strName2() As String
Dim i As Integer
Dim j As Integer
Dim blnDelimter1 As Boolean
Dim blnDelimter2 As Boolean

blnDelimter1 = InStr(1, pstrSomeTextToParse, pstrDelimiter1) > 0
blnDelimter2 = InStr(1, pstrSomeTextToParse, pstrDelimiter2) > 0

If blnDelimter1 Then
strName = Split(pstrSomeTextToParse, pstrDelimiter1)
End If

If blnDelimter1 Then
For i = LBound(strName) To UBound(strName)
If blnDelimter2 Then
strName2 = Split(strName(i), pstrDelimiter2)
For j = LBound(strName2) To UBound(strName2)
If Trim$(strName2(j)) > vbNullString Then MsgBox strName2(j)
Next j
Else
If Trim$(strName(i)) > vbNullString Then MsgBox strName(i)
End If
Next i
Else
If blnDelimter2 Then
strName2 = Split(pstrSomeTextToParse, pstrDelimiter2)
For j = LBound(strName2) To UBound(strName2)
If Trim$(strName2(j)) > vbNullString Then MsgBox strName2(j)
Next j
Else
If Trim$(pstrSomeTextToParse) > vbNullString Then MsgBox pstrSomeTextToParse
End If
End If
End Sub

 
Topcat1a.

thanks. I could not get my head around, its been one of those days.

many thanks again it works cool and just what i was trying to do.

Stephen
 
I think the commas solution is the best, you can use something like:

sFullName = "John,Doe"
sFName = LEFT(sFullName, INSTR(1,sFullName,chr(46))- 1)
sSName = MID(sFullName,INSTR(1,sFullName,chr(46))+ 1)


Mal'chik [bigglasses]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top