Modify the "Proper" Function

G

Guest

I have a function (copied from a public website) that converts the first
character of each word in a control to upper case. Don't quite know enough VB
(yet) to write it myself. I would like to modify it to ignore numbers (so
17th doesn't become 17Th)
If anyone knows a relatively simple way to modify it, or perhaps a link to a
version that already does what I want. Thanks in advance.

Here is the code

Public Function Proper(X)
' Capitalize first letter of every word in a field.
' Use in an event procedure in AfterUpdate of control;
' for example, [Last Name] = Proper([Last Name]).
' Names such as O'Brien and Wilson-Smythe are properly capitalized,
' but MacDonald is changed to Macdonald, and van Buren to Van Buren.
' Note: For this function to work correctly, you must specify
' Option Compare Database in the Declarations section of this module.
Dim Temp$, C$, OldC$, i As Integer
If IsNull(X) Then
Exit Function
Else
Temp$ = CStr(LCase(X))
' Initialize OldC$ to a single space because first
' letter needs to be capitalized but has no preceding letter.
OldC$ = " "
For i = 1 To Len(Temp$)
C$ = Mid$(Temp$, i, 1)
If C$ >= "a" And C$ <= "z" And _
(OldC$ < "a" Or OldC$ > "z") Then
Mid$(Temp$, i, 1) = UCase$(C$)
End If
OldC$ = C$
Next i
Proper = Temp$
End If
End Function
 
P

plh

I have a function (copied from a public website) that converts the first
character of each word in a control to upper case. Don't quite know enough VB
(yet) to write it myself. I would like to modify it to ignore numbers (so
17th doesn't become 17Th)
If anyone knows a relatively simple way to modify it, or perhaps a link to a
version that already does what I want. Thanks in advance.

Here is the code

Public Function Proper(X)
' Capitalize first letter of every word in a field.
' Use in an event procedure in AfterUpdate of control;
' for example, [Last Name] = Proper([Last Name]).
' Names such as O'Brien and Wilson-Smythe are properly capitalized,
' but MacDonald is changed to Macdonald, and van Buren to Van Buren.
' Note: For this function to work correctly, you must specify
' Option Compare Database in the Declarations section of this module.
Dim Temp$, C$, OldC$, i As Integer
If IsNull(X) Then
Exit Function
Else
Temp$ = CStr(LCase(X))
' Initialize OldC$ to a single space because first
' letter needs to be capitalized but has no preceding letter.
OldC$ = " "
For i = 1 To Len(Temp$)
C$ = Mid$(Temp$, i, 1)
If C$ >= "a" And C$ <= "z" And _
(OldC$ < "a" Or OldC$ > "z") Then
Mid$(Temp$, i, 1) = UCase$(C$)
End If
OldC$ = C$
Next i
Proper = Temp$
End If
End Function

It should ignore numbers the way it is, because the characters 0 through 9 come
before a through z in the ASCII table.
http://web.cs.mun.ca/~michael/c/ascii-table.html
If that is not in fact the case try:

If C$ >= "a" And C$ <= "z" And _
(OldC$ < "a" Or OldC$ > "z") And _
InStr("1234567890", OldC$) = 0 _
Then
IIUC, You would not want to perform the InStr function with C$.
-plh
 
G

Guest

Thank you plh. That fixed the problem. You're a genius.

plh said:
I have a function (copied from a public website) that converts the first
character of each word in a control to upper case. Don't quite know enough VB
(yet) to write it myself. I would like to modify it to ignore numbers (so
17th doesn't become 17Th)
If anyone knows a relatively simple way to modify it, or perhaps a link to a
version that already does what I want. Thanks in advance.

Here is the code

Public Function Proper(X)
' Capitalize first letter of every word in a field.
' Use in an event procedure in AfterUpdate of control;
' for example, [Last Name] = Proper([Last Name]).
' Names such as O'Brien and Wilson-Smythe are properly capitalized,
' but MacDonald is changed to Macdonald, and van Buren to Van Buren.
' Note: For this function to work correctly, you must specify
' Option Compare Database in the Declarations section of this module.
Dim Temp$, C$, OldC$, i As Integer
If IsNull(X) Then
Exit Function
Else
Temp$ = CStr(LCase(X))
' Initialize OldC$ to a single space because first
' letter needs to be capitalized but has no preceding letter.
OldC$ = " "
For i = 1 To Len(Temp$)
C$ = Mid$(Temp$, i, 1)
If C$ >= "a" And C$ <= "z" And _
(OldC$ < "a" Or OldC$ > "z") Then
Mid$(Temp$, i, 1) = UCase$(C$)
End If
OldC$ = C$
Next i
Proper = Temp$
End If
End Function

It should ignore numbers the way it is, because the characters 0 through 9 come
before a through z in the ASCII table.
http://web.cs.mun.ca/~michael/c/ascii-table.html
If that is not in fact the case try:

If C$ >= "a" And C$ <= "z" And _
(OldC$ < "a" Or OldC$ > "z") And _
InStr("1234567890", OldC$) = 0 _
Then
IIUC, You would not want to perform the InStr function with C$.
-plh
 

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