Ray did a great job with the basic code. I’ve been using it since he first posted it here, and it has worked very well.
Though, I did make some modifications to it, so that I could:
1. Use it in the “On Element Immediate Change” event.
2. Make it allow the use of caps in the middle of a name, without putting a space in the name.
3. Use it with any element, by putting it in a subroutine.
This topic came up recently in another thread, so I thought I would post the modified code here for others to make use of.
The following code can be placed into Global Code (or if you are using version 2, placed into your custom *.sbas file), and then you can simply put cuInitialCaps() into the On Element Immediate Change event for each element you want to use it in. If you use table view to enter or modify data, you will also want to place cuInitialCaps() in the On Element Exit event, because the immediate change event does not work in table view.
Version 2 has an Initial Caps format option in Designer, but it only changes the way the data is displayed in Sesame. It does not change the way it is stored on disk. The problem with that is, if you export the data or use it in a merge letter, it will not be formatted with caps; it will be exactly as it was typed during data entry. So, the solution is to use SBasic code like this to actually change the data before it gets stored.
//======================================================================
// Initial Caps - Basic code from Ray Yoxall, modified by Carl Underwood
SUBROUTINE cuInitialCaps()
Var vStr as String
Var vFirst as Char
Var vSnipet as String
Var vCnt as Int
Var vPos as Int
vCnt = 0
vStr = ThisElement
If @Len(vStr) > 0
{
vPos = @CursorPosition(ThisElement)
// vStr = ToLower(vStr)
// Removal of the previous line allows you to force caps in the middle of a word or name.
// Examples: MacDonald, McBride, StOnge, PO Box, etc.
// Put it back in if you want to only allow the 1st letters to be in caps.
vFirst = @Left(vStr, 1)
vStr = @ReplFir(vStr, vFirst, ToUpper(vFirst))
While vCnt < @Len(vStr)
{
If @Mid(vStr, vCnt, 1) = " "
{
vSnipet = vSnipet + @Left(vStr, vCnt)
vStr = @Del(vStr, 0, vCnt)
vCnt = 0
vFirst = @Left(vStr, 1)
vStr = @ReplFir(vStr, vFirst, ToUpper(vFirst))
}
vCnt = vCnt + 1
}
vSnipet = vSnipet + vStr
ThisElement = vSnipet
CursorPosition(ThisElement, vPos)
}
END SUBROUTINE
//======================================================================