Force capitilization

anthonyevans

Registered User.
Local time
Today, 19:07
Joined
Mar 15, 2001
Messages
29
I would like a form to automatically capitilize the first letter of a chosen field, such as a persons first name when a user enters this data. Is there an input mask that will do this for me ?

Thankyou in advance for your kind help.

Anthony
 
Hi Anthony

Yes you can achieve this but how you do it can determine how much it meets you requirements. You could use an input mask and use > before the first letter (capital) and then < to force all the characters afterwards to be lower case.

L Letter (A to Z, entry required).
? Letter (A to Z, entry optional).

So you would need:

>L<???????????????????????????

This would allow you to have names of different lengths but what happens to "double names" - Lisa Marie, John Paul would appear as Lisa marie and John paul.

A more sophisticated manner is to use a module that converts the first letter via coding to a capital letter for each word, even those names that have two words (or a hyphenated name).

Below is such a module - not my work but one I was sent or found somewhere on a site like this one.

'---------------------------
Function Proper(strTxt As String) As String
' Comments :
' Parameters : strTxt -
' Returns : String -
' Created :
' Modified :
'
' --------------------------------------------------------
'---------------------------
' modul General
'
' Makes all characters lower case and then
' make every character before a space upper case
' EX: txtVar = Proper("" & Me!Name & "")
'
' Changed 12-07-98
'---------------------------

Dim strCHAR As String * 1
Dim strTxtTmp As String
Dim strWord As String
Dim intIX As Integer

On Error GoTo errProper

strCHAR = ""
strTxtTmp = ""

' Make all lowercase
strTxt = LCase(strTxt) & " "

' Search for spaces chr(32) or " - " chr(45) or " / " chr(47)
For intIX = 1 To Len(strTxt)
strCHAR = Mid(strTxt, intIX, 1)
strWord = strWord & strCHAR

' If a space or "-" is found make first character uppercase
If strCHAR = Chr(32) Or strCHAR = Chr(45) Or strCHAR = Chr(47) Then
strWord = UCase(Left(strWord, 1)) & Mid(strWord, 2, Len(strWord))

strTxtTmp = strTxtTmp & strWord
strWord = ""
End If
Next
Proper = Trim(strTxtTmp)

exitProper:
Exit Function

errProper:
MsgBox Error$ & Chr(13) & "Nr: " & Err, 48, "Proper"
Resume exitProper

End Function
----------------------------------

This module is called from the AfterUpdate event of the text box on your form when you enter data onto your database. The coding below is then the coding behind the form (CBF) to call on the module.


---------------------------------------
Private Sub txtLastName_AfterUpdate()
' Comments :
' Parameters : -
' Returns : -
' Created :
' Modified :
'
' ---------------------------------------------------

On Error GoTo Err_txtLastName_AfterUpdate
If IsNull(txtLastName) Then
Exit Sub

Else
Me![txtLastName] = Proper("" & Me![txtLastName] & "")

End If

Exit Sub

Err_txtLastName_AfterUpdate:
MsgBox "The following error occurred: " & Error$
Resume Next
End Sub
------------------------------------

I hope that helps.

Rich Gorvin

[This message has been edited by Rich@ITTC (edited 03-15-2001).]
 
Dear Rich, that is most kind of you.

The first piece of help is just what i need.

All the best
 
Thanks for that Mike.

Appologies for my ignorance here but i am quite new to this game.

I copied the code into the underlying code behind the form where i wish this formatting to take place but it does not appear to work. On entering a persons name it stays as lowercase. Am i pasting this in the right place. I have a feeling it should go somewhere else.

Thanks for your time.
 
Hi Mike

I take your point about vbProper .. but when I have tried it it doesn't seem to work for hyphenated names, or do you know differently!?!

Rich Gorvin
 
No, you're right Rich, in fact this demostrates my ignorance, I didn't look properly at the code you posted.

It would probably be quite easy to modify so that it handles 'Angus McTavish','Ian MacGregor','Feargal O'Flaherty' (and the like) properly too.

Mike
 
Hi Mike

Thanks for your reply. I have played around with amending the code and have got it to take account of O'Brien, McDonald/MacDonald etc. However there is a problem when it comes to names like Machin which it converts to MaChin!! Any ideas on how one could get round that? I have only thought about, but not done, a possible "exceptions to the rule" table that is called upon (at the moment I have the exceptions hard-coded in, see below):

Function ProperLast(strTxt As String) As String
' Comments :
' Parameters : strTxt -
' Returns : String -
' Created :
' Modified :
'
' --------------------------------------------------------
'---------------------------
' modul General
'
' Makes all characters lower case and then
' make every character before a space upper case
' Special check for McArthur and O'Neil
' EX: txtVar = ProperLast("" & Me!Name & "")
'
' Changed 12-07-98
'---------------------------

Dim strCHAR As String * 1
Dim strTxtTmp As String
Dim strWord As String
Dim intIX As Integer

On Error GoTo errProperLast

strCHAR = ""
strTxtTmp = ""

' Make all lowercase
strTxt = LCase(strTxt) & " "

' Search for spaces chr(32) or " - " chr(45) or " / " chr(47)
For intIX = 1 To Len(strTxt)
strCHAR = Mid(strTxt, intIX, 1)
strWord = strWord & strCHAR

' If a space or "-" is found make first character uppercase
If strCHAR = Chr(32) Or strCHAR = Chr(45) Or strCHAR = Chr(47) Then
strWord = UCase(Left(strWord, 1)) & Mid(strWord, 2, Len(strWord))

' Check for McDonald, or O'Brien
If Left(strWord, 2) = "Mc" Or Left(strWord, 2) = "O'" Then
strWord = Left(strWord, 2) & UCase(Mid(strWord, 3, 1)) & Mid(strWord, 4, Len(strWord))

'Check for MacDonald
ElseIf Left(strWord, 3) = "Mac" Then
strWord = Left(strWord, 3) & UCase(Mid(strWord, 4, 1)) & Mid(strWord, 5, Len(strWord))

'Check for Mace, Macey, Machin etc.
If strWord = "Mace " Or strWord = "Macey " Or strWord = "Machen " Or strWord = "Machin " Or strWord = "Machell " Or strWord = "Mack " Then
strWord = Left(strWord, 3) & LCase(Mid(strWord, 4, 1)) & Mid(strWord, 5, Len(strWord))



End If
End If
strTxtTmp = strTxtTmp & strWord
strWord = ""
End If

Next
ProperLast = Trim(strTxtTmp)

exitProperLast:
Exit Function

errProperLast:
MsgBox Error$ & Chr(13) & "Nr: " & Err, 48, "ProperLast"
Resume exitProperLast

End Function
------------------------------------------

Have you Mike, or anyone else , got any further modifications that you could suggest to take account of these "unusual" names (my name is unusual enough for me to feel OK about calling other names unusual!!). I would also like to take account of non-English based names - van Houten, de Faria, etc.

Rich Gorvin

[This message has been edited by Rich@ITTC (edited 03-15-2001).]
 
Apologies for coming in half way through!

The code I use for forcing capitalization is as follows:

txtMyControl.Value = StrConv(txtMyControl, vbProperCase)

I place this in the control's On Lost Focus event and it works fine. As for coping with like MacDonald, McGregor etc it obviously won't work. However with the foreign names like van Houten you could create a function that would firstly trim any leading spaces, test to find the space that seperates the two names and then convert them accordingly.
 
I think if you were going to try to get it to catch all of the possible oddities, you'd need a table of rules, you might even need to list the full 'MacDonald' etc like so:

fldName, fldConvert
macd,MacD (i.e. always capitalise this letter sequence)
mct,McT
van,van (i.e. leave uncapitalised)

you could get your code to test for the common name prefixes and if it is not defined in the list of rules, pop up a form with an option group to prompt the user:

"You have entered the name 'mccarthy', do you want to:
convert to 'McCarthy'
Convert to 'Mccarthy'
Leave alone

You could also have a check box 'don't ask me this again' - if checked, it would add a row to the rules table corresponding with the user's selection.

or some such


Mike


[This message has been edited by Mike Gurman (edited 03-15-2001).]
 
I am also trying to do a simple Capitilazation of the first letter and lower case on other letters in the word. i am not doing names, do dont care about exceptions. I copied this in put mask from above :

>L<???????????????????????????

but not sure where to apply it. if i do it in the tabel field, the code "&GT" shows up. i have a form that selects from a combo list (and can add when its not on the combo) but when i do it on that field it doesnt work either. Any suggestions?
 

Users who are viewing this thread

Back
Top Bottom