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

Increment Character

Status
Not open for further replies.

WMXTeam

Programmer
Oct 5, 2005
41
US
I am working on Foxpro 2.6 application, which I have not worked in for a while. There is a field that contains a character and then two numbers; i.e. A01. I need to write a procedure that will auto-increment this number; i.e. A02, A03, A04...A99, B01, B02.

I am hoping there is a easy way to increment from A to B, but could use some assistance.

Thanks for your help.
 
One way to increment the letter is:

letter1 = 'A'
letter2 = CHR(ASC(letter1)+1)
? letter1
? letter2

The only letter you got to be careful with is 'Z' because if you increment it you get '[' (left bracket).

As far as incrementing the number/letter combinations that you give, you should be able to do that in in 2 or 3 lines of code.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
You could use a function like this:

[tt]
FUNCTION autoinc
PARAMETERS oldnum
PRIVATE z

z=ALLTRIM(STR(ASC(oldnum)*100+VAL(RIGHT(oldnum,2))+1))

RETURN CHR(VAL(LEFT(z,2)))+SUBSTR(z,3)
[/tt]

This function will automatically increment the complete number ('A01', 'A02', etc) to the next valid number, including incrementing the letter as needed. It will not work for 'Z99' nor any number not containing EXACTLY two numeric digits after a SINGLE letter.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
Hmmmm. Guess I should have also included an example.

[tt]
oldnum = 'F99'
newnum = AUTOINC(oldnum)
? oldnum
[COLOR=black yellow]F99[/color]
? newnum
[COLOR=black yellow]G00[/color]
[/tt]

Note also that if you need to skip numbers ending in '00' that you need to test for that and increment a second time to get correct number like below:

[tt]
oldnum = 'F99'
newnum = AUTOINC(oldnum)
newnum = IIF(ASC(newnum) = ASC(oldnum), newnum, AUTOINC(newnum))
? oldnum
[COLOR=black yellow]F99[/color]
? newnum
[COLOR=black yellow]G01[/color]
[/tt]


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
Thank you for your help. I used your suggestions and it worked perfectly.
 
mmerlinn:

your solution is very impressive. i have not seen this clear thinking. you may like to write 'tips and tricks'

best wishes

nasib kalsi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top