Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Avoid repeated values with more than two users
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01322172
Message ID:
01322175
Views:
13
>I want to make sure code generation is not repeating codes when using two instaces of the same form
>
>ped_test.dbf is buffered as 5
>
>Then I runned form 1 and form 2 at the same time with
>2 differents instaces of VFP, as below:
>
>
>***********************form 1 code**************************
>SELECT PED_TEST
>DO WHILE PED_TEST.COD < 100000
>
>
>dimension lctemp[1]
>select max(PED_TEST.cod) from PED_TEST ;
> into array lctemp
> lcTemp[1] = nvl(lcTemp[1],0)+1
>
> APPEND BLANK
>
>replace PED_TEST.COD with lctemp
>replace ped_test.id WITH "FORM 1"
> = TABLEUPDATE ()
> LOOP
> ENDDO
>
>
>***********************form 2 code**************************
>
>
>SELECT PED_TEST
>DO WHILE PED_TEST.COD < 100000
>
>
>dimension lctemp[1]
>select max(PED_TEST.cod) from PED_TEST ;
> into array lctemp
> lcTemp[1] = nvl(lcTemp[1],0)+1
>
> APPEND BLANK
>
>replace PED_TEST.COD with lctemp
>replace ped_test.id WITH "FORM 2"
> = TABLEUPDATE ()
> LOOP
> ENDDO
>**********************************************
>
>At the end of the test I have repeated values
>if I browse my two fields table I have something as:
>
>------------------------
>cod ---ID
>------------------------
>
>1000 - FORM 1
>1000 - FORM 2
>1001 - FORM 1
>1001 - FORM 2
>1002 - FORM 1
>1002 - FORM 2 , ETC
>
>
>I would like something as:
>
>
>1000 - FORM 1
>1001 - FORM 2
>1002 - FORM 1
>1003 - FORM 2
>1004 - FORM 1
>1005 - FORM 2 , ETC
>
>Can anybody imagine a better code to run my tests ?

Add AUTOINC field to your table and make it Primary key.
Other way is to have an additional table where you should store last key used and before insert in that table LOCK the table from where you get the keys and AFTER you commit all changes then unlock it. Just keep in mind that you should not lock that table when you enter the form and release it after you close the form :-) Only there where you APPEND and commit records:
CREATE TABLE NewKey FREE (KeyFld int)
APPEND BLANK

***Form 1:
USE NewKey IN 0
IF NOT FLOCK([NewKey])
   MessageBox([Somebody tries to add records right now, please try after a few  seconds])
   RETURN
ENDIF
lnNewKey = NewKey.KeyFld+1
SELECT PED_TEST
DO WHILE  PED_TEST.COD < 100000
   dimension lctemp[1]
   APPEND BLANK
   replace COD         with lnNewKey,;
           ped_test.id WITH "FORM 1"
   lnNewKey = lnNewKey + 1
ENDDO
IF NOT TABLEUPDATE(1, .t., [PED_TEST])
   TABLEREVERT(.t., [PED_TEST])
ELSE
   REPLACE NewKey WITH lnNewKey + 1 IN NewKey
ENDIF

***Form 2:
USE NewKey IN 0
IF NOT FLOCK([NewKey])
   MessageBox([Somebody tries to add records right now, please try after a few  seconds])
   RETURN
ENDIF
lnNewKey = NewKey.KeyFld+1
SELECT PED_TEST
DO WHILE  PED_TEST.COD < 100000
   dimension lctemp[1]
   APPEND BLANK
   replace COD         with lnNewKey,;
           ped_test.id WITH "FORM 2"
   lnNewKey = lnNewKey + 1
ENDDO
IF NOT TABLEUPDATE(1, .t., [PED_TEST])
   TABLEREVERT(.t., [PED_TEST])
ELSE
   REPLACE NewKey WITH lnNewKey + 1 IN NewKey
ENDIF
That is code just to get idea, nbot error handling included and not 100% bullet proof :-)

But I still prefer AUTOINC fields. They could be PIA sometimes, but for such purposes are great.
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform