Solved

Problems With ListView Boxes & Deleting items

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now