-
May 3rd, 2021, 11:34 AM
#1
Thread Starter
New Member
[RESOLVED] Auto incremented number doesn't change for all rows after being deleted from listview
I have one auto increment number in listview which is not connected to database as I have being adding sales items in it through a button and also 2 more buttons for editing and deleting. My problem is that when I delete a row the auto increment number does not change for all the remaining rows. Please suggest a solution. My code is in vb6.0.
Code:
If listview1.selecteditem is nothing then Exit Sub
Listview1. Listitems.Remove listview1. Selecteditem.Index
End sub
-
May 3rd, 2021, 12:34 PM
#2
Re: Auto incremented number doesn't change for all rows after being deleted from list
Um... what do you mean you "have one auto increment number in listview" ??? The LV doesn't have an autoincrement number feature (that I'm aware of) ... Maybe if you show the code you used to load it, it would be helpful to know why it "doesn't work." (which it probably does, but doesn't match your expectations.)
-tg
-
May 3rd, 2021, 02:54 PM
#3
Re: Auto incremented number doesn't change for all rows after being deleted from list
I would assume, perhaps incorrectly, that when you expect it to change, you are expecting that if you have the sequence 1, 2, 3, 4, 5, and delete record 3, that the 4 will renumber to 3 and the 5 will renumber to 4. Essentially, that would be expecting autonumber to "fill in" the hole created by a deletion. I've never heard of ANY autonumber that works that way, and would suggest that it would be a REALLY bad (and mostly pointless) thing for it to be doing. You wouldn't be able to use that autonumber as a key field anymore, and using it in any kind of relationship would simply fail as soon as a record was deleted. Since those are the two primary uses for such a field, breaking both of them at once would be a pretty bad thing to do. Furthermore, if autonumber did work that way, then it would be pointless, because the collection ordinal could be used in its place.
Of course, all of that is an assumption based on the fact that you seem to be expecting the auto increment number to change upon a deletion.
My usual boring signature: Nothing
-
May 3rd, 2021, 05:05 PM
#4
Re: Auto incremented number doesn't change for all rows after being deleted from list
Well like others I am not aware of any auto number feature for a listview. There is a listindex property which could be referenced and it's value placed in one of the columns which would look like an auto number but the text would not change when an item is removed unless the code looped through the list and placed the new values in there as would be the case with pretty much any method I can think of. The text of the other items in the listview will not auto-magically change when you add or remove an item. If you want it to change then you have to have the code there to change it.
-
May 3rd, 2021, 05:30 PM
#5
Re: Auto incremented number doesn't change for all rows after being deleted from list
If you are not using some sort of database, HOW ARE YOU keeping track of things? If you start your program, add some things to your listview, and then delete some (or add more) ("though the use of command buttons"), and you restart the program...guess what? What you are doing is making ZERO sense for any type of sales tracking program.
Sam I am (as well as Confused at times).
-
May 3rd, 2021, 05:55 PM
#6
Re: Auto incremented number doesn't change for all rows after being deleted from list
The ListView cannot work with numbers of any type, only text. Once you "kill and mount" the "head" of a numeric value all you have is that dead taxidermied head on your ListView wall.
-
May 4th, 2021, 06:46 AM
#7
Thread Starter
New Member
Re: Auto incremented number doesn't change for all rows after being deleted from list
Originally Posted by Hemant Visaria
I have one auto increment number in listview which is not connected to database as I have being adding sales items in it through a button and also 2 more buttons for editing and deleting. My problem is that when I delete a row the auto increment number does not change for all the remaining rows. Please suggest a solution. My code is in vb6.0.
Code:
If listview1.selecteditem is nothing then Exit Sub
Listview1. Listitems.Remove listview1. Selecteditem.Index
End sub
" I am sorry for as I couldn't make you understand my problem. I will try again briefly. I have one listview in a vb6 form with 4 command buttons and some text boxes and a label. 1st button adds records from textbox to listview and the code is
Code:
Private Sub command_click()
Dim lv as ListItem
Dim i as string
i= listview. Listitems.Count +1
Set lv=listview. Listitems.Add(, , i)
lv.subitems(1)=text2.text
lv.subitems(2)=text3.text
End sub
Private sub Listview 1_click()
index_lbl.Caption = Listview 1.selectedItem.Index
Text5.Text = Listview 1.selectedItemText
Text2.Text = Listview 1.selectedItem.SubItems(1)
Text3.Text = Listview 1.selectedItem.SubItems(2)
End Sub
2nd button is for editing the records which are in listview by selecting the listview items
Code:
Private Sub command 2_click()
With listview 1
.listitems(Val(index_lbl.Caption)).Text = Text5.text
.listitems(Val(index_lbl.Caption)).Subitems(1)= Text2.text
.listitems(Val(index_lbl.Caption)).Subitems(2)= Text3.text
End With
End sub
3rd button is to delete a selected row
Code:
Private sub Listview 3_click()
If listview1.selecteditem is nothing then Exit Sub
Listview1. Listitems.Remove listview1. Selecteditem.Index
End sub
And the 4th button to save all records to a access database
Everything is working fine except the 3rd button
Example
No. Product name qty
1 Apple 3
2 Mango 5
3 Pineapple 1
Now suppose I click on 2nd row in listview and I press my 3rd button to delete this row then it gets deleted but the number column is still 3 and so on for further rows. So I would like that the numbers get changed.
I think it would work with loops.
I have tried best to explain but still if you are not clear I will try again .Please suggest your solutions
Thanks 😊
Last edited by Shaggy Hiker; May 4th, 2021 at 09:41 AM.
-
May 4th, 2021, 09:48 AM
#8
Re: Auto incremented number doesn't change for all rows after being deleted from list
That's not an autonumber. You're just adding the item count to each row. The "auto" part of autonumber would mean that it happens automatically. You aren't doing automatic anything, it's manual. Therefore, if you want the numbers to change, YOU have to change it manually, just as you set it manually. Also, there is no autonumber that would do what you want, because that's not the way they work.
However, I would say that you shouldn't even have that column in the listview. What good does it do you? You don't need it for outputs, because the number is just the row number, so you could output the row number rather than trying to store that in a column. It wouldn't be a good idea to renumber if you are storing to a file or database. So, the only possible use for this that I can see is if you have some reason why you'd want to see that column in a display on a form, and that doesn't seem all that useful. Are you really sure that column needs to be there?
My usual boring signature: Nothing
-
May 4th, 2021, 10:00 AM
#9
Re: Auto incremented number doesn't change for all rows after being deleted from list
Smells like a row number as you'd have in a spreadsheet, just gingerbread unless you also have range-based cell expressions or something.
-
May 4th, 2021, 01:12 PM
#10
Re: Auto incremented number doesn't change for all rows after being deleted from list
You are adding a number to that column based on the current count. It will never change unless you change it.
You would have to loop through each item after the one you remove and change that number to the new value to get the result you want.
Listindex would make more sense than a count of the items but still no matter how you do it you will have to write code to update the value of all the items which need to change.
-
May 5th, 2021, 04:46 AM
#11
Thread Starter
New Member
Re: Auto incremented number doesn't change for all rows after being deleted from list
Hi thanks to all of you for replying. An expert programmer has solved my problem. I would like to share the code .When I click my 3rd button that is to delete a selected row I applied this code.
[Code]
Code:
Private sub Listview 3_click()
If listview1.selecteditem is nothing then Exit Sub
Listview1. Listitems.Remove listview1. Selecteditem.Index
Dim i as Integer
i=i+1
While Not i = Listview 1.listitems.Count +1
Listview2.listitems(i).Text =i
i=i+1
DoEvents
Wend
End sub
-
May 5th, 2021, 04:51 AM
#12
Re: Auto incremented number doesn't change for all rows after being deleted from list
So like the others said, you only want a kind of row number like in Excel.
Code:
Private sub Listview 3_click()
Dim i As Long
If Listview1.SelectedItem is nothing then Exit Sub
Listview1.Listitems.Remove Listview1.Selecteditem.Index
For i = 1 To Listview1.ListItems.Count
Listview1.ListItems(i).Text = CStr(i)
Next i
End sub
-
May 5th, 2021, 04:52 AM
#13
Re: Auto incremented number doesn't change for all rows after being deleted from list
Hi thanks to all of you for replying. An expert programmer has solved my problem.
in this whole thread you had expert's answering, shame you didn't take the time to try and understand
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 5th, 2021, 08:02 AM
#14
Re: Auto incremented number doesn't change for all rows after being deleted from list
An expert programmer would not have used a doevents. That is a bit sloppy as is the while wend.
The For Next like shown in post #12 is what most good programmers would use. Faster, more efficient and less typing.
-
May 7th, 2021, 07:22 AM
#15
Thread Starter
New Member
Re: Auto incremented number doesn't change for all rows after being deleted from list
I am extremely sorry to all please forgive me .
It was all my fault as I couldn't explained it properly so please forgive me and also all of you experts tried to explain me but I couldn't get it so please forgive me. A big SOORY to all you experts 🙏🙏😞.
As I am new to this ....please .
-
May 7th, 2021, 07:27 AM
#16
Thread Starter
New Member
Re: Auto incremented number doesn't change for all rows after being deleted from list
-
May 7th, 2021, 07:28 AM
#17
Thread Starter
New Member
Re: Auto incremented number doesn't change for all rows after being deleted from list
-
May 7th, 2021, 07:32 AM
#18
Re: Auto incremented number doesn't change for all rows after being deleted from list
No need to apologize!
We all were beginners at some point.
-
May 7th, 2021, 01:39 PM
#19
Re: Auto incremented number doesn't change for all rows after being deleted from list
Yep, when I look back at some of my early code I cringe more than a little
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
|