Search code examples
vb6

How capitalize fullname in vb6


hi all i have this question as bellow how capitalize full in one vb6 Vb6 string variable ‘example ‘my fullname Dim fullname as string Fullname = “abdirahman abdirisaq ali” Msgbox capitalize(fullname) it prints abdirahmanAbdirisaq ali that means it skips the middle name space even if I add more spaces its same . this is my own code and efforts it takes me at least 2 hours and still . I tired it tired tired please save me thanks more. Please check my code and help me what is type of mistakes I wrote . This is my code

                Private Function capitalize(txt As String) As String
                        txt = LTrim(txt)
                        temp_str = ""
                        Start_From = 1
                        spacing = 0
                            For i = 1 To Len(txt)

                            If i = 1 Then
                            temp_str = UCase(Left(txt, i))
                            Else
                         Start_From = Start_From + 1
                            If Mid(txt, i, 1) = " " Then
                                 Start_From = i
                                 spacing = spacing + 1
                            temp_str = temp_str & UCase(Mid(txt, Start_From + 1, 1))
                             Start_From = Start_From + 1
                            Else
                             temp_str = temp_str & LCase(Mid(txt, Start_From, 1))
                            End If
                            End If
                            Next i
                            checkName = temp_str
                End Function

Solution

  • It's far simpler than that. In VB6 you should use Option Explicit to properly type your variables. That also requires you to declare them.

    Option Explicit
    Private Function capitalize(txt As String) As String
        Dim temp_str as String
        Dim Names As Variant
        Dim Index As Long 
    
        'Remove leading and trailing spaces
        temp_str = Trim$(txt)
    
        'Remove any duplicate spaces just to be sure.
        Do While Instr(temp_str, "  ") > 0
            temp_str = Replace(temp_str, "  ", " ")
        Loop
    
        'Create an array of the individual names, separating them by the space delimiter
        Names = Split(temp_str, " ")
    
        'Now put them, back together with capitalisation
        temp_str = vbnullstring
        For Index = 0 to Ubound(Names) 
            temp_str = temp_str + Ucase$(Left$(Names(Index),1)) + Mid$(Names(Index),2) + " "
        Next
        'Remove trailing space
        capitalize = Left$(temp_str, Len(temp_str) - 1)
    End Function
    

    That's the fairly easy part. If you are only going to handle people's names it still needs more work to handle names like MacFarland, O'Connor, etc.

    Business names get more complicated with since they can have a name like "Village on the Lake Apartments" where some words are not capitalized. It's a legal business name so the capitalization is important.

    Professional and business suffixes can also be problematic if everything is in lower case - like phd should be PhD, llc should be LLC, and iii, as in John Smith III, would come out Iii.

    There is also a VB6 function that will capitalize the first letter of each word. It is StrConv(string,vbProperCase) but it also sets everything that is not the first letter to lower case. So PhD becomes Phd and III becomes Iii. Where as the above code does not change the trailing portion to lower case so if it is entered correctly it remains correct.