[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

Listview Question

Hello, i am in need of some help working with a listview in VB.

I need to be able to add to a QTY within the list view... This is my code right now:

Private Sub Form_Load()
With LV1
        .ColumnHeaders.Add , , "QTY", 550
        .ColumnHeaders.Add , , "Item Name", 2500
        .ColumnHeaders.Add , , "Price"
        .View = lvwReport
End With
End Sub

Private Sub Command3_Click()
Dim ListObj As ListItem
Dim QTY, Name As String
Dim Price As String

Name = "Sandwich"
QTY = "1"
Price = "$" & "1.22"
        Set ListObj = LV1.ListItems.Add(, , QTY)
        ListObj.SubItems(1) = Name
        ListObj.SubItems(2) = Price
End Sub

This is what it looks like when i push the command button to test it out..
QTY  |   Item      |   Price
 1       Sandwich    $1.22
 1       Sandwich    $1.22

But i need it to do this...

QTY  |   Item      |   Price
 2       Sandwich    $2.44

So this is what i need:
 - The QTY to add if the button is hit more than just once
 - Add the Price if the button is hit more than just once

Thats it!

Thanks for your time,
David
0
Stealthrt
Asked:
Stealthrt
  • 3
  • 2
1 Solution
 
DabasCommented:
Hi Stealthrt:
Does your listview have to be Qty Item Price, or can it be Item Qty Price
Reason I am asking is that you probably will need to use the ListView's FindItem function, and it will be easier if your ListItem's text property is the one to search

Dabas
0
 
StealthrtAuthor Commented:
I would like it to be first so it could be read as 1 item price.... But if it wont work like that then i guess i could have it as item qty price.... But i really would like it more qty item price...

David
0
 
DabasCommented:
David:
Its not a big issue.
instead of Set ListObj = lv1.listitems.add(,,Qty)
Try

    Set ListObj = lv1.FindItem(Name, lvwSubItem)
    If ListObj is nothing then
        Set ListOBj = lv1.ListItems.Add(,,Qty)
    else
        listObj.Text = Val(ListObj.Text) + Qty)
    end if

    ListObj.SubItems(1) = Name
    ListObj.Subitems(2) = Price
Dabas
0
 
StealthrtAuthor Commented:
That worked just fine Dabas...

Another question though... if i wanted to double click on that in the listview to subtract one from the quanity, how would i do that? Im able to do that now but only if its the same product.

Private Sub LV1_DblClick()
Dim ListObj As ListItem 'Set listObj as a listitem
Dim QTY, Name As String
Dim Price As String

Name = "CFA Sandwich"
Price = "$" & "1.22"
QTY = 1
Set ListObj = LV1.findItem(Name, lvwSubItem)
    If ListObj Is Nothing Then
        Set ListObj = LV1.ListItems.Add(, , QTY)
    Else
        ListObj.Text = Val(ListObj.Text - QTY)
    End If

    ListObj.SubItems(1) = Name
    ListObj.SubItems(2) = Price

End Sub

Any suggestions?

Thanks again,
David
0
 
DabasCommented:
Stealthrt:
>         ListObj.Text = Val(ListObj.Text - QTY)
Should be Val(ListObj.Text) - QTY

Dabas
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now