| The Farber Consulting Group, Inc. |
Tel:
(516) 796-6545 / Fax: (516) 796-1273
E-Mail: doron@dfarber.com
Our Web Site: http://www.dfarber.com
FldFRmt Function
Sometimes data is entered in a less than satisfactory way. For instance, input may not start in the first character of the field. Sometimes a user leaves more then one space between words.
I developed a function (FldFrmt) to make sure that character field content is always aligned properly. It is especially handy when a keyboard is not available, as in a pen-based computer. Obviously on a desktop computer it prevents any extra work in case the field is not entered properly.
If you don't need to validate a field any other way, the function can be called directly in the field's Valid, as follows:
@ 19,2 GET m.MemVar VALID FldFrmt(VARREAD(),;
,_CUROBJ)
If you need to perform other validation on the field, you can call this routine from the validation routine or call both FldFrmt and the validation routine.
You can also use a hot key or a menu short cut to activate the FldFrmt function. The following is the hot key assignment:
ON KEY LABEL F4 DO FldFrmt WITH VARREAD(),_CUROBJ
Set up menu shortcut like this:
DEFINE BAR 4 OF MyPop PROMPT "Field Format" COLOR SCHEME 3 KEY ALT-F, "[ALT-F]" ;
SKIP FOR EMPTY(VARREAD()) OR
_CUROBJ==0
.
.
.
ON SELECTION BAR 4 OF MyPop DO FldFrmt WITH VARREAD(),_CUROBJ
FldFrmt uses Foxpro's STRTRAN()
function to reduce additional spaces between words. It can
perform search and
replace on multiple occurrences at the same time. The following
line reduces all pairs of consecutive spaces
to a single space:
m.NowFrmt=STRTRAN(m.NowFrmt," "," ")
Page 2
For instance, if there are eight spaces between two words, when the STRTRAN() above is executed, the space is reduced to 4 characters. The second time STRTRAN() is applied, the space is reduced to 2 characters. After three times, there's only one space. If there were 16 characters between the 2 words, then 4 passes take care of the problem. I doubt anybody is going to accidentally enter a line that has 16 characters between 2 words, but FldFrmt() can handle it.
After reducing all spaces to single characters, FldFrmt pads the result on the right to its original length.
*================================================
* FUNCTION........: FldFrmt
* Author..........: Doron Farber
* Created.........: 04/10/94
* Project.........: Common
* Copyright.......: (c) The Farber Consulting Group
* Purpose.........: Reformat the field
* Parameters List.: m.WhichField - Gets the name
* ................: of the field Via VARREAD()
* ................: m.WhichObj - Gets the object
* ................: Number of the field via
* ................: _CUROBJ)
* Calling Samples.: (Via the field)
*.................: VALID FldFrmt(VARREAD(),;
*.................: _CUROBJ)
*.................: (Via a hotkey)
*.................: ON KEY LABEL "MyKey" DO ;
*.................: FldFrmt WITH VARREAD() ,;
*.................: _CUROBJ
* ................: (Via a menu short cut)
*.................: ON SELECTION BAR 4 OF MyPop
*.................: DO FldFrmt WITH VARREAD(),;
*.................:_CUROBJ
* Return Type.....: .T.
* Notes...........: A field GET must be active
* ................: before calling this function.
*================================================
FUNCTION FldFrmt
PARAMETERS m.WhichField,m.WhichObj
PRIVATE m.FldCnt,m.NowFrmt,m.NowParams
* m.FldCnt - Holds the length of the field before format
* m.NowFrmt- Holds the length of the field after format
* Get the parameters value
m.NowParams=PARAMETERS()
DO CASE CASE m.NowParams<2 WAIT WINDOW "You didn’t pass enough parameters" RETURN .T.
CASE TYPE('m.WhichObj')<>'N'
WAIT WINDOW "MemVar WhichObj must be a numeric type"
RETURN .T.
CASE m.WhichObj<1 WAIT WINDOW "_CUROBJ must be greater then zero" RETURN .T.
CASE TYPE('m.WhichField')<>'C'
WAIT WINDOW "Field is not a character string"
RETURN .T.
ENDCASE
* Get the content of the field
m.FldCnt=EVALUATE(m.WhichField)
IF ! EMPTY(m.FldCnt) m.NowFrmt=ALLTRIM(m.FldCnt) DO WHILE " " $ m.NowFrmt m.NowFrmt=STRTRAN(m.NowFrmt," "," ") ENDDO
STORE PADR(m.NowFrmt,LEN(m.FldCnt)) TO (m.WhichField) _CUROBJ=m.WhichObj SHOW GET ENABLE (m.WhichField) ENDIF RETURN .T.