4

The LA Fox Developer Newsletter
May 1998
Hot Tips...
Effective Use of
WITH...ENDWITH
by Rod Paddock

Do you get tired of typing the object hierarchy code for objects all the time? Try this tip and see if it doesn’t make you more productive.

Change this:

THISFORM.PAGEFRAMEI.PAGE2.TEXTI.ENABLED .T.
THISFORM.PAGEFRAMEI.PAGE2TEXT2.ENABLED = .T.
THISFORM.PAGEFRAMEI .PAGE2.TEXT3.ENABLED = .T.
THISFORM.PAGEFRAMEI .PAGE2.C0MMANDBUTrONI .CAPTION=”OK”.
THISFORM.PAGEFRAMEI.PAGE2.TEXTI.ENABLED = .T.

To This:

WITH THISFORM.PAGEFRAMEI.PAGE2

.TEXTI.ENABLED = .T. .TEXT2.ENABLED = .T. TEXT3.ENABLED = .T. .COMMANDBUTTONI .CAPTION=”OK” .TEXTI .ENABLED = .T.

ENDWITH

Your fingers will thank you.


Working Effectively With the Form’s AutoCenter Property in VFP 3.0/5.0
One of the really neat properties in VFP is the form property, AutoCenter. Setting AutoCenter to .T. will cause it to be automatically centered at runtime. One drawback is that it also centers it in the Form Designer causing you to have to move it up and over to work with it. Well, how can we have the form auto center at runtime, but not at design time? The answer is quite simple. Leave AutoCenter .F. in the Form Designer and place

THIS.AutoCenter = .T.

in the lnit Method for the form. Place that code in your base form and you’ll never have to worry about centering forms again. VFP 5.0 took care of that annoying little problem.


The SetAll Method
Do you find yourself setting the same property for multiple items at a time. For example, you decide you want the font for all the header captions in a grid to all be 12 point Anal with a blue background and yellow text. Now you could manually set the Font and FontSize properties in the Form Designer or you could
(Con’t, page 8)
Visual FoxPro Q&A
by Gary DeWitt
Question: How can I implement an automatically incrementing primary key?

Answer: The most foolproof way I can find, and I’ve tried many, is to use a “system” table that stores the most-recently incremented number for each field. I have a GetPKey() function that will lock the record in the system table, increment the number, unlock the record and return the number. If I wanted to, GetPKey() could be used as the default value for the field in the database. I choose not to do so for many reasons, but that is the simplest way to “autonumber” in VFP, IMHO. It would be nice if this were a built-in feature of the VFP engine as there are many drawbacks to the “roll your own” techniques required now. Forexample, functions fordefaultvalues don’tworkvia ODBC.

This is the most bomb-proof I’ve come up with yet. I’ve fiddled with many different means of getting unique values, including various datetime-related values, and have settled on this. I always pass the fullpath to the table and the name of the field to increment.
FUNCTION GETPKEY(tcTable, tcField)

LOCAL Inkeyvalue, Inselect, Icalias, Ilopen

* Check for table & field. IF PCOUNTŘ =0 tctable = “mrsys’ tcfield = “bKey”
END4F

* Save current work area Inselect = SELECT(O)

*__ Try to open the table
Icalias = justfname(tctable)
Ilopen !USED(lcalias)
IF Ilopen
USE (tctable) AGAIN IN 0 ALIAS (Icalias)
ENDIF
SELECT (Icalias)

* Lock and increment the value
IF RLOCKO
Inkeyvalue = &tcfield + I
REPLACE (tcfield) WiTH Inkeyvalue
UNLOCK
ELSE
Inkeyvalue 0
ENDF

IF Ilopen
USE
ENDIF
* Restore work area SELECT (Inselect)

RETURN Inkeyvalue
Page 4

4