-
Apr 13th, 2010, 08:32 AM
#1
Thread Starter
Lively Member
run time error '340' - Control array element '29' doesn't exsit
Hi All!
I got this error:
run time error '340' - Control array element '29' doesn't exsit
When I press the Debug button,
I'm in this bold line:
For i = 1 To 28
ETSI_Selector(i).Value = 0
ETSI_Selector(i).Visible = False
ETSI_No(i).Visible = False
ETSI_Test(i).Visible = False
Next i
What can I do?
10X!!!
Y
-
Apr 13th, 2010, 08:35 AM
#2
Re: run time error '340' - Control array element '29' doesn't exsit
How can 29 be in play? Your loop only goes from 1 to 28.
-
Apr 13th, 2010, 08:36 AM
#3
Re: run time error '340' - Control array element '29' doesn't exsit
Either create control array element 29 or make your loop from 0 to 28
If there is a third choice, it escapes me.
-
Apr 13th, 2010, 08:44 AM
#4
Thread Starter
Lively Member
Re: run time error '340' - Control array element '29' doesn't exsit
My loop is from 1 to 28!
How the error can be by 29?
-
Apr 13th, 2010, 08:47 AM
#5
Re: run time error '340' - Control array element '29' doesn't exsit
Originally Posted by li9erYol
My loop is from 1 to 28!
How the error can be by 29?
That's what I asked.
Run it again, when you get the error, hover of the variable i and what is its value? Is it 29?
If so, is i declared at the top of your form? If so, it may have been changed due to a call to one of your other events, i.e., ETSI_Selector(i) click event for example. In this case, DIM i in the same routine that your for:next loop is running in
-
Apr 13th, 2010, 08:55 AM
#6
Re: run time error '340' - Control array element '29' doesn't exsit
It's never a good idea to hardcode counters if you can use some dynamic input: in case of control array you have Lbound/Ubound:
Code:
For i = ETSI_Selector.LBound To ETSI_Selector.UBound
'...
Next i
You'd still need some basic error handler to trap non-existant members (control could've been unloaded).
-
Apr 13th, 2010, 08:56 AM
#7
Thread Starter
Lively Member
Re: run time error '340' - Control array element '29' doesn't exsit
i decllration:
Option Explicit
Dim i As Integer
Private Sub Form_Load()
i = 0
' Clear & Disable ETSI Objects (Selectors, ETSI No. & Test Name)
ETSI_Selector(0).Visible = False
ETSI_No(0).Visible = False
ETSI_Test(0).Visible = False
For i = 1 To 39
Load ETSI_Selector(i)
Load ETSI_No(i)
Load ETSI_Test(i)
Next
Private Sub ClearSelectedTests()
For i = 1 To 39
ETSI_Selector(i).Value = 0
ETSI_Selector(i).Visible = False
ETSI_No(i).Visible = False
ETSI_Test(i).Visible = False
Next i
-
Apr 13th, 2010, 08:58 AM
#8
Re: run time error '340' - Control array element '29' doesn't exsit
That should answer your question and verifies my assumptions in previous reply. The variable i is being changed outside of your loop. As I suggested, DIM it in your routines that you use it in, or better yet, DIM a different variable and use that. Do not allow your for:next loops to use the shared i variable and your problem will go away.
Edited: Rhinobull's suggestion is a good one to implement.
Last edited by LaVolpe; Apr 13th, 2010 at 09:02 AM.
-
Apr 14th, 2010, 01:16 AM
#9
Thread Starter
Lively Member
Re: run time error '340' - Control array element '29' doesn't exsit
In the Private Sub ClearSelectedTests():
I remark the 3 lines, as you can see below,
and it works!
What is wrong here? the line "ETSI_Selector(i).Value = 0" is working in this For loop
For i = 1 To 39
ETSI_Selector(i).Value = 0
'ETSI_Selector(i).Visible = False
'ETSI_No(i).Visible = False
'ETSI_Test(i).Visible = False
Next i
-
Apr 14th, 2010, 01:44 AM
#10
Re: run time error '340' - Control array element '29' doesn't exsit
If that works then this should also work.
Code:
For i = 1 To 39
ETSI_Selector(i).Value = 0
ETSI_Selector(i).Visible = Falsese
Next i
Can you try it?
-
Apr 14th, 2010, 02:03 AM
#11
Thread Starter
Lively Member
Re: run time error '340' - Control array element '29' doesn't exsit
It works but only if i is 1 to 2.
I tried another thing:
I remark the first line only:
For i = 1 To 39
'ETSI_Selector(i).Value = 0
ETSI_Selector(i).Visible = False
ETSI_No(i).Visible = False
ETSI_Test(i).Visible = False
Next i
It works!!!
I don't undrestand why...?
-
Apr 14th, 2010, 02:08 AM
#12
Re: run time error '340' - Control array element '29' doesn't exsit
What happened when you tried the code I provided?
-
Apr 14th, 2010, 02:51 AM
#13
Thread Starter
Lively Member
Re: run time error '340' - Control array element '29' doesn't exsit
It didn't work!
I tried another thing and its works! I don't understand the differents:
For i = 1 To 39
ETSI_Selector(i).Value = 0
'ETSI_Selector(i).Visible = False
'ETSI_No(i).Visible = False
'ETSI_Test(i).Visible = False
Next i
ETSI_Selector(1).Visible = False
ETSI_Selector(2).Visible = False
.
.
.
ETSI_Selector(28).Visible = False
ETSI_No(1).Visible = False
ETSI_No(2).Visible = False
ETSI_No(3).Visible = False
.
.
.
ETSI_No(28).Visible = False
ETSI_Test(1).Visible = False
ETSI_Test(2).Visible = False
ETSI_Test(3).Visible = False
.
.
.
ETSI_Test(28).Visible = False
-
Apr 14th, 2010, 03:02 AM
#14
Re: run time error '340' - Control array element '29' doesn't exsit
If you can upload your project with the faulty form then we may be able to check what's going on.
-
Apr 14th, 2010, 03:23 AM
#15
Re: run time error '340' - Control array element '29' doesn't exsit
li9erYol, can you confirm that you have removed the declaration of i from the top and placed it in the routines with the loop? (see posts #5 & #8)
Code:
Option Explicit
Dim i as Integer '<-- bad, will potentially cause problems in loops
Private Sub ClearSelectedTests()
Dim i as Integer '<-- good, no issues with scope
For i = 1 To 39
ETSI_Selector(i).Value = 0
ETSI_Selector(i).Visible = False
ETSI_No(i).Visible = False
ETSI_Test(i).Visible = False
Next i
End Sub
It is also advisable for follow RhinoBull's advice and use LBound() and UBound() to determine the array bounds.
Last edited by Milk; Apr 14th, 2010 at 03:26 AM.
Reason: more
W o t . S i g
-
Apr 14th, 2010, 03:28 AM
#16
Thread Starter
Lively Member
Re: run time error '340' - Control array element '29' doesn't exsit
I'm using the Option Explicit to another variables,
Can I do what you write?
Can you please explain me why:
* Dim i as Integer is bad, will potentially cause problems in loops ?
* Dim i as Integer and no issues with scope?
Thanks again!!!
Y
Last edited by li9erYol; Apr 14th, 2010 at 03:46 AM.
-
Apr 14th, 2010, 03:48 AM
#17
Re: run time error '340' - Control array element '29' doesn't exsit
Originally Posted by li9erYol
I'm using the Option Explicit to another variables,
Can I do what you write?
Can you please explain me why:
* Dim i as Integer is bad, will potentially cause problems in loops ?
* Dim i as Integer and no issues with scope?
Thanks again!!!
Y
The answer is this:
Originally Posted by LaVolpe
That should answer your question and verifies my assumptions in previous reply. The variable i is being changed outside of your loop. As I suggested, DIM it in your routines that you use it in, or better yet, DIM a different variable and use that. Do not allow your for:next loops to use the shared i variable and your problem will go away.
Edited: Rhinobull's suggestion is a good one to implement.
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Apr 14th, 2010, 12:59 PM
#18
Re: run time error '340' - Control array element '29' doesn't exsit
Me speak simple English.
Code:
Option Explicit
' MUST BE REMOVED
Dim i as Integer
' Because: it will be SHARED with ALL procedures
' When changed in one place, changed everywhere
Private Sub ClearSelectedTests()
' Instead: add it in each procedure
Dim i as Integer
' Because: no longer shared with other procedures!
For i = 1 To 39
ETSI_Selector(i).Value = 0
ETSI_Selector(i).Visible = False
ETSI_No(i).Visible = False
ETSI_Test(i).Visible = False
Next i
End Sub
Last edited by Merri; Apr 14th, 2010 at 01:08 PM.
Reason: 1) Fixing Milk's typos 2) Putting it back like it was
-
Apr 14th, 2010, 01:02 PM
#19
Re: run time error '340' - Control array element '29' doesn't exsit
Merri, suggest you change from: 1 to 39, to: 1 to 28
-
Apr 14th, 2010, 01:07 PM
#20
Re: run time error '340' - Control array element '29' doesn't exsit
Milk typo. He fix too.
Edit!
Or not a typo at all, 39 is in Y's own code.
-
Apr 14th, 2010, 01:08 PM
#21
Re: run time error '340' - Control array element '29' doesn't exsit
Originally Posted by Merri
Milk typo. He fix too.
Edit!
Or not a typo at all, 39 is in Y's own code.
Ah, you are correct sir, I see 2 different values in 2 different posts -- good eye.
Which indicates that RhinoBull's suggestion in post #6 is worth implementing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|