-
HI There!!
I'm having a problem with my program. I'm a beginner, so it's been a little confusing for me, using Visual Basic 5.
My problem is this: In my program the user enters his or her name into Text1.Text like this: Jim Smith or Jim, Smith or whomever they are.
Next, I have to reverse the name so that the last name comes first like this: Smith, Jim or Smith Jim, using Command1 to reverse it. I am having so much problems with this still, can anyone help me? It's just this one little thing to finish!
It would be the greatest thing to get some coding that will work in Visual Basic 5!!
Thanks:)
-
<?>
Code:
'this will work if names are entered with spaces
'Ie. Jim Jones Larry Smith will not work without modifications if
'you enter Jim,Jones or Larry-Smith
'it is split based on a space between names and only using 2 names
'first and last
Public Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
Dim sParts() As String
Dim lParts As Long
Dim lPos As Long
lPos = InStr(sString, sSeparator)
While lPos
ReDim Preserve sParts(lParts)
sParts(lParts) = Left(sString, lPos - 1)
sString = Mid(sString, lPos + Len(sSeparator))
lPos = InStr(sString, sSeparator)
lParts = lParts + 1
Wend
If Len(sString) Then
ReDim Preserve sParts(lParts)
sParts(lParts) = sString
End If
Split2 = IIf(lParts, sParts, Array())
End Function
Private Sub Command1_Click()
Dim sSplitMe As Variant
Dim myString As String
myString = Text1.Text
sSplitMe = Split2(myString, " ")
Text1 = sSplitMe(UBound(sSplitMe)) & " " & sSplitMe(LBound(sSplitMe))
End Sub
-
I'd use "InStr" to find the space and then use "Left", "Mid" and "Right" to split up the string.
Use help, click F1 when the cursor is on the command you want help with.
-
I am not sure whether or not this code works with V5, it probably does.
Dim strFirstName As String, strSecondName As String
Dim IsThereaComma As Boolean, lngCommaPos As Long
Dim lngSpacePosition As Long
lngCommaPos = InStr(1, Text1, ",")
If lngCommaPos = 0 Then
IsThereaComma = False
lngSpacePosition = InStr(1, Text1, " ")
End If
If lngCommaPos <> 0 Then
strFirstName = Left$(Text1, lngCommaPos - 1)
strSecondName = Right$(Text1, Len(Text1) - lngCommaPos)
Text2 = Trim$(strSecondName & ", " & strFirstName)
Else
strFirstName = Left$(Text1, lngSpacePosition - 1)
strSecondName = Right$(Text1, Len(Text1) - lngSpacePosition)
Text2 = Trim$(strSecondName & " " & strFirstName)
End If
-
Thanks you guys!!
Hey, thanks for all the help from those that replied. I'll try it once I get to school. (I don't have VB5 at home). I am no longer stressed out about it, it was worrying me before. Thanks again:)
-
why not...
why don't you make two or even three boxes for first name, last name, and middle intitals, that away when they hit command1...
assuming you re-asemble their name into Text4 and Text1 is First Name, Text2 is Middle Name, and Text3 is Last name
Code:
Text4.text = Text3.Text & ", " & Text1.Text & " " & Text2.text
That would be your easiest solution