|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ListView Duplicate Items!Captions of the 2 CommandButtons are 'Yes' & 'No'. The ListView has 2 Columns. Suppose I enter the text 'Computer' (without the quotes) in the TextBox & then click the 'Yes' CommandButton. When I do so, the 1st column of the ListView gets populated with 'Computer' (again without the quotes) & the corresponding 2nd column gets populated with 'Yes'. Now suppose I again enter 'Computer' in the TextBox & now click the 'No' CommandButton. Since the item 'Computer' already exists in the ListView, I want that instead of 'Computer' again getting added to the ListView as a new item, the 1st column of the existing item should remain 'Computer' & only the 2nd column value should change to 'No'. How would I do this? Thanks, Arpan What have you tried?
-- Show quoteHide quoteChris Hanscom - Microsoft MVP (VB) Veign's Resource Center http://www.veign.com/vrc_main.asp Veign's Blog http://www.veign.com/blog -- "Arpan" <arpan***@hotmail.com> wrote in message news:1131755634.923175.295100@g44g2000cwa.googlegroups.com... >A Form has a TextBox, a ListView & 2 CommandButtons. Assume that the > Captions of the 2 CommandButtons are 'Yes' & 'No'. The ListView has 2 > Columns. > > Suppose I enter the text 'Computer' (without the quotes) in the TextBox > & then click the 'Yes' CommandButton. When I do so, the 1st column of > the ListView gets populated with 'Computer' (again without the quotes) > & the corresponding 2nd column gets populated with 'Yes'. > > Now suppose I again enter 'Computer' in the TextBox & now click the > 'No' CommandButton. Since the item 'Computer' already exists in the > ListView, I want that instead of 'Computer' again getting added to the > ListView as a new item, the 1st column of the existing item should > remain 'Computer' & only the 2nd column value should change to 'No'. > How would I do this? > > Thanks, > > Arpan >
Show quote
Hide quote
"Arpan" <arpan***@hotmail.com> wrote in message So before inserting a new item just loop through all existing ones and news:1131755634.923175.295100@g44g2000cwa.googlegroups.com... >A Form has a TextBox, a ListView & 2 CommandButtons. Assume that the > Captions of the 2 CommandButtons are 'Yes' & 'No'. The ListView has 2 > Columns. > > Suppose I enter the text 'Computer' (without the quotes) in the TextBox > & then click the 'Yes' CommandButton. When I do so, the 1st column of > the ListView gets populated with 'Computer' (again without the quotes) > & the corresponding 2nd column gets populated with 'Yes'. > > Now suppose I again enter 'Computer' in the TextBox & now click the > 'No' CommandButton. Since the item 'Computer' already exists in the > ListView, I want that instead of 'Computer' again getting added to the > ListView as a new item, the 1st column of the existing item should > remain 'Computer' & only the 2nd column value should change to 'No'. > How would I do this? > > Thanks, > > Arpan > compare with new text. If matching item is found then change the second column, otherwise insert new item. There is second approach. Since you want content of the first column to be unique, you can use it as a Key property of each item. Each time before inserting an item try to retrieve existing one by key. If error then there is no such item - insert new one and assign inserted text as a key; if found - just change the second column. The second solution is riskier in that that not any text is good to be used as a key. I think, it will not accept numeric values or those starting with number. And I can guess that there can be problems with texts containing some non-letter characters like punctuation signs, apostrophes, quotas and others. So if choose this approach - make sure that you find out all such limitations and validate user input. Dmitriy. Yeah...I made use of the Key property wherein the Key of each item will
be the item itself (i.e. the text that was entered in the TextBox). For e.g. for the item 'Computer', the Key will be 'Computer' or for an item, say, 'Monitor', the Key will be 'Monitor'. But the problem is if a duplicate item exists, VB just generates an error. For the time being, I am using On Error Resume Next to bypass the error but I don't know how to trap this error & use it in an If......Else condition. Any idea on how to go about it? Thanks, Arpan
Show quote
Hide quote
"Arpan" <arpan***@hotmail.com> wrote in message Wait a second. You are telling that the rule should be: if an item with such news:1131760493.864139.117480@g49g2000cwa.googlegroups.com... > Yeah...I made use of the Key property wherein the Key of each item will > be the item itself (i.e. the text that was entered in the TextBox). > For e.g. for the item 'Computer', the Key will be 'Computer' or for an > item, say, 'Monitor', the Key will be 'Monitor'. But the problem is if > a duplicate item exists, VB just generates an error. For the time > being, I am using On Error Resume Next to bypass the error but I don't > know how to trap this error & use it in an If......Else condition. Any > idea on how to go about it? > > Thanks, > > Arpan > text already exists, then it is not added - only the second column is updated. Having this rule it is ABSOLUTELY IMPOSSIBLE to have duplicate keys if you use item's text (which is unique according to mentioned rule) as a key. The only way of having problem here is if in addition to populating items using described way of using those Textbox and CommandButtons, there is some other source for items (e.g. you initially populate them from DB and they are not unique there). If this is the case, then you should solution #1 from my previous post. Error handling is not good option anyway in this case. As for error handling in If...Else statement, you have multiple options. Assuming that we are talking about multiline if...else statement, one of the options is to check Err.Number after the first If branch and then act accordingly (means it depends on the logic of your application) Dmitriy 'USAGE
NOTE!!! Code not tested Call AddNewItem("YourNewItemName", "ValueIfAddingToList", ValueIfItemExists) Sub AddNewItemToLV(sItem, ValToAddAsNew, ValIfItemExists) Dim itmX as listItem For each itmx in lvwX 'Replace lvwX with your listViewName If itmx = sItem then 'Means the new Item exists in listview 'Then update the second column and exit itmx.subItems(1) = ValIfItemExists Exit sub ENd if Next 'If code gets here means new item is not 'in the list... so add it to listView Set itmx = lvwX.listitems.add itmX = sItem ItmX.subItems(1) = ValToAddAsNew set itmx = nothing End Sub Show quoteHide quote "Dmitriy Antonov" <antonovdima@netzero.net_NOT_FOR_SPAM> wrote in message news:OffGB9y5FHA.4012@TK2MSFTNGP14.phx.gbl... > > "Arpan" <arpan***@hotmail.com> wrote in message > news:1131760493.864139.117480@g49g2000cwa.googlegroups.com... > > Yeah...I made use of the Key property wherein the Key of each item will > > be the item itself (i.e. the text that was entered in the TextBox). > > For e.g. for the item 'Computer', the Key will be 'Computer' or for an > > item, say, 'Monitor', the Key will be 'Monitor'. But the problem is if > > a duplicate item exists, VB just generates an error. For the time > > being, I am using On Error Resume Next to bypass the error but I don't > > know how to trap this error & use it in an If......Else condition. Any > > idea on how to go about it? > > > > Thanks, > > > > Arpan > > > > Wait a second. You are telling that the rule should be: if an item with such > text already exists, then it is not added - only the second column is > updated. > > Having this rule it is ABSOLUTELY IMPOSSIBLE to have duplicate keys if you > use item's text (which is unique according to mentioned rule) as a key. > > The only way of having problem here is if in addition to populating items > using described way of using those Textbox and CommandButtons, there is some > other source for items (e.g. you initially populate them from DB and they > are not unique there). If this is the case, then you should solution #1 from > my previous post. Error handling is not good option anyway in this case. > > As for error handling in If...Else statement, you have multiple options. > Assuming that we are talking about multiline if...else statement, one of the > options is to check Err.Number after the first If branch and then act > accordingly (means it depends on the logic of your application) > > Dmitriy > > |
|||||||||||||||||||||||