Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems With ListView Boxes & Deleting items

Posted on 2006-06-22
3
Medium Priority
?
164 Views
Last Modified: 2010-04-30
Hey Experts

I have a problem with my listview box

I am making POS software for a company and i use a listview box to delete a item off the bill. The user selects the item in the listview box that they want to delete then clicks the delete item button and if it the quantity is greater then 1 then it removes one off the quantity if the quantity is equal to 1 then it gets removed from the bill.

The problem is if you have only one item in the bill and its quantity is 1 and you try to delete it my program crashes and I don’t know how to fix it.

The code is as follows

If Int(lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3)) = 1 Then
lstbill.ListItems.Remove (lstbill.SelectedItem.Index)

If Int(lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3)) > 1 Then
lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3) = lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3) - 1
bill(lstbill.SelectedItem.Index) = bill(lstbill.SelectedItem.Index) - 1

It crashes at If Int(lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3)) > 1 Then with the error "Object variable or With block variable not set"

Thanks

Nothern_Lights
0
Comment
Question by:Nothern_Lights
3 Comments
 
LVL 12

Expert Comment

by:jkaios
ID: 16966524
Try putting an "On Error Resume Next" statement in the procedure where the delete operation takes place.
0
 
LVL 2

Expert Comment

by:davidnsc1
ID: 16989142
You need to turn that second if into an ElseIf.  As it is now, if it removes the item, it still does the second check, which is impossible if there are no other elements.
0
 
LVL 1

Accepted Solution

by:
tguez earned 2000 total points
ID: 17040133
ok the problem is in your code... you do not need a resume next.  Look at this:

If Int(lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3)) = 1 Then
lstbill.ListItems.Remove (lstbill.SelectedItem.Index)
>>>> Here, if the item is 1, you remove the item.  So now the list box is empty.
>>>>  There is therefore no value in the selecteditem  because the selected item is
>>>> null.  There are no values in the list box.
>>>> so if you try in the next if line, to check (istbill.selecteditem.index) you will
>>>> get an error because selecteditem is nothing.  It is null.  Since you emptied the list just a line before.

So you can put the if below under an else clause, look below for the modified code:

If Int(lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3)) > 1 Then
lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3) = lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3) - 1
bill(lstbill.SelectedItem.Index) = bill(lstbill.SelectedItem.Index) - 1

It crashes at If Int(lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3)) > 1 Then with the error "Object variable or With block variable not set"
>>>> modified code:

If Int(lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3)) = 1 Then
lstbill.ListItems.Remove (lstbill.SelectedItem.Index)
elseIf Int(lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3)) > 1 Then
lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3) = lstbill.ListItems(lstbill.SelectedItem.Index).SubItems(3) - 1
bill(lstbill.SelectedItem.Index) = bill(lstbill.SelectedItem.Index) - 1

So now you will not have a problem.  If the subitem(3) is=1 then, it will removed, and that is it.  If the subitem(3) is not 1, then the next if will be executed, and no error.

Tomer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question