Results 1 to 4 of 4

Thread: [RESOLVED] SQL (when an ID is deleted, the following ID will -1 to take the new position.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Resolved [RESOLVED] SQL (when an ID is deleted, the following ID will -1 to take the new position.

    i have a ss_table which consist of sID, qnsID, ansID.

    the values of sID are s0001, s0002, s0003 and so on.

    If i were to remove s0002, i would like s0003 to shift up and set as s0002.

    can someone show me how i can do this in sQL

    my approach is rst.Open "SQL", cn, adOpenDynamic, adLockOptimistic


    note: sID is set as 'Text' in Access table but autonumber-ed by SQL
    rst!sID = "ss" & Right("0000" & CStr(CInt(rst1!ansID) + 1), 4)

    Thank You

    Astro

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: SQL (when an ID is deleted, the following ID will -1 to take the new position.

    Run this query right after you delete the record:


    VB Code:
    1. UPDATE ss_table
    2. SET ss_table.sID = Left([sID],1) & Format(CLng(Mid([sID],2))-1,"0000")
    3. WHERE (((ss_table.sID)>"s0002"));
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: SQL (when an ID is deleted, the following ID will -1 to take the new position.

    Hey Mark,

    Brilliant! =) Pardon me for not having to mention that ss0002 will be removed when the referenced ansID (ans0001, ans0002, ans0003) is double clicked on.

    rst1.Open "UPDATE ss_table SET ss_table.sID = Left([sID],1) & Format(CLng(Mid([sID],2))-1,'000000') WHERE '" & List3.Column(0, List3.ListIndex) & "' < ss_table.ansID", cn, adOpenDynamic, adLockOptimistic

    How does this looks? note: i've changed 0000 to 000000.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: SQL (when an ID is deleted, the following ID will -1 to take the new position.

    Hey Mark,

    I got it. Well actually i am asked to drop the idea. Great help anyway!

    Thanks

    Astro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width