Normal Topic Initial Caps (Read 5982 times)
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Initial Caps
Feb 23rd, 2004 at 4:09pm
Print Post Print Post  
The following code capitilizes the first letter of every word in an Element, and makes the rest of the characters lowercase.  It was written for the On Exit Event of the Company Element in the database Customers.db, but can easily be modified to any text field by just changing the Element name in two places.

[code]
var vName as String
var vFirst as Char
var vCnt as int
var vSnipet as String

vCnt = 0
vName = Company //Company is a Text Element 
if @Len(vName) > 0 then
{
vFirst = @Left(vName,1)
vName = ToLower(vName)
vName = @ReplFir(vName, vFirst, ToUpper(vFirst))
While vCnt < @Len(vName)
{
  if @Mid(vName,vCnt,1) = " " then
  {
   vSnipet = vSnipet + @Left(vName, vCnt)
   VName = @Del(vName, 0, vCnt)
   vCnt = 0
   vFirst = @Left(vName,1)
   vName = @ReplFir(vName, vFirst, ToUpper(vFirst))
  }
  vCnt = vCnt + 1
}
vSnipet = vSnipet + vName
Company = vSnipet
}
[/code]

BREAKDOWN OF ABOVE CODE
vName = Company //Company is a Text Element 
**This gets the String from the field

if @Len(vName) > 0 then
***If the string has some data in it

{
vFirst = @Left(vName,1)
***This gets the First Character of the First word

vName = ToLower(vName)
***This makes the rest of the string lowercase

vName = @ReplFir(vName, vFirst, ToUpper(vFirst))
***This replaces the First Character with the uppercase
***version

While vCnt < @Len(vName)
***Loop that goes until the end of the string

{
  if @Mid(vName,vCnt,1) = " " then
***If it is a space then a new word follows that gets
***capitilized

  {
   vSnipet = vSnipet + @Left(vName, vCnt)
***Grab all characters including the space

   VName = @Del(vName, 0, vCnt)
***Delete all the characters that were just grabbed

   vCnt = 0
***Reset Counter to 0  so Reading will begin from
***beginning of VName

   vFirst = @Left(vName,1)
   vName = @ReplFir(vName, vFirst, ToUpper(vFirst))
***Same as the above pieces

  }
  vCnt = vCnt + 1
***Move along the string looking for a space

}
vSnipet = vSnipet + vName
***Combine both strings for the original string that is
***capitilized

Company = vSnipet
***Place the new Capitilized String back in the element
}
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1350
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Initial Caps
Reply #1 - Aug 9th, 2007 at 12:50pm
Print Post Print Post  
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.


Code
Select All
//======================================================================
// 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
//====================================================================== 

  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Infinity
Senior Member
Members
*****
Offline


Diagonally parked in a
parallel dimension

Posts: 1290
Location: Massachusetts
Joined: May 27th, 2005
Re: Initial Caps
Reply #2 - Aug 9th, 2007 at 1:10pm
Print Post Print Post  
Thanks Carl, this is marvelous.  Questions: why immediate change instead of just change?  And what would I have to modify to use this in a Mass Update to freshen existing data?
  

**
Captain Infinity
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1350
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Initial Caps
Reply #3 - Aug 9th, 2007 at 11:55pm
Print Post Print Post  
Infinity wrote on Aug 9th, 2007 at 1:10pm:
Thanks Carl, this is marvelous.  Questions: why immediate change instead of just change?

So that the changes occur as you are typing, rather than after you exit the element.

Quote:
And what would I have to modify to use this in a Mass Update to freshen existing data?

Nothing needs to be modified. You can use it as is, or you could remove the two lines that refer to CursorPosition since they are not needed in a mass update. Those two lines are only needed when it is used in "On Element Immediate Change". (They'll have no effect in a mass update.)

If you only need to mass update one field, you could remove the code out of the subroutine, and just place it directly into the mass update event for that element. If you have multiple fields to update, you would place the entire subroutine into Global Code, and just place "cuInitialCaps()" in each element's event for which you want to update.

As always, though, with Mass Updates: BACKUP FIRST. Just in case.  Wink
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged