-
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
-
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.
-
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.
-
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?
-
Re: run time error '340' - Control array element '29' doesn't exsit
Quote:
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
-
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).
-
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
-
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.
-
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
-
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?
-
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...?
-
Re: run time error '340' - Control array element '29' doesn't exsit
What happened when you tried the code I provided?
-
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
-
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.
-
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.
-
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
-
Re: run time error '340' - Control array element '29' doesn't exsit
Quote:
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:
Quote:
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.
-
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
-
Re: run time error '340' - Control array element '29' doesn't exsit
Merri, suggest you change from: 1 to 39, to: 1 to 28 ;)
-
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.
-
Re: run time error '340' - Control array element '29' doesn't exsit
Quote:
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.