Category : Dbase (Clipper, FoxBase, etc) Languages Source Code
Archive   : TN9005.ZIP
Filename : INCRMENT.TXT
IV. Due to the limitations of this media, certain graphic elements
such as screen shots, illustrations and some tables have been
omitted. Where possible, reference to such items has been deleted.
As a result, continuity may be compromised.
TechNotes is a monthly publication from the Ashton-Tate Software
Support Center. For subscription information, call 800-545-9364.
Incrementing the Easy Way
Michael P. Dean
Have you ever wanted to have a numeric field automatically increment
for each new record, only to find out that you would have to write a
program to accomplish the task? Well, you'll find it written for you
in this article. You will be shown two ways to increment a field
using format files. The first example requires that you create a
small User Defined Function (UDF), and then modify the .FMT file by
adding a few lines of code. The only disadvantage of doing it this
way is that if you ever change anything in your format file, your
changes are overwritten when the .FMT file is regenerated and you will
have to edit the file and type in the changes once again.
With the dBASE IV Developer's Edition we have the availability of the
Template Language which leads us to the alternative method. The
second example is a modified .COD file for a new form generator that
will automatically write all necessary code needed including the UDF!
By doing everything through a .GEN file, you don't have to worry about
re-writing any code just because you made a few changes to your
screen. You can also use this new .GEN file for regular format files
that do not have any auto incrementing fields. All you have to do is
rename the current FORM.GEN to something else (how about FORMOLD.GEN?)
and when you run the template file compiler, use the -o (output file)
option to output a new FORM.GEN. The compiler command would appear as
shown below:
DTC -i Autoincr.COD -o FORM.GEN
Alternatively, you can simply rename the file to FORM.GEN. It would
be advisable to make a backup copy of the FORM.GEN before any
modifications were made if you are using a modified version other than
what has come with your original diskettes. If you have any other
questions, refer to the Template Language book that comes with the
Developer's Edition.
For users who do not have the Developer's Edition, AutoIncr.GEN can be
obtained from the Ashton-Tate or CompuServe BBS. See back cover for
phone numbers and hours of access.
Things to Remember
A couple of points are worth mentioning before we get started.
Regardless of which method you choose to accomplish auto incrementing,
you must keep a few things in mind.
ù When using this form for auto incrementing, you must set an
index whose primary index key (controlling field) is the field
to be incremented.
ù In version 1.0, you can not have CARRY set to YES for any
other field in a screen form that uses auto-incrementing.
This will cause conflicts with the DEFAULT VALUE option used
in the UDF.
ù If you enter a new value into the field that contains the
default, this will throw the numbering sequence off. The
starting (default) value is determined by evaluating the last
sequence number in the index and incrementing it according to
the number you specify. Therefore, if you need to start out
at a specific number, add a record to the file without the
UDF/format file in use and insert the starting number.
ù Every time the UDF is modified and saved, do not forget to
compile it before activating the screen form or doing an
APPEND. An error message, "Illegal call to the compiler" may
result.
With these points in mind, hold on to your data, because here we go.
First, write the following program and compile it:
* Program ...: Fldincr.PRG
* Author ....: Michael P. Dean
* Date ......: Wed 11-29-1989
* Versions ..: dBASE IV Version 1.0
* Notes .....: UDF to automatically increment a numeric field.
FUNCTION Autoincr
PARAMETERS incr_value
fld_value = fld_value + incr_value
RETURN fld_value
* EOP: Fldincr.PRG
Then, make the following change to your .SCR file. Do this by typing
the following command from the dot prompt:
MODIFY SCREEN
From the Control Center, select the form to have this change and
choose the Modify layout option. Once you are on the Forms design
work surface, add a new field or highlight an existing numeric field
on whicht you want to perform auto incrementing. Under Edit options:
Default value, enter Autoincr(n) where n is the number to increment
by.
Inserting AutoIncr(4) under Default value will automatically
increment the field by 4. Decremnting by 2 would be expressed as
AutoIncr(-2).
Saving your form will generate an .FMT file. After this is complete,
make the following changes by typing from the dot prompt:
MODIFY COMMAND
Locate the line which appears below. Press the = key to position your
cursor above this line. Pressing Ctrl-N will open up blank lines for
you to include additional command and comment lines listed below.
You can use the Words:Locate option to search for the string "@ SAY
GETS". This should take you directly to the line referenced here.
Look for:
*Ä @ SAY GETS Processing.
Above this line, type in:
*Ä Store the current record number to a variable, then
* store the last number to the memory variable sys_value
sys_record = RECNO()
GO BOTTOM
sys_value = {FLD_FIELDNAME}
*Ä If at the beginning or end of file,
* do not attempt to go to the record
* stored in sys_record
IF sys_record <= RECCOUNT()
GO sys_record
ENDIF
Anything that is above the "@ SAY GETS...." line will get processed
only once. All remarks (lines beginning with an asterisk) are, of
course, optional. However, putting them in may be helpful to someone
else who may later need to modify the code, or remind you when you
need to look at the code at a later date.
When this is finished, select this form from the Control Center, or
type in SET FORMAT TO
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/