Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

incrementing a string 6

Status
Not open for further replies.

maggielady

Technical User
Jan 3, 2003
60
0
0
US
I have these lines of code and I want the family_id to increment and I want the key field in the syskey table to increment the same as the family_id. I can't get it to work. Could someone Please give me some help???
select 18
nextnum=key (this is numeric)
* FAMILY PROFILE - CREATE
SELECT 2
APPEND BLANK (family_id is char)
REPLACE FAMILY_ID WITH str(nextnum+1)
TFAMID=FAMILY_ID
nextnum=(nextnum+1)
select 18
nextnum=(nextnum+1)
18 is the syskey table and 2 is the family table I need the number to go in and be incremented for each new family
Thanks a bunch in advance for your help!

 
select 18
nextnum=key (this is numeric)
* FAMILY PROFILE - CREATE
SELECT 2
APPEND BLANK (family_id is char)
REPLACE FAMILY_ID WITH str(nextnum+1)
TFAMID=FAMILY_ID
nextnum=(nextnum+1)
select 18
REPLACE key WITH nextnum

Also be aware that STR() returns a 10-character string.

Jim
 
STR(nExpression,nLength,nDecimalPlaces)

REPLACE FAMILY_ID WITH alltrim(str(nextnum+1,7,0))
 
I forgot that I need leading zero's on this number. It has to have leading zeros and then the number. How do put in the leading zeros?
 
Code:
REPLACE FAMILY_ID WITH transform(nextnum+1, "@L 9999999")
Change the number of 9's to the length of your Family_ID field.

Rick
 
To add leading zeros, look at the PADL() function.

REPLACE FAMILY_ID WITH ;
padl(alltrim(str(nextnum+1,7,0)),7,"0")

 
I recommend that you be careful using this approach. If you are in a multi-user environment (or ever expect to get there) you can be assured that eventually, you will find a numeric mis-match. Try this:

SELECT SysKey
llLocked = .f.
DO WHILE !llLocked
IF RLOCK()
lnNextNum = SysKey.NextNum+1
REPLACE SysKey.NextNum WITH lnNextNum
SELECT FamilyTable
INSERT INTO FamilyTable (NextNum, Field2, ...);
VALUES (lnNextNum, NextValue, ...)
llLocked = .t.
SELECT SysKey
UNLOCK
ENDIF
ENDDO

You may also wish to include code to get you out of an infinate loop if for some reason another user locks the SysKey record or table.
 
TheMissionary (IS/IT--Manageme)
Try this:

SELECT SysKey
llLocked = .f.
DO WHILE !llLocked
IF RLOCK()
lnNextNum = SysKey.NextNum+1
REPLACE SysKey.NextNum WITH lnNextNum
SELECT FamilyTable
INSERT INTO FamilyTable (NextNum, Field2, ...);
VALUES (lnNextNum, NextValue, ...)
llLocked = .t.
SELECT SysKey
UNLOCK
ENDIF
ENDDO

You may also wish to include code to get you out of an infinate loop if for some reason another user locks the SysKey record or table.

I would also recommend to include FLUSH command after INSERT. I've found that on the newer systems, with "heavy" HDD buffering (i.e. RAID), mem. vars. content is not necesserily transferred onto the disk by INSERT (or REPLACE) command. Actully, any FoxPro commands for writing onto disk from mem. vars. should be followed by FLUSH command to be on the safer side. (I learnt it hard way, BTW.)

HTH


Regards,

Ilya
 
I Agree

Put in the FLUSH!

I also learned the hard way - about two weeks ago. Now all I have to do is remember it!! Must be the altzheimers acting up.
 
Thanks you guys!! Works great! Appreciate all the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top