VB record insertion PLS HELP

In my vb application, it uses a table header which has 5 columns

month   slno   PO      COL   col_sl_no
1            1     nnn      1         1
1            2    aop       2         1
1            3     afg       2         2
1            4     abd      1         2  
2            1    qqq       3         1
2            2    eee       1         1
2            3    ddd       4         1
2            4    ccc       3         2

right now the appln is used by a single user.  If the user selects a month, it finds the last SL_NO of the month.
 There are say, 4 COL. If the users enters a PO in the textbox and clicks enter,the application finds out to which col the PO entered by the user should be added to.(will be any of 1 to 4). And it updates in this table.  
Say if user, selects a month 1, and enters a PO, nnn, then the appln finds which COL it belongs to,say if its 1, then it finds out the last entered col_ser_no of the COL and of the month 1 from the db and enters the new PO to this db adding 1 to last col_ser_no and 1 to last SL_NO of the month. Say, row will be 1  1  nnn 1  1. Next if the user enters another PO and it belongs to same COL then finds the last COL_SER_NO,adds 1 and inserts new rec to db as 1  2  ddd  1  '2' .  

Next if the user selects another month, it finds the last SL_NO of the month and does the same above process.
This appln runs fine if a single user runs it. Now this app need to be used my multiple user. if two users try to get the last SL_NO and also the COL_SL_NO at the same time it may get duplicated.  i donot want for the same month,the SL_NO be duplicated. As well as for the same week,for the same COL,the COL_SER_NO shouldn't be duplicated.

I tried creating  two constraints like month+ SL_No and 2nd constraint as MONTH+COL+COL_SER_No. the apln throws an error but

Pls let me know how should i handle such situation like if multiple user hits the button at the very same time. i donot want to throw the constraint error to the user. is there any other way

Who is Participating?
Robberbaron (robr)Connect With a Mentor Commented:
the only way i can see is to have the ap trap the constraint error from the database and then do a retry on the determination of the SL_NO.  so the first person to click the button (they wont be at exactly the same time. 1millisec is enough) will get assigned the first number and the second person will go through the retry.
ok = false : retries = 0
do while not ok and retries < 10
   on error resume next
   err.clear: ok=true
   calc the SL_NO
   try to insert
   if error then ok = false

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.