?
Solved

Problems With ListView Boxes & Deleting items

Posted on 2006-06-22
3
Medium Priority
?
162 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses

752 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