-
July 30th, 2010, 05:05 AM
#1
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.
-
July 30th, 2010, 01:15 PM
#2
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.
-
July 30th, 2010, 10:51 PM
#3
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?
-
July 31st, 2010, 12:16 PM
#4
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
-
August 1st, 2010, 10:31 PM
#5
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.
-
August 2nd, 2010, 06:26 AM
#6
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.
-
August 2nd, 2010, 11:16 AM
#7
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.
-
August 3rd, 2010, 01:00 AM
#8
Re: Issue of Updating Serial Number in Database Table
Originally Posted by DataMiser
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.
-
August 3rd, 2010, 01:48 AM
#9
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.
-
August 3rd, 2010, 01:48 AM
#10
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
-
August 8th, 2010, 10:11 PM
#11
Re: Issue of Updating Serial Number in Database Table
Originally Posted by dglienna
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|