-
Aug 21st, 2017, 01:12 PM
#1
Thread Starter
Enjoy the moment
[RESOLVED] trying to check if a value exists from another form something is Wrong!!
hey,
i have 2 listviews
the first one contains products that i have in the list.
the second is to add a new product.
when i add a new product and i want to check if its exsits in another listview
dosnt seem to work.
i need to check between 2 listviews to see if the same product exists
before i add it to the list.
the 2 listviews are in diffrent forms
anyway im stuck
this is my code
Code:
Dim LX As Integer
Dim IsCheked As Boolean
Dim I As Integer
For I = 1 To LTreat.ListItems.Count
If LTreat.ListItems(I).Checked Then
IsCheked = True
For LX = 1 To Frmnewproduct.LsVw.ListItems.Count
If LTreat.ListItems(I).Text = Frmnewproduct.LsVw.ListItems(LX).Text Then
MsgBox "the product is already added to the list", vbInformation
Exit Sub
End If
Next
Set itm = Frmnewproduct.LsVw.ListItems.Add(, , (LTreat.ListItems(I).Text))
itm.SubItems(1) = LTreat.ListItems(I).SubItems(1)
itm.SubItems(2) = LTreat.ListItems(I).SubItems(2)
itm.SubItems(3) = LTreat.ListItems(I).SubItems(3)
itm.SubItems(4) = LTreat.ListItems(I).SubItems(4)
itm.SubItems(5) = LTreat.ListItems(I).SubItems(5)
itm.SubItems(6) = LTreat.ListItems(I).SubItems(6)
End If
Next
If IsCheked = False Then
MsgBox "no product has been chosen", vbInformation
Else
Unload Me
End If
tnx for any help
salsa
-
Aug 21st, 2017, 01:41 PM
#2
Re: trying to check if a value exists from another form something is Wrong!!
If you are testing just the Text property of the listviews' listitems, use FindItem and no need for looping:
Code:
...
If LTreat.ListItems(I).Checked Then
IsCheked = True
If Not Framewproduct.LsVw.FindItem(LTreat.ListItems(I).Text, lvwText, 1) Is Nothing Then ' found
MsgBox "the product is already added to the list", vbInformation
Exit Sub
End If
... rest of your code
Edited: One of the reasons why your version didn't work, may be because you are exiting the Sub if any of the checked items are already loaded. I don't think you want to exit the Sub, but want to skip adding that item?
Last edited by LaVolpe; Aug 21st, 2017 at 01:47 PM.
-
Aug 21st, 2017, 01:48 PM
#3
Thread Starter
Enjoy the moment
Re: trying to check if a value exists from another form something is Wrong!!
hey sir
i got a little confused
how do i combine it with my code?
-
Aug 21st, 2017, 01:50 PM
#4
Re: trying to check if a value exists from another form something is Wrong!!
You don't have to. You can loop thru the listview items if you want. But read my edited comments in my previous reply. Is that the problem? You are not adding the items that should be added? If so, my edited comments explain why.
-
Aug 21st, 2017, 01:52 PM
#5
Thread Starter
Enjoy the moment
Re: trying to check if a value exists from another form something is Wrong!!
i think i do
beacuse if its not the same item then i need to loop to add the values to the other listview
-
Aug 21st, 2017, 01:58 PM
#6
Thread Starter
Enjoy the moment
Re: trying to check if a value exists from another form something is Wrong!!
i read the edited comments
this is the scenario
the listview has checkboxes
first i need to check if is there any checked items in the list
then i need to check if any of the checked items has the same name as the other listview
if it is the same then msgbox displayed and nothing is happening
else
i add to the other listview the selected checkboxes in the listview
-
Aug 21st, 2017, 02:00 PM
#7
Banned
Re: trying to check if a value exists from another form something is Wrong!!
the reason its not working because your working with listview , now look at your code here.
Dim LX As Integer
Dim IsCheked As Boolean
Dim I As Integer
For I = 1 To LTreat.ListItems.Count
If LTreat.ListItems(I).Checked Then
IsCheked = True
For LX = 1 To Frmnewproduct.LsVw.ListItems.Count
If LTreat.ListItems(I).Text = Frmnewproduct.LsVw.ListItems(LX).Text Then
MsgBox "the product is already added to the list", vbInformation
Exit Sub
End If
Next
If LTreat.ListItems(I).Text = Frmnewproduct.LsVw.ListItems(LX).Text Then
this is only checking the 1st row only and not sub items so dont get mistaken.
so you need to se in listview1 were the value is if its root or sub , and also check listview2 if its root or sub.
if its root then its If LTreat.ListItems(I).Text
if its subitem then its If LTreat.ListItems(I).subitem somthing like that
-
Aug 21st, 2017, 02:00 PM
#8
Re: trying to check if a value exists from another form something is Wrong!!
Understand the scenario, but that is not what you coded. You are exiting the routine once you found an item in that 2nd listview that matches your checked item. After that item was found, no more items will be checked, no more items will be added becaue of the Exit Sub statement
-
Aug 21st, 2017, 02:04 PM
#9
Re: trying to check if a value exists from another form something is Wrong!!
salsa
I just briefly read the foregoing, but could it simply be a reference issue?
If you have forms and components with these names ..
.. Form1
.. ListView1 on Form1
.. Form2
.. ListView1 on Form2
Then this code in Form1 might get info about Form2's ListView1 component ..
Code:
z = Form2.ListView1.ListItems.Count
If you are on Form2 then this might get info about Form1's ListView1 component ..
Code:
z = Form1.ListView1.ListItems.Count
And, if you are in a BAS module, then
Code:
z1 = Form1.ListView1.ListItems.Count
z2 = Form2.ListView1.ListItems.Count
Spoo
Last edited by Spooman; Aug 21st, 2017 at 02:10 PM.
-
Aug 21st, 2017, 02:06 PM
#10
Thread Starter
Enjoy the moment
Re: trying to check if a value exists from another form something is Wrong!!
Originally Posted by LaVolpe
Understand the scenario, but that is not what you coded. You are exiting the routine once you found an item in that 2nd listview that matches your checked item. After that item was found, no more items will be checked, no more items will be added becaue of the Exit Sub statement
so i do i code it right?
-
Aug 21st, 2017, 02:06 PM
#11
Banned
Re: trying to check if a value exists from another form something is Wrong!!
here is example
Code:
Private Sub Command1_Click()
Dim LX As Integer
Dim IsCheked As Boolean
Dim I As Integer
For I = 1 To ListView1.ListItems.Count
For LX = 1 To ListView2.ListItems.Count
If ListView1.ListItems(I).Text = ListView2.ListItems(LX).Text Then
MsgBox "the product is already added to the list", vbInformation
End If
Next
Next
End Sub
Private Sub Form_Load()
Dim li As ListItem
With ListView1
Set li = .ListItems.Add(, , "List3.Text")
li.SubItems(1) = "aaaa"
li.SubItems(2) = "xx"
End With
With ListView2
Set li = .ListItems.Add(, , "List3.Text")
li.SubItems(1) = "aaaa"
li.SubItems(2) = "xx"
End With
End Sub
=========
Code:
Private Sub Command1_Click()
Dim LX As Integer
Dim IsCheked As Boolean
Dim I As Integer
For I = 1 To ListView1.ListItems.Count
For LX = 1 To ListView2.ListItems.Count
If ListView1.ListItems(I).Text = ListView2.ListItems(LX).Text Then
MsgBox ListView1.ListItems(I).Text & " " & "was found"
Exit Sub
End If
Next
Next
End Sub
Private Sub Form_Load()
Dim li As ListItem
With ListView1
Set li = .ListItems.Add(, , "List3.Text")
li.SubItems(1) = "aaaa"
li.SubItems(2) = "xx"
End With
With ListView2
Set li = .ListItems.Add(, , "List3.Text")
li.SubItems(1) = "aaaa"
li.SubItems(2) = "xx"
End With
End Sub
Last edited by gennna21; Aug 21st, 2017 at 02:11 PM.
-
Aug 21st, 2017, 02:11 PM
#12
Re: trying to check if a value exists from another form something is Wrong!!
Originally Posted by salsa31
so i do i code it right?
Using your code and just changing a couple things, I think this will work. Changed items are in Blue
Code:
Dim LX As Integer
Dim IsCheked As Boolean
Dim I As Integer
For I = 1 To LTreat.ListItems.Count
If LTreat.ListItems(I).Checked Then
IsCheked = True
For LX = 1 To Frmnewproduct.LsVw.ListItems.Count
If LTreat.ListItems(I).Text = Frmnewproduct.LsVw.ListItems(LX).Text Then
MsgBox "the product is already added to the list", vbInformation
Exit For
End If
Next
If LX > Frmnewproduct.LsVw.ListItems.Count Then
Set itm = Frmnewproduct.LsVw.ListItems.Add(, , (LTreat.ListItems(I).Text))
itm.SubItems(1) = LTreat.ListItems(I).SubItems(1)
itm.SubItems(2) = LTreat.ListItems(I).SubItems(2)
itm.SubItems(3) = LTreat.ListItems(I).SubItems(3)
itm.SubItems(4) = LTreat.ListItems(I).SubItems(4)
itm.SubItems(5) = LTreat.ListItems(I).SubItems(5)
itm.SubItems(6) = LTreat.ListItems(I).SubItems(6)
End If
End If
Next
If IsCheked = False Then
MsgBox "no product has been chosen", vbInformation
Else
Unload Me
End If
Now you may want to re-work that msgbox. If 10 items were already added to the list, 10 msgboxes -- ugh.
-
Aug 21st, 2017, 02:18 PM
#13
Thread Starter
Enjoy the moment
Re: trying to check if a value exists from another form something is Wrong!!
Now you may want to re-work that msgbox. If 10 items were already added to the list, 10 msgboxes -- ugh.
so how do i check the whole list before i add the items to the listview?
i dont want to add any item until is it good.
if there is no match with the items then i add it to the other listview
-
Aug 21st, 2017, 02:25 PM
#14
Re: trying to check if a value exists from another form something is Wrong!!
So, now just a bit confused.
1. If 10 items were checked, and 1 already existed in the other list, then none of the 10 items are added?
2. Same scenario above, but if just 1 already existed in the other list, then the other 9 that didn't exist are added?
The code posted in #12 above should support scenario 2.
-
Aug 21st, 2017, 02:29 PM
#15
Thread Starter
Enjoy the moment
Re: trying to check if a value exists from another form something is Wrong!!
If 10 items were checked, and 1 already existed in the other list, then none of the 10 items are added?
exactly sir
if there is a way to tell the user that a certain item in the list is already exists then it will be perfect
-
Aug 21st, 2017, 02:37 PM
#16
Re: trying to check if a value exists from another form something is Wrong!!
If I was a user, I would be really annoyed with that logic. Here is why.
1. I checked 10 items and clicked "add" button
2. If 2 items were already added to the other list, I wouldn't want to be told: "Do it again after you uncheck the duplicate ones"
3. I would want the logic to silently ignore the duplicate items and add the ones that are not duplicates
But maybe that's me. You could separately count how many duplicates and if there were any, alert the user that some duplicate selections existed. At this point, I think it is just a matter of your personal preference.
-
Aug 21st, 2017, 02:47 PM
#17
Thread Starter
Enjoy the moment
Re: trying to check if a value exists from another form something is Wrong!!
If I was a user, I would be really annoyed with that logic
you are right sir
You could separately count how many duplicates and if there were any, alert the user that some duplicate selections existed.
how do i do that?
-
Aug 21st, 2017, 02:55 PM
#18
Re: trying to check if a value exists from another form something is Wrong!!
Originally Posted by salsa31
how do i do that?
declare another Integer variable, maybe: iDuplicates
Just before the "Exit For" statement: iDuplicates = iDuplicates + 1
If you wish to display the duplicate items, then this could work
declare another String variable, maybe: strDuplicates
Just before the "Exit For" statement: strDuplicates = strDuplicates & vbCrLf & LTreat.ListItems(I).Text
Now you can use something like this:
Code:
If IsCheked = False Then
MsgBox "no product has been chosen", vbInformation
Else
If strDuplicates <> vbNullString Then
Msgbox "Products have been added. The following were already added previously:" & strDuplicates, vbInformation
End If
Unload Me
End If
In either case, remove the msgbox just before the "Exit For" statement.
-
Aug 21st, 2017, 03:05 PM
#19
Thread Starter
Enjoy the moment
Re: trying to check if a value exists from another form something is Wrong!!
BINGO!!!
thank you very much sir for your time and patience
thank you all for your help
-
Aug 21st, 2017, 03:26 PM
#20
Re: [RESOLVED] trying to check if a value exists from another form something is Wrong
You're welcome. If you used the FindItem method, you wouldn't need 2 loops. Changing the code from post #12, it would look like this (hopefully no typos):
Code:
Dim LX As Integer
Dim IsCheked As Boolean
Dim I As Integer
For I = 1 To LTreat.ListItems.Count
If LTreat.ListItems(I).Checked Then
IsCheked = True
If Frmnewproduct.LsVw.FindItem(LTreat.ListItems(I).Text, lvwText) Is Nothing Then
Set itm = Frmnewproduct.LsVw.ListItems.Add(, , (LTreat.ListItems(I).Text))
itm.SubItems(1) = LTreat.ListItems(I).SubItems(1)
itm.SubItems(2) = LTreat.ListItems(I).SubItems(2)
itm.SubItems(3) = LTreat.ListItems(I).SubItems(3)
itm.SubItems(4) = LTreat.ListItems(I).SubItems(4)
itm.SubItems(5) = LTreat.ListItems(I).SubItems(5)
itm.SubItems(6) = LTreat.ListItems(I).SubItems(6)
Else
' duplicate item, track it if you want to
End If
End If
Next
If IsCheked = False Then
MsgBox "no product has been chosen", vbInformation
Else
Unload Me
End If
Edited: removed the final parameter in FindItem() method. Not needed in this case and could cause error if the listview has no items yet.
Last edited by LaVolpe; Aug 21st, 2017 at 03:33 PM.
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
|