VB record insertion PLS HELP

Posted on 2011-05-07
Last Modified: 2012-05-11
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

Question by:AnnaJames77
    1 Comment
    LVL 32

    Accepted Solution

    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


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now