Cor can u help again?

G

Guest

Hi Cor

I spoke with u previously about this problem i had and u gave me some code that helped me. That code worked fine but i have now encountered another issuee that im hoping u may know how to solve
What my code below is doin is if a combobox and textbox is not empty, it is adding the items to a listview
The listview has 2 columns...
Product Quantit

What i want to do is to add the product along with the quantity into the listview only if that product has not been inserted previously. If it has been inserted previosly then i do not want a new row to be added but i simply want the quantity to be incremented
e.g if Nintendo 3 already exists in the lisview, if the user again selects nintendo with a quantity of 2, i do not want this entire row to be added again. I only want Nintendo 5 to be displayed (only the quantity should be updated).
what currently happens is that if the same product is added again the listview is searched and the quantity is correctly updated. However the problem i have at the momnet is that a new row is also inserted after the quantity has been updated. Does anyone know how i can stop this? This is my full code..

If Not ((CBoxProduct.Text = "") Or (txtQty.Text = "")) The

Dim Product As Strin
Product = CBoxProduct.Text.ToStrin
Dim Quantity As Intege
Quantity = txtQty.Tex

Dim Item1 As New ListViewItem(Product , 0

Dim item As ListViewIte
For Each item In LViewProductReq.Item
If item.Text = Product The
'if product is already in the listview, update the quantity instaed of addin a new
' row into the listvie
item.SubItems(1).Text = (CInt(item.SubItems(1).Text) + Quantity).ToStrin
Exit Fo
End I
Nex

'if the product does not exist in the listview i want to add it to the listvie
Item1.SubItems.Add(Quantity

'add the items to the listvie
LViewMealBookings.Items.Add(Item1

Els
'else display a message to state all required field are neede
MsgBox("Please ensure all the required fields are entered correctly"
End If
 
C

Cor

Hi Jiten,

This is not difficult I think, there are more possibilities, I take the one
which needs the less changes and shows it the best to you.
If Not ((CBoxProduct.Text = "") Or (txtQty.Text = "")) Then

Dim Product As String
Product = CBoxProduct.Text.ToString
Dim Quantity As Integer
Quantity = txtQty.Text

Dim Item1 As New ListViewItem(Product , 0)
Dim found as boolean
Dim item As ListViewItem
For Each item In LViewProductReq.Items
If item.Text = Product Then
'if product is already in the listview, update the
quantity instaed of addin a new
' row into the listview
item.SubItems(1).Text = (CInt(item.SubItems(1).Text) +
Quantity).ToString

found = true
Exit For
End If
Next

if not found then
'if the product does not exist in the listview i want to add it to the listview
Item1.SubItems.Add(Quantity)

'add the items to the listview
LViewMealBookings.Items.Add(Item1)

end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Listbox 3
Access Passing the value to a table 1
VBA Data Entry Window 1
Problem rounding down Access 2000 2
adding items to a Listview 1
Help with lookup 4
Listboxes on userform 3
Counting occurances and multiplying by a quantity 1

Top