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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uppercase first character 5

Status
Not open for further replies.

LeizladNeb

Technical User
Jan 27, 2003
7
0
0
GB
Hi. I'd like to know how to automatically make the first character of each data entry uppercase 'On Exit'. I think it'll use something like below, i'm just having trouble reffering to the first charater only.

Private Sub FirstName_Exit(Cancel As Integer)
FirstName = UCase(FirstName)
End Sub

Ben Dalziel
 
hello m8,

I use a function as below...

Code:
Function Proper(pstrFld As Control) As Variant
Dim intArraySize As Integer
Dim intArrayPos As Integer
Dim strReturnVal As String

If IsNull(pstrFld) Then
Proper = Null
Exit Function
End If

intArraySize = Len(Trim(pstrFld)) ' set size of array

pstrFld = LCase(pstrFld) ' set all chrs to lowercase

ReDim strArray(intArraySize) ' size array to hold field

For intArrayPos = 1 To intArraySize ' fill the array with the field characters
strArray(intArrayPos) = Mid$(pstrFld, intArrayPos, 1)
Next intArrayPos

strReturnVal = UCase(strArray(1)) ' upper case the first character

For intArrayPos = 3 To intArraySize ' go through the remaining letters
    
    ' add the next current character to the answer
    strReturnVal = strReturnVal + strArray(intArrayPos)
    
Next intArrayPos
' return the answer

pstrFld = strReturnVal
End Function
____________________________________
Eat [pc3], Sleep [pc3], Live [pc3]
 
on exit event: =proper([field name])

hope this helps ____________________________________
Eat [pc3], Sleep [pc3], Live [pc3]
 
sorry dude... this line needs changing:

FROM
Code:
For intArrayPos = 3 To intArraySize ' go through the remaining letters
TO
Code:
For intArrayPos = 2 To intArraySize ' go through the remaining letters

(I use this to upeer case the first 2 chars from the nt user name) ____________________________________
Eat [pc3], Sleep [pc3], Live [pc3]
 
I've used this code in the past...it probably does more than whats needed but its only an alternative option to what Lavey has posted as his method works great. The only difference with my code is that it will Upcase the first character of each word, i.e: "joe bloggs" converts to "Joe Bloggs"

**********code begins

Function CapitalizeFirst(Str)
Dim Counter, Strlen As Integer

Counter = 1
Strlen = Len(Trim(Str))
CapitalizeFirst = UCase(Mid(Str, 1, 1))
Do While Counter < Strlen
Counter = Counter + 1
If Mid(Str, Counter, 1) = &quot; &quot; Then
CapitalizeFirst = CapitalizeFirst & &quot; &quot;
Counter = Counter + 1
CapitalizeFirst = CapitalizeFirst & UCase(Mid(Str, Counter, 1))
Else
CapitalizeFirst = CapitalizeFirst & Mid(Str, Counter, 1)
End If
Loop
End Function

Private Sub txtAttention_AfterUpdate()
If Not (IsNull(Me.txtAttention)) Then
Me.txtAttention = CapitalizeFirst((Me.txtAttention))
End If
End Sub

**********code ends
[yinyang]
 
Another single line alternative is to use a <UCase, Left, Right, Len, Trim> function combination:

...

MyText = UCase(Left(Trim(MyText), 1)) & Right(Trim(MyText),Len(Trim(MyText)) - 1)

* warning: this is not entirely failsafe but works a treat
[yinyang]
 
A shorter version of shannonp1's solution:
Mid(MyText, 1, 1) = UCase(Left(MyText, 1))
However, this assumes there are no leading spaces in MyText. Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Still need to upcase though don't you?

Your version:
UCase(Mid(MyText, 1, 1))

My version:
UCase(Left(MyText, 1))

Also, you would still need to concatenate the rest of MyText onto it wouldn't you? {Right(Trim(MyText),Len(Trim(MyText)) - 1)}
[yinyang]

 
Shannonp1,

No, you're misunderstanding my code. 'Mid', when used on the left side of an &quot;=&quot; (assignment) statement, lets you change a substring embedded within a string. You can't change its length, but you can change its content. Here it's changing just the first character, Mid(MyText, 1, 1).

BTW, instead of Right(s, Len(s) - 1) consider using Mid(s, 2). When you leave out the third parameter of Mid(), you get from the starting point to the end of the string. Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
There is some excellent programming in this thread!

I have a first name and last name control on my form. The following procedure and function eliminates the spaces (john son becomes johnson) and then capitalizes the first letter of the name (johnson becomes Johnson).

Code:
done
Private Sub BuyerOwnerLastName_AfterUpdate()
'Strips spaces from text box control on form and
'capitalizes first letter of entry
    If Not IsNull(Me.BuyerOwnerLastName.Value) Then
'This only works if there is something in the control, i.e., not a null
    		Me.BuyerOwnerLastName.Value = ProperNoSpace
(Me.BuyerOwnerLastName.Value)
    End If
End Sub

Code:
done
Public Function ProperNoSpace(ByVal strString As String) As String
'Used with procedure above
        strString = Replace(strString, &quot; &quot;, &quot;&quot;, 1, -1, vbTextCompare)
'Spaces stripped
        strString = StrConv(strString, vbProperCase)
'First letter capitalized
        ProperNoSpace = strString
'Cleaned-up string replaces old string
End Function

Judge Hopkins

&quot;...like hunting skunks underwater....&quot;
John Steinbeck, The Grapes of Wrath
 
Oops! Forget that &quot;done&quot;! Judge Hopkins

&quot;...like hunting skunks underwater....&quot;
John Steinbeck, The Grapes of Wrath
 
hehe, I never knew that RickSpr...thansk for the insight. Judge Hopkins: nice piece of code
[yinyang]
 
Try this I got this from this site

strconv(me.text1.value,vbpropercase)

Works great and easier than writing all that code. Later.
 
titan514, very nice little piece o code :D

____________________________________
Eat [pc3], Sleep [pc3], Live [pc3]
 
Wow! Thats another function I didn't know about. Thanks Titan!
[yinyang]
 
Shannon,

I tried your method and it works like a dream. Thanks so much.
There is yet another question i have. Might be easy one or something silly.
I have a lot of lastname and firstname fields all coming from the same table for various people involved for that record and on the same form but on different tabs within the form.
My question is instead of changing your mytext within the procedure, is it possible to just declare the word mytext = the name of the field and then leave your code intact without me having to change the mytext within the statement. doi make sense??

eg:

Lets say I have the following text fields in the form :

pfname
plname
sfname
slname
dfname
dlname
slname

Private Sub fname_AfterUpdate()
fname = mytext
Me!Mytext = UCase(Left(Trim(Me!Mytext), 1)) & Right(Trim(Me!Mytext), Len(Trim(Me!Mytext)) - 1)
End Sub

that way when i want to write the procedure for lname
then all i have to do is change the

lname= mytext and the rest remain untouched..

your input wld be greatly appreciated..
 
Ushameister, no question is silly...especially if you do not know the answer.

Let me add a question to your question: Couldn't you do this? You will have to put the procedure in every control in the form that you want it to run on; put the function in a module. This may save you a little time.

I am not a programmer and I did not test this! It's just a thought-starter....

Code:
Private Sub fname_AfterUpdate()
     fname = mytext
     Me.fname.Value = YourFunction
End Sub

Code:
Public Function YourFunction(ByVal Mytext As String) As String
     Me!Mytext = UCase(Left(Trim(Me!Mytext), 1)) & Right(Trim(Me!Mytext), Len(Trim(Me!Mytext)) - 1)
End Function
Judge Hopkins

&quot;...like hunting skunks underwater....&quot;
John Steinbeck, The Grapes of Wrath
 
Ushameister

I have just read your post and I think I may have an answer for you. Try this piece of code, hopefully its what you're looking for.

**********code begins

Dim ctl As control
'For every field NAME that ends with &quot;name&quot;, this code will execute
For Each ctl In Me.Form
If ctl.Name Like &quot;*name&quot; Then Me.Controls(ctl.Name) = UCase(Left(Trim(Me.Controls(ctl.Name)), 1)) & Right(Trim(Me.Controls(ctl.Name)), Len(Trim(Me.Controls(ctl.Name))) - 1)
Next ctl

**********code ends
[yinyang]
 
shannonp
This is exactly what I want to do as well. Only probelm is I don't know where to put this code. Is it a form Module? OnExit event?
 
DblDogDare

This will work in a sub or module. If you intend to use it often then I would use it within a module, otherwise the easiest way to do it would be to put it directly into the sub routine.

Cheers

[yinyang]
Shann
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top