Index out of range (1 Viewer)

me1258

Registered User.
Local time
Today, 01:09
Joined
Apr 6, 2004
Messages
37
Hi:confused:
This code error is a bit complicated so I hope I can explain it properly.
This is from a ticketing program I am working on.
When the user clicks one of the buttons, say the adult button, the ticket type, price, qty, and total appear in a list box.
One click on any button adds one of that type of ticket.
Here is what is happening. When I start the program several buttons appear each with a kind of ticket. Say Adult, child, Auto.
If I pick any button and click it once it will add one of those tickets to the list box. If I click any OTHER button the program will work as needed regardless of what I do after those 2 initial clicks. The error occurs when the first 2 clicks are on the same button.
I get
" Runtime Error 6015
Cant Add this item. The index is to large"
I will also attach the DB for you to look at Thanks


Code:
Code:
Private Sub UpdateReceipt(intCommodity As Integer)

Dim ReceiptLine As String

ReceiptLine = arrCommodityQty(intCommodity) & ";" & _
              arrCommodityDescription(intCommodity) & _
              " @ " & _
              Format(arrCommodityRate(intCommodity), "$#0.00") & ";" & _
              Format(arrCommodityQty(intCommodity) * arrCommodityRate(intCommodity), "$#0.00")

' This next line checks to see if the arrCommodityReceiptLine has been assigned a list count number
' which is the line of the reciept the item is on

If arrCommodityReceiptLine(intCommodity) > 0 Then
    
    ' This next line of code removes your original entry on the lst box so it can be replace with
    ' a new entry with 1 more qty added to it.
    ' ie: If you remove this next line every time you hit Adult you will get a new Adult
    ' entry PLUS the original one will incrament 1 so you would get Adult 2 Adult 1 if you his it again
    ' you will get Adult 3 Adult 2 Adult 1 and so on
    
    lstReceipt.RemoveItem (arrCommodityReceiptLine(intCommodity) - 1)
    
    lstReceipt.AddItem ReceiptLine, (arrCommodityReceiptLine(intCommodity) - 1)
    
    
  Else
    lstReceipt.AddItem (ReceiptLine)
    
    ' this line sets the arrCommodityReceiptLine to the list count number of the lstBox
    ' This is the physical line this commodity is listed on
    ' line 1-3 are populated in sub BuildScreen
    arrCommodityReceiptLine(intCommodity) = lstReceipt.ListCount
    
    End If
    
    ' the intcommodity is hta number of the mail box
    ' the arrCommodityRecieptLine is the contents of the mailbox
    
    
    ' the list box has an index and when you remove one thing everything else the index
    ' for them changes
    
'    If arrCommodityQty(intCommodity) = 0 Then
'    lstReceipt.RemoveItem (arrCommodityReceiptLine(intCommodity) - 1)
'
'
'    End If
 

Attachments

  • 1393728-Stripped.zip
    139.3 KB · Views: 83

petehilljnr

Registered User.
Local time
Yesterday, 23:09
Joined
Feb 13, 2007
Messages
192
Without taking a too deep a look into your code, I'd say somewhere it is trying to reference an item in the list with index = -1 because it has removed too many items or something like that.

I've found the easiest way to deal with adding and removing items from a list is to have the list populated from a table.
Therefore:
When you add an item, you simply add a new record to the table then requery the list control.
When you remove an item, you simply delete a record from the table then requery the list control.
When you're finished with the selected items, delete all records from the table.
It makes things a lot easier also when you go to "do" things with the items you have selected.
 

Users who are viewing this thread

Top Bottom