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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a sequentially numbered field 2

Status
Not open for further replies.

danyd

MIS
May 25, 2011
6
US
Re: Dbase lV I am unable to find the command to have the data in a field change for each record so that I end up with a sequentially numbered field ie first record 1000, second record 1001, third record 1002...etc.

Any help will be appreciated.
:)
 
Danyd,
Are you looking for a routine to increment the number ?
 
Yes, I need to number each record in the database and am thinking there must be a simple way to do this on the command line.

thanks..
 
For dBase IV you need to code it yourself. The windows versions, starting with VdB 7 have an 'autoincrement' field type that does what you want, but it's not available in dBase IV.

Alternatively you could use the record number, but never, ever pack or sort your data file then.

Mike.
 
Thanks Mike... I thought about using the record number which actually would work for me. Can you tell me how to get the record number to appear in my field structure?

 
It will never show up in a structure listing, but you reference it with recno().

Mike
 
Thanks Mike...Sorry for being so dense but what do you mean by "reference it"

Can you tell what to enter on the command line ie. replace custid with ?recno()

Fred
 
Fred,
If you store recno() to mrecno, then on the next line type in replace custid with mrecno, that will do it for you. Although it would be best if you have a table that you use specifically for this.
For example if you create a table called custid, with just a numeric field in it.
Then when you are gonig to add a new record you can
sele 1
use custid
sele 2
use customers && or whatever the table is called that you are adding to
Do while Condition
sele 1
store custid+1 to mcustid
sele 2
appen blank
replace custid with mcustid
sele 1
replace custid with mcustid
sele 2
skip
loop
enddo

This is a rough idea of what you may need to do, bear in mind the code isnt correct but the method should work for you

Aiden
 
Danyd,

You reference it just like a field, memory variable or function like date():

?recno() displays the record number

if custid = recno()
do stuff
endif

replace custid with recno()

But, as Aiden suggested, it's better to roll your own. It has the advantage of being able to use any starting number you want. Also, for recno to be a truly incrementing and unique number, you can NEVER, EVER sort or pack your table (and no one else should either - but there's nothing to stop someone).

Like Aiden suggests, create a new table, CustID, with 1 field: CustID, Numeric, 10.0. Add 1 record to it with the starting customer ID number, e.g. 1001. In your program, when you are ready to add a new customer (this is different code than Aiden's but both work):

Use Customer && or whatever the table name is.
Use CustID in 2
append blank
Replace CustID with CUSTID->CustID + 1;
CUSTID->CustID with CustID + 1

Note: The manual says don't replace in another work area. This method works fine as long as the replaced field isn't in an index, as above.

Mike.
- There are an infinite number of ways to dBase your programming...
 
DanyD,

2 things: First, I realized I made a typo above. the replace line should be:

replace CustID with CUSTID->CUSTID + 1;
CUSTID->CustID with CUSTID->CUSTID + 1

Second, I just reread the thread. It looks like you're trying to assign new customer ID's. In that case, use this code loop to add a custID to an existing customer list:

use customer
use custid in 2
SCAN
replace CustID with CUSTID->CUSTID + 1;
CUSTID->CustID with CUSTID->CUSTID + 1
ENDSCAN

Then, insert my first code (with the corrected replace line) in your program for adding new customers.

Mike.

Hope that helps
 
Ok, so I must be bored today... And my reading comprehension is bad... I reread it again, and in this case, recno() may work for you because you're just assigning a field first time:

replace all recordnum with recno() + 1000

That will make the first customer 1001, the second one 1002, etc.

Then do the extra table method above to add new customer numbers programmatically.

Mike.
 
Yippee.. I got it. The last suggestion worked perfectly after I changed the recno field from character to numeric.

Thanks for the help all!

Fred
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top