Dim Dbl1 As Double
Dim Dbl2 As Double
Dim Aux1 As String
Dim qtyadd As String
Dim subtotaladd As String
Dim itm As MSComctlLib.ListItem
Dim i As Integer
  Set itm = ListView1.FindItem(txtCode.Text)
If itm Is Nothing Then
ListView1.ListItems.Add , , txtCode.Text
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtproductname.Text
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtqty.Text
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtprice.Text
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , lbltotal.Text
ElseIf ListView1.ListItems.Item(ListView1.ListItems.Count).Text <> txtCode.Text Then
ListView1.ListItems.Add , , txtCode.Text
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtproductname.Text
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtqty.Text
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtprice.Text
ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , lbltotal.Text
ElseIf Not itm Is Nothing Then
For i = 1 To ListView1.ListItems.Count
If ListView1.ListItems(i).Text = txtCode.Text Then
    Dbl1 = (ListView1.ListItems(i).SubItems(2))
    Dbl2 = (txtqty.Text)
    ListView1.ListItems(i).SubItems(2) = Format((Dbl1 + Dbl2))
    'Total
    Dbl1 = (ListView1.ListItems(i).SubItems(4))
    Dbl2 = (lbltotal.Text)
    ListView1.ListItems(i).SubItems(4) = Format((Dbl1 + Dbl2), "###################.00")
    Aux1 = "*"
End If
Next i

End If

Sir Help me to Correct this Code the output of this code like this
So if you have.

item1 1

then add item1 again you get

item1 2

also if you have

item2 1
item1 1

and then you add item1 you get

item2 1
item1 2

but, if you have

item1 1
item2 1

then you add another item1, you want

item1 1
item2 1
item1 1

help me sir..

From your examples I assume if the new item equals the last item in the list then add the value to the last item, otherwise add the new item to the list. Something like this might work:

    Dim Itm As MSComctlLib.ListItem
    Dim Dbl1 As Double
    Dim Dbl2 As Double
    Set Itm = ListView1.FindItem(txtCode.Text,0)
    If Not Itm Is Nothing And ListView1.Items(ListView1.Items.Count -1).Text = Itm.Text Then
        Dbl1 = CDbl(Itm.SubItems(2))
        Dbl2 = CDbl(txtqty.Text)
        Itm.SubItems(2) = Format((Dbl1 + Dbl2))
        Dbl1 = CDbl(Itm.SubItems(4))
        Dbl2 = CDbl(lbltotal.Text)
        Itm.SubItems(4) = Format((Dbl1 + Dbl2), "###################.00")
    Else
        Set Itm = ListView1.ListItems.Add(, , txtCode.Text)
        Itm.SubItems(1) = txtproductname.Text
        Itm.SubItems(2) = txtqty.Text
        Itm.SubItems(3) = txtprice.Text
        Itm.SubItems(4) = lbltotal.Text            
    End If

Edited 3 Years Ago by tinstaafl

Try this:

    Dim Itm As MSComctlLib.ListItem
    Dim Dbl1 As Double
    Dim Dbl2 As Double
    Dim AddItem As Boolean
    AddItem = False
    Set Itm = ListView1.FindItem(txtCode.Text, 0)
    If ListView1.ListItems.Items.Count = 0 Then
        AddItem = True
    Else
        If Itm Is Nothing Or ListView1.Items(ListView1.ListItems.Count - 1).Text <> Itm.Text Then
            AddItem = True
        End If
    End If
    If Not AddItem Then
        Dbl1 = CDbl(Itm.SubItems(2))
        Dbl2 = CDbl(txtqty.Text)
        Itm.SubItems(2) = Format((Dbl1 + Dbl2))
        Dbl1 = CDbl(Itm.SubItems(4))
        Dbl2 = CDbl(lbltotal.Text)
        Itm.SubItems(4) = Format((Dbl1 + Dbl2), "###################.00")
    Else
        Set Itm = ListView1.ListItems.Add(, , txtCode.Text)
        Itm.SubItems(1) = txtproductname.Text
        Itm.SubItems(2) = txtqty.Text
        Itm.SubItems(3) = txtprice.Text
        Itm.SubItems(4) = lbltotal.Text
    End If

Edited 3 Years Ago by tinstaafl

Sorry my previous code won't work, tried to do it on the fly. This code will work, again my apologies.

    Dim Itm As MSComctlLib.ListItem
    Dim Dbl1 As Double
    Dim Dbl2 As Double
    Dim AddItem As Boolean
    AddItem = False
    If ListView1.ListItems.Count = 0 Then
        AddItem = True
    Else
        Set Itm = ListView1.FindItem(txtCode.Text, 0)
        If Itm Is Nothing Then
            AddItem = True
        ElseIf ListView1.ListItems(ListView1.ListItems.Count).Text <> Itm.Text Then
            AddItem = True
        End If
    End If
    If Not AddItem Then
        Dbl1 = CDbl(Itm.SubItems(2))
        Dbl2 = CDbl(txtqty.Text)
        Itm.SubItems(2) = Format((Dbl1 + Dbl2))
        Dbl1 = CDbl(Itm.SubItems(4))
        Dbl2 = CDbl(lbltotal.Caption)
        Itm.SubItems(4) = Format((Dbl1 + Dbl2), "###################.00")
    Else
        Set Itm = ListView1.ListItems.Add(, , txtCode.Text)
        Itm.SubItems(1) = txtproductname.Text
        Itm.SubItems(2) = txtqty.Text
        Itm.SubItems(3) = txtprice.Text
        Itm.SubItems(4) = lbltotal.Caption
    End If

Notice I set a Boolean value in the main logic. This is easier for debugging. If your logic needs changing then you don't have to copy and paste large blocks of code.

Edited 3 Years Ago by tinstaafl

Thanks Sir Your Code is Working But, If the Item in Listview Find the only the last item Should Be Updated not in the First, Help me Sir.

I think this will do what you want:

Private Sub FillListView()

    Dim Itm As MSComctlLib.ListItem
    Dim Dbl1 As Double
    Dim Dbl2 As Double
    Dim AddItem As Boolean
    AddItem = False
    If ListView1.ListItems.Count = 0 Then
        AddItem = True
    Else
        Set Itm = ListView1.FindItem(txtCode.Text, 0)
        If Itm Is Nothing Then
            AddItem = True
        ElseIf ListView1.ListItems(ListView1.ListItems.Count).Text <> Itm.Text Then
            AddItem = True
        Else
            Set Itm = ListView1.ListItems(ListView1.ListItems.Count)
        End If
    End If
    If Not AddItem Then
        Dbl1 = CDbl(Itm.SubItems(2))
        Dbl2 = CDbl(txtqty.Text)
        Itm.SubItems(2) = Format((Dbl1 + Dbl2))
        Dbl1 = CDbl(Itm.SubItems(4))
        Dbl2 = CDbl(lbltotal.Caption)
        Itm.SubItems(4) = Format((Dbl1 + Dbl2), "###################.00")
    Else
        Set Itm = ListView1.ListItems.Add(, , txtCode.Text)
        Itm.SubItems(1) = txtproductname.Text
        Itm.SubItems(2) = txtqty.Text
        Itm.SubItems(3) = txtprice.Text
        Itm.SubItems(4) = lbltotal.Caption
    End If

Thanks Sir, Your Great This is What I want, Sir Can U Explain to Why You Use Boolean Property?

Notice I set a Boolean value in the main logic. This is easier for debugging. If your logic needs changing then you don't have to copy and paste large blocks of code.

Especially if you have to modify it months or years down the road. It is much easier to think in terms of true or false rather than add new item or adjust existing item. Conversely, if you need to change which columns to display, or the type of data in a column, you'll have fewer places to make changes.

Your welcome. Please remember to mark this solved thank you.

Edited 3 Years Ago by tinstaafl

Comments
indeed.

I agree with you tinstaafl. When creating an application we think that it is running smooth, nothing will change. 6 months down the line the user wants additional features or something changed... It is so much easier to just work with blocks than entire pages of code.

This question has already been answered. Start a new discussion instead.