Replace first character of name in formfield (Instr function)

J

jacobk

Hi,

I'm looking for code for the following:

I have a formfield (textbox) for Firstnames.
I'd like to automaticly replace the first character of each name in UpperCase.
So, if 3 firstnames are entered in this Textbox, all the first characters of
these three firstnames has to be replaced in Uppercase.
I've tried different ways with the Instr function, but not found a solution
yet.
Anyone can help?

Jacob.
 
G

Greg Maxey

Maybe running something like this on exit from the field:

Sub FFOnExit()
With ActiveDocument
.Unprotect
.FormFields("Text1").Range.Case = wdTitleWord
.Protect wdAllowOnlyFormFields, True
End With
End Sub

Hi,

I'm looking for code for the following:

I have a formfield (textbox) for Firstnames.
I'd like to automaticly replace the first character of each name in
UpperCase. So, if 3 firstnames are entered in this Textbox, all the
first characters of these three firstnames has to be replaced in
Uppercase.
I've tried different ways with the Instr function, but not found a
solution yet.
Anyone can help?

Jacob.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
J

jacobk

Hello Greg,

Perhaps I was'nt clear enough, but it is a textbox field on a Userform.
Your solution does'nt work on Userforms, right?
 
G

Greg Maxey

Right, but you said formfield ;-)

Something like this may work:

Private Sub txtFirstName_Change()
Dim pStr As String
pStr = Me.txtFirstName.Value
If Not Left(pStr, 1) Like "[A-Z]" Then
Me.txtFirstName = UCase(Left(pStr, 1))
End If
On Error GoTo Err_Handler
If Mid(pStr, Len(pStr) - 1, 1) = Chr(32) Then
Me.txtFirstName = Left(pStr, Len(pStr) - 1) & UCase(Right(pStr, 1))
End If
Exit Sub
Err_Handler:
End Sub

Where txtFirstName is the name of your UserForm text field.


Hello Greg,

Perhaps I was'nt clear enough, but it is a textbox field on a
Userform. Your solution does'nt work on Userforms, right?

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
J

jacobk

Hi Greg,

Again sorry for the misunderstanding about the formfield.
Unfortunatly your second solution was not quite what I looked for.
I found just a solution for myself.
Perhaps it is not the most optimal solution, but it works for me.
Here is my code:

Sub test()

Dim strTest As String
Dim strTemp As String
Dim strResult As String
Dim iLength As Integer
Dim iPos As Integer
Dim j As Integer
Dim k As Integer

strTest = "maria johanna cleopatra angy"
iLength = Len(strTest)

If InStr(1, strTest, " ") = 0 Then
strTest = UCase(Left(strTest, 1)) & LCase(Right(strTest, iLength - 1))
Else
j = 1
Do
iPos = InStr(j, strTest, " ")
If iPos = 0 Then
strTemp = UCase(Mid(strTest, j, 1)) & LCase(Mid(strTest, j +
1, iLength - j))
strResult = strResult & strTemp
Exit Do
Else
k = iPos - Len(strResult)
strTemp = UCase(Mid(strTest, j, 1)) & LCase(Mid(strTest, j +
1, k - 1))
strResult = strResult & strTemp
j = iPos + 1
End If
Loop Until j >= iLength
End If
Debug.Print strResult

End Sub

Perhaps it may halp others.

Greetings and thanks again,

Jacob.

Greg Maxey said:
Right, but you said formfield ;-)

Something like this may work:

Private Sub txtFirstName_Change()
Dim pStr As String
pStr = Me.txtFirstName.Value
If Not Left(pStr, 1) Like "[A-Z]" Then
Me.txtFirstName = UCase(Left(pStr, 1))
End If
On Error GoTo Err_Handler
If Mid(pStr, Len(pStr) - 1, 1) = Chr(32) Then
Me.txtFirstName = Left(pStr, Len(pStr) - 1) & UCase(Right(pStr, 1))
End If
Exit Sub
Err_Handler:
End Sub

Where txtFirstName is the name of your UserForm text field.


Hello Greg,

Perhaps I was'nt clear enough, but it is a textbox field on a
Userform. Your solution does'nt work on Userforms, right?

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top