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

Is there a better way

Status
Not open for further replies.

jjgraf

Programmer
Aug 19, 2001
237
US
I need to generate a serial for product that is alpha numeric. I send this function the Last Used Serial Number and have it increment it. I was wonder if anybody can come up with a more readable one

thanks
****************************************************
function tserial
LPARAMETERS lcSerial
LOCAL lnASCII
lnASCII = ASC(SUBSTRC(lcSerial,1,1))

DO CASE
CASE lnASCII = 32
return STRTRAN(lcSerial, CHR(lnASCII),CHR(48),1,1,1)
CASE lnASCII < 57
return STRTRAN(lcSerial, CHR(lnASCII), CHR(lnASCII+1), 1,1,1)
CASE lnASCII = 57
return STRTRAN(lcSerial, CHR(lnASCII), CHR(lnASCII+8), 1,1,1)
CASE lnASCII < 90
return STRTRAN(lcSerial, CHR(lnASCII), CHR(lnASCII+1), 1,1,1)
CASE lnASCII = 90
return (CHR(48) + tserial(SUBSTRC(lcSerial,2)))
ENDCASE

endfunc

*****************************************************
 
HI

*********************************************************
** Author ... Subramanian.G
**
** Function to increment a number to an alphanumeric
** field and return its value.
**
** oldId sent here should be the full field value
** without any TRIM, since the length of the field
** is computed using the passed parameter.

** How to do..
** Copy the below procedure as myNewId to your form
** Whenever you want the next number..
** GO BOTTOM.. read the last value..
** myNextId = ThisForm.myNewId(myLastFieldValue)
****************************************************
PARAMETERS oldId

LOCAL cId, nNumber, nLen, nMaxLen, newId, I

cId = ALLTRIM(oldId)
nMaxLen = LEN(oldId)
nLen = LEN(cId)
nNumber = 1
I=1
IF nLen > 0
nMaxLen = nlen
ENDIF
newId = &quot;&quot;

FOR I = nLen TO 1 STEP -1
IF ISDIGIT(SUBSTR(cId,I,1))
nNumber = VAL(SUBSTR(cId,I))+1
ELSE
EXIT
ENDIF
ENDFOR

nMaxLen = nMaxLen - I
nNumber = RIGHT(REPLICATE(&quot;0&quot;,nMaxLen) + ALLTRIM(STR(nNumber)),nMaxLen)
NewId = LEFT(cId,I)+nNumber

RETURN NewId
*********************************************************
** EOF
** No implied warranties applicable
*********************************************************
Hope this helps you :)
ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
I looked over the example it appears to me it is not alphanumeric example A000 the next value would be B000 then after Z000 would be 0100 then 1100 and so on the last serial number of 4 characters would be ZZZZ which would allow 1,679,616 possible combinations
 
jjgraf:

That's a strange set of logic:
DO CASE
CASE lnASCII = 32
return STRTRAN(lcSerial, CHR(lnASCII),CHR(48),1,1,1)
CASE lnASCII < 57
return STRTRAN(lcSerial, CHR(lnASCII), CHR(lnASCII+1), 1,1,1)
CASE lnASCII = 57
return STRTRAN(lcSerial, CHR(lnASCII), CHR(lnASCII+8), 1,1,1)
CASE lnASCII < 90
return STRTRAN(lcSerial, CHR(lnASCII), CHR(lnASCII+1), 1,1,1)
CASE lnASCII = 90
return (CHR(48) + tserial(SUBSTRC(lcSerial,2)))
ENDCASE

What are you trying to accomplish here?

Are you assuming there can be some bad data in a serial field?

Anyway...

By digesting your post, I assumed at first your alphnumeric field is in the following format: A1119, but after re-reading and partially reviewing the above referenced logic I am not sure.

If you just use a simple field structure like A1110, the following should suffice:

* Bump alpha serial number
Function BumpAlpha(lcAlphaSerial)

local lcSaveAlpha, lcNumeric

lcSaveAplha = left(lcAlpaNumeric,1)
lnNumeric = substr(lcAlplaNumeric,2)

if !empty(lcNumeric)
lcNumeric = int(val(lcNumeric)) + 1
else
lcNumeric = 1
endif

return lcSaveAlpha + alltr(str(lcNumeric))
endfunc

Note: I didn't include any test to insure that there are no alpha chars in the lnNumeric subfield portion. If for some reason the data gets munged, BumpAlhpa() will throw an exception.

Darrell
 
no checking for bad data at all

each position of the 4 character field can contain a letter or a number.

What I’m asking is there better way to scan through a string and increment it to the next possible combination

example
A100
A233
AAAA
ZABC

The reason it looks funky and strange is the fact on the ASCII chart number and letters are separated by a large margin.
ASCII 32 = blank Spaces
48 through 57 are 0 - 9
65 through 90 are capital letters A-Z

what i do is send it the last serial number given out

LPARAMETERS lcSerial
LOCAL lnASCII
lnASCII = ASC(SUBSTRC(lcSerial,1,1))

DO CASE
CASE lnASCII = 32 && This is here just incase the serial number has white spaces in it, i then replace it with Zero
return STRTRAN(lcSerial, CHR(lnASCII),CHR(48),1,1,1)

CASE lnASCII < 57 && if it is less then 57 which puts between 0-8 on the ASCII chart just replace that char with the next number in the list
return STRTRAN(lcSerial, CHR(lnASCII), CHR(lnASCII+1), 1,1,1)

CASE lnASCII = 57 && if it is equal to 9 we need to rplace it with the letter A
return STRTRAN(lcSerial, CHR(lnASCII), CHR(lnASCII+8), 1,1,1) && which by the way is 8 places up on the ASCII chart

CASE lnASCII < 90 && if it is less then 90 on the ASCII chart we know we are in the Alaphbet so just replace it with the next letter
return STRTRAN(lcSerial, CHR(lnASCII), CHR(lnASCII+1), 1,1,1)

CASE lnASCII = 90 && if we are equal to 90 which is the letter Z we need to reset this character to 0 and then recall this function so we can increment the next letter in the Serial number
return (CHR(48) + tserial(SUBSTRC(lcSerial,2))) && this strips off the first character and then sends it to the tserial function so it can increment the next character
ENDCASE

I’m a bit worry you see because once I’m gone and somebody else takes over supporting my code they need to be able to understand it and looking at this code it not to easy.
 
temp = 'A111'
temp = stuff(temp,len(temp),1,chr(asc(right(temp,1))+1))

this will produce 'A112'

you will need to code for the max value of the replacement if max is reached then shift to the next position. right(temp,2)..... Attitude is Everything
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top