hi good morning,
I would like to ask why when I run my program, but it appears a message like this "Runtime error '381 'Invalid property array index".
I am confused about this because I am still a beginner :)
what is the solution for this?
This is my code:

For i = 0 To List2.ListCount + 1
List2.Selected(i) = True
If List2.Selected(i) = True Then
  SQLTambah = "Insert Into tbmusik (idmusik,nama,judul,singer,kategori,path,location) values ('" & Text1 & "','" & Text2 & "','" & Text7 & "','" & Text3 & "','" & Combo1 & "','" & Text5 & "','" & Text8 & "')"
            conn.Execute SQLTambah
             Call Command1_Click
            MsgBox ("Data berhasil disimpan.! :D ")
             Call cmdMovetoRight_Click
            Call Form_Load
            End If
 Next i

regards,
Vivi

Please post the actual string value of SQLTambah so we can see the query you are running.

For i = 0 To List2.ListCount + 1

Shouldn't this be:

For i = 0 To List2.ListCount - 1

Doh! Time for new reading glasses. Either that or stop posting at 3:00 am

I agree with Pritaeas. If that doesn't work, you can try adding a breakpoint and see where the problem is exactly.

For : Pritaeas, dom246

I've replaced it become like this

For i = 0 To List2.ListCount - 1
List2.Selected(i) = True
If List2.Selected(i) = True Then
  SQLTambah = "Insert Into tbmusik (idmusik,nama,judul,singer,kategori,path,location) values ('" & Text1 & "','" & Text2 & "','" & Text7 & "','" & Text3 & "','" & Combo1 & "','" & Text5 & "','" & Text8 & "')"

but the error was there on the second row,
and the problem is its array index,
I am confused please help,
and dom246 if you could explain more details about the breakpoint?
I still do not understand

thanks

For : Reverend Jim

Hai Jim,
Can you explain in more detail about the actual value of SQLTambah string that is in my program?
I still do not understand what you mean,

thanks,

Hi ViVi,
1.
.ListCount start counting at 1
So that part looks right in your last post.
.Selected is Boolean and indicates either that the user has selected an item or .selected(i) = true selects item i in the list (starts counting at 0)
If list2 is empty i will be -1 which will cause your fault code.
You need to know what listcount is.
Do one of two things:
Add a line in your code at line 1

Debug.Print list2.listcount

and the value will print in the immedate window (View\immedate window)
Or:
2.
To place a break in your program put your cursor in the margin to the left of any executable line (not a comment and not a declaration)
Click in the margin. A circle should appear in the margin and the executable line will highlight.
The code will pause at this line.
If i is declared in the sub you can HOLD your cursor over i and its value will show in a note by the cursor.

First you must know what the meaning of the error warning.
"Runtime error '381 'Invalid property array index".

You said that vb pointing you in line 2 (List2.Selected(i) = True) when error is came up, it means that your selected index is not available on the list.

Hai Jx_Man,
if my index is not available in the list should I do?
because it's what I use code like this

For i = 0 To List2.ListCount - 1

please help,

thanks

For : Klahr_R
I've added this code on the first line,

    Debug.Print list2.listcount

but the error still remains as in the beginning Klahr_R,
even though I have added the code.
is there any other possibility?
I've absolutely no index?

thanks

I spent some time trying to generate the 381 error from this code. I found two cases where the index for List2.Selected(index?) will throw a 381:

if the index? is greater than the largest index, which would happen with the original code posted going to List2.ListCount + 1

AND

if the index? = -1 which would occur in the later posting where
for 1 = -1 to List2.ListCount -1

If I may ask a silly question, does the routine fail on the first time through the loop, or on a subsequent time through? I ask this, because your logic appears to do this:
1. Start the loop
2. Set the "selected" property to "True" in every case
3. Check to see if the "selected" property is "True", which it will always be since you just set it that way.
4. Execute some SQL
5. Execute some other subroutines (and we don't know what happens in them)
6. Do it again for the next row in the listbox.

So the next obvious question is, are you in your "cmdMovetoRight_Click", "Command1_Click" or "Form_Load" subroutines, RESETTING the listbox "List2" so that it's empty? Let's think with this. If you do that, then the listbox's ListCount would be zero. So, when your iteration variable "i" tries to point to the NEXT row, there will not be a valid listbox row to point to.

My suggestion is investigate the other routines, find out if the listbox is being reset anywhere, fix that, and maybe even rethink your listbox handling logic.

Good luck! Hope this helps!

Bit Bit:
All you say may well be true and worthwhile suggestions, but the List2.Selected(X) automatically triggers a List2 click event which, as I understand it in this case, causes the text boxes to update. Consequently some (unseen) code executes between the two lines. An interesting albeit unusual method.
As with most early efforts, Job 1 is to get it working, then improve and polish.

Klahr_R:
I've tried some of the code you have given me, I have been using code like this:

For i = 0 To List2.ListCount - 1

and the result is that I can Select two data simultaneously, but if I possessed the data in the list more than two data will appear error '381 'is,
if I use code like this:

For i = -1 To List2.ListCount - 1

and the result is that I can only Select only one data,
thanks, :)

Bit Bit:
okay thanks for all your help,
logic that you provide is true according to my logic for this,
so maybe I did not investigate it,
but if you could give advice to the code that you think is more correct?

thanks,

@vividia: Without knowing what the other routines are doing, it's really kind of hard to suggest anything. As I mentioned above, I suspect that somewhere in one of the called routines, your listbox is either being reloaded or cleared out, and that causes a situation where your iteration variable "i" is trying to point to a list entry that no longer exists. But since you didn't post the code to those routines, we can only assume or guess.

Is there a specific reason you have to retrieve the data and load the form before you can insert it into the database? Unless there is some kind of fancy processing going on in there, have you considered just looping through the list box one time, and using a "insert into...select from" and push all the heavy lifting onto the DBMS? Just a thought.

All:
all the advice that has been given has helped me in completing my program,
and I also tried to implement my program,
and finally WORKED!!
My program is now complete,
Thank you so much for helping me,
we will discuss the new problems,

regards,
Vivi

Glad we could help. Please mark the thread "solved". Happy coding!

thank you BIt Bit and for everything,
how to mark the thread "solved"?
I still do not know, because I've just joined here.

V

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.