Solved

Problems With ListView Boxes & Deleting items

Posted on 2006-06-22
3
159 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 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

861 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