Solved

Problems With ListView Boxes & Deleting items

Posted on 2006-06-22
3
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 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