How Can i create a function that returns the case of a word

D

DZ

Greeting

How Can i create a function that returns the case of a
word.

Example:

Function ReturnCaseOfWord(strWord As String) As String

End Function

I want the function to return a string that is the case of
the word: "Upper", "Lower" or "Proper".

Thanks for any help
DZ
 
C

Chris Nebinger

The key is to use Option Compare Binary:

Option Compare Binary
Option Explicit
Function ReturnCaseOfWord(strWord As String) As String
Select Case strWord
Case UCase(strWord)
ReturnCaseOfWord = "Upper"
Case LCase(strWord)
ReturnCaseOfWord = "Lower"
Case StrConv(strWord, vbProperCase)
ReturnCaseOfWord = "Proper"
Case Else
ReturnCaseOfWord = "Other"
End Select
End Function


Chris Nebinger
 
J

John Nurick

Hi DZ,

This is fairly simple if you're only interested in the 26 letters of the
English alphabet.

Something like this:

Dim A As Long
Dim j As Long
Dim blFirstUpper As Boolean
Dim blRestUpper As Boolean
Dim blRestLower As Boolean
Dim blNotAWord As Boolean
Dim S As String

'First character
A = Asc(Left(strWord,1))
If A >= 65 And A <= 90 Then
blFirstUpper = True
ElseIf A >= 97 And A <= 122 Then
blFirstUpper = False
Else
blNotAWord = True
End If

'Remainder
blRestUpper = True
blRestLower = True
For j = 2 to Len(strWord)
A = Asc(Mid(strWord(j, 1))
If A >=65 And A <= 90 Then
blRestLower = False
ElseIf A < 97 Or A > 122 Then
blNotAWord = True
End If
If A >= 97 And A <= 122 Then
blRestUpper = False
ElseIf A < 65 Or A > 90 Then
blNotAWord = True
End IF
Next

'Work it out
If blNotAWord Then
S = "Not a word"
Else
'a series of logical tests, e.g.
If blFirstUpper And blRestLower Then S = "Proper"
If blFirstUpper And blRestUpper Then S = "Upper"
If Not blFirstUpper And BlRestLower Then S = "Lower"
... otherwise S = "Mixed"
End If

If however you need to cope with accented characters, non-western
scripts and the like it gets quite complicated!
 
J

John Nurick

I've just had a neater idea, perhaps.

Apply strConv() to your word with various arguments, and see which
argument doesn't change the word.
 
J

John Nurick

Hi Chris,

Much neater than my solution.

I tried it out with Central European and Greek characters (on a form, to
avoid Unicode problems in the Immediate pane) and it fails on these,
which must stem from the way the VBA string functions handle Unicode
(Windows 2000/Access 2002 at this end). With luck these will eventually
be fixed<g>.
 
C

Chris Nebinger

I'm one of the arrogant American's who thinks the world
does not exist outside of the US. :)


Actually, I've never had to work with Unicode, so I don't
have any advice for that.


Chris Nebinger

-----Original Message-----
Hi Chris,

Much neater than my solution.

I tried it out with Central European and Greek characters (on a form, to
avoid Unicode problems in the Immediate pane) and it fails on these,
which must stem from the way the VBA string functions handle Unicode
(Windows 2000/Access 2002 at this end). With luck these will eventually
be fixed<g>.

The key is to use Option Compare Binary:

Option Compare Binary
Option Explicit
Function ReturnCaseOfWord(strWord As String) As String
Select Case strWord
Case UCase(strWord)
ReturnCaseOfWord = "Upper"
Case LCase(strWord)
ReturnCaseOfWord = "Lower"
Case StrConv(strWord, vbProperCase)
ReturnCaseOfWord = "Proper"
Case Else
ReturnCaseOfWord = "Other"
End Select
End Function


Chris Nebinger

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
.
 

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