CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2010
    Posts
    50

    Exclamation Issue of Updating Serial Number in Database Table

    My entries are saved in a table having a field of Serial No alongwith other fields to keep them in sorted order. However when we reorder the record in MSFlexgrid/Listbox, what is the economical procedure adopted by professional to update that record order in the table.

    The example is given below:

    Sr. No., Saved Order, Changed Order

    1, A, A
    2, B, C
    3, C, B
    4, D, D
    5, E, E

    The letter C was placed previously at Sr. No.3. After shifting, it goes to Sr. No.2. My procedure is to store the previous index of letter “C” in a variable, open the table order by Sr. No. Move the record to InxdexVariable and change the value of field Sr. No. = IndexVariable -1 in case of shifting Up or Index Variable + 1 in case of Shifting Down. In a similar way I amend the second affecting record “B”.

    I would like also to know the procedure in case of removing a record and then updating the rest of Sr. Nos. in a table.
    Last edited by erncd1; July 30th, 2010 at 05:56 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Issue of Updating Serial Number in Database Table

    Loop from the END to the BEGINNING if you remove something. Doing it the wrong way will erase the record you're ABOUT to delete.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2010
    Posts
    50

    Re: Issue of Updating Serial Number in Database Table

    That is ok for removing an item and updating serial. But what about shifting the Record Serial?

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Issue of Updating Serial Number in Database Table

    Why shift the SR? Just delete it. Use an autonumber field, and it will be created automatically
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2010
    Posts
    50

    Re: Issue of Updating Serial Number in Database Table

    I think the use of an autonumber field won't be helpful if I am to insert a record in the middle. Insofar as the autoNumber field, that will insert the record at the last position. So that is why I am persisting with Manual Serial Number field.

    I didn't use autonumber field in my programme but these are my assumptions.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Issue of Updating Serial Number in Database Table

    When you insert a record into the database it goes to the end regardless of where it may appear on your display.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Issue of Updating Serial Number in Database Table

    I had a strange problem adding records to a collection. It seemed to add them in the MIDDLE, not matter how I stored the field. My controller was spitting out 1,3,2,4 or 3,1,2,4.

    I was using DateTime fields, and the solution turned out to be to always manually SORT the collection, and bind the sort to the grid.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jul 2010
    Posts
    50

    Re: Issue of Updating Serial Number in Database Table

    Quote Originally Posted by DataMiser View Post
    When you insert a record into the database it goes to the end regardless of where it may appear on your display.
    "Insert a record in the middle" was asked in the sense that when I will use autonumber field it will give last number to my newly added record, whereas I add the record in middle of MSFlexgrid/Listbox and want to give it relative Serial Number not Autonumber.

    In simple, It is the matter of Just Serial Number and not placing of Record into Database. When I add record with its Serial Number (say) "5", there is also another record having serial number 5, which needs to be increased "6" and so on.

    Hope this will further explain my problem.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Issue of Updating Serial Number in Database Table

    You NEVER want to change the serial number in a table, and definitely not when the DB decides to change it.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Apr 2009
    Posts
    394

    Re: Issue of Updating Serial Number in Database Table

    So then your table design needs to be something like...

    iID Autonumber used to keep records unique
    iSortID Number this is what we will be sorting on when we request records from this table
    vText Text/Varchar this is the data to be displayed.

    Then your process would be to...
    Code:
    sqlString = "UPDATE tablename SET [tablename.iSortID]=[tablename.iSortID]+1 WHERE iSortID >= 5"
    adoCn.Execute sqlString
    sqlString = "INSERT INTO tablename(SortID, vText) VALUES(5, 'Do Hicky Name')"
    adoCn.Execute sqlString
    Then of course, to pull the data out...
    Code:
    sqlString = "SELECT vText FROM tablename ORDER BY iSortID"


    Good Luck

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Issue of Updating Serial Number in Database Table

    Quote Originally Posted by dglienna View Post
    You NEVER want to change the serial number in a table, and definitely not when the DB decides to change it.
    Agreed, never change serial numbers and especially never insert one that would cause others to change. Serial number is normally a unique field used to identify an item quite often it is also barcoded on that item. If you change this at will then it is pretty much useless.
    Always use [code][/code] tags when posting code.

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

Featured