Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is this a correct logic for AutoNumber ??
Message
 
 
To
07/09/2001 11:32:58
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00553829
Message ID:
00553841
Views:
12
>hello all
>
>My app generates a autonumber in a form,i have this code in Savedata method of a form,let me know will this work fine in a multiuser environment and i generate the autonumber only when the user clicks save is this the correct method ??
>
>Txs
>
>
>cmdSave Click Method
>---------------------
>
>BEGIN TRANSACTION
>Thisform.nAutoNumber.Value=autonumber()
>
>SELECT BANKACCT
>M_BALANCE=BANKACCT.BALANCE
>
>SELECT LOAN
>llloan=TABLEUPDATE(.T.)
>
>SELECT CHECK
>M_BALANCE = M_BALANCE - SC_AMOUNT
>APPEND BLANK
>REPLACE CK_NUM WITH SC_CHECK;
>BANK_ID WITH 789012
>llcheck=TABLEUPDATE(.T.)
>
>SELECT BANKACCT
>REPLACE BALANCE WITH M_BALANCE
>llbank=TABLEUPDATE(.T.)
>
>SELECT LOANREIM
>APPEND BLANK
>REPLACE CK_NUM WITH SC_CHECK
>llloanreim=TABLEUPDATE(.T.)
>
>IF llloan and llcheck and llbank and llloanreim = .t.
>	end transaction
>ELSE
>	messagebox('Another User modified the record,try later',64,'Network Conflict')
>	rollback
>ENDIF
>		
>Wait wind 'RECORD ADDED !!! ' timeout .5
>RETURN
>
>Stored Procedure Autonumber
>----------------------------
>FUNCTION Autonumber(tcAlias)
>  LOCAL lcAlias, ;
>  		lcID, ;
>        lcOldReprocess, ;
>        lnOldArea
>
>  lnOldArea = SELECT()
>
>  IF PARAMETERS() < 1
>    lcAlias = UPPER(ALIAS())
>  ELSE
>    lcAlias = UPPER(tcAlias)
>  ENDIF
>
>  lcOldReprocess = SET('REPROCESS')
>
>  *-- Lock until user presses Esc
>  SET REPROCESS TO AUTOMATIC
>
>  IF !USED("COUNTER")
>    USE dbcTest!counter IN 0
>  ENDIF
>
>  SELECT counter
>   IF RLOCK()
>      lcID = counter.nextcheckno
>      REPLACE counter.nextCheckNo WITH ;
>      ALLT(STR(VAL(ALLT(lcID)) + 1, LEN(counter.nextcheckno)))
>      UNLOCK
>    ENDIF
>  SELECT (lnOldArea)
>  SET REPROCESS TO lcOldReprocess
>  RETURN lcID
>  RELEASE lcID
>ENDFUNC
Few strange things, which caught my attention:
1) Why you're using character field for ID? Usually it's Integer field.

2) You pass parameter into your function, but you don't use it at all and you don't search for this alias in counter table (I assume, it contains more than one record). So, you're locking not the right record.

3) No command after RETURN statement would be executed!! And lcOldReprocess is actually lnOldReprocess, since it's a numeric value.

Just go to Craig Bernston's site and copy the function from there. Adjust the table name, if you like.

No need to re-invent the wheel.
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform