Click to See Complete Forum and Search --> : runtime error '340'
dimpsoo
October 5th, 2005, 03:46 AM
i am getting thia error
runtime error '340'
control array element '1' doesnt exit
i am dynamically creating text boxes and their respective tables first time it goes throuh
but the second time when i amd loading the text boxes created at run time it throws this error.
please help
HanneSThEGreaT
October 5th, 2005, 04:07 AM
Hello :wave:
Are you using a loop ¿
Can you post your code which gives the error, so that we can have a look :thumb:
rahul.kul
October 5th, 2005, 04:08 AM
Hi, Please post your code to define your problem !
dimpsoo
October 5th, 2005, 04:23 AM
For i = 0 To Text1().UBound
ind = Text1(i).Index
taxval = Text1(i).Text
next
first time the loop runs fine
second time on ind = Text1(i).Index it throws the error
rahul.kul
October 5th, 2005, 04:30 AM
Hi Dimpsoo !
I debug your code.
I place two textboxes on the form named Text1 having index 0 and 2 ( i missed index 1)
and when i execute your code it gives the same error, which you talked about earlier.
that means your indexing of the control is not correct! that's why it run's first time perfectly as it founds 0th index and when it looks up for 1st index of the textbox, it throws an error :
runtime error '340'
control array element '1' doesnt exit
So , i think you got the point !
Regards!
HanneSThEGreaT
October 5th, 2005, 04:36 AM
i am dynamically creating text boxes and their respective tables first time it goes throuh
Post the code you use to create the textboxes
rahul.kul
October 5th, 2005, 04:42 AM
I Dont so Dimpsoo uses the code for creating the texboxes.. !
I think he just places the texboxes with copy-paste technique and somehow deleted accidently a texbox having index as 1.
that's why when he execute his code, got the above mentioned error !
HanneSThEGreaT
October 5th, 2005, 04:46 AM
I Dont so Dimpsoo uses the code for creating the texboxes.. !
I think he just places the texboxes with copy-paste technique and somehow deleted accidently a texbox having index as 1.
that's why when he execute his code, got the above mentioned error !
I agree with your point Rahul :thumb:
The reason I asked for the code, was becuase he stated, he is dynamically creating the controls - that gave me the impression he uses code. After all you dynamically create a control with code.
dimpsoo
October 5th, 2005, 05:30 AM
First i create a label and text box with iindex0 on my form
then i dynamically create textboxes and labels
strsql_load = "select count(*) from tax_majr_head_mstr where actv='A' "
Set rsload = conn.Execute(strsql_load)
cnt = rsload(0)
Dim temptop As Long
Dim templeft As Long
Dim txttop As Long
Dim txtleft As Long
templeft = 120
temptop = 120
txtleft = 125
txttop = 125
Label1(0).BorderStyle = 1
Label1(0).Caption = ""
Label1(0).Visible = False
Label1(0).Move 120, 120, 4200, 300
strsql_load = "select dcsrptn from tax_majr_head_mstr where actv='A' order by majr_head "
Set rsload = conn.Execute(strsql_load)
Do Until rsload.EOF
str_majr_desc = rsload.Fields(0)
n = n + 1 'supplies you with array index.
Load Label1(n)
Load Text1(n)
Label1(n).Caption = str_majr_desc
Label1(n).Visible = True
Text1(n).Visible = True
Label1(n).Top = temptop
Label1(n).Left = templeft
Text1(n).Top = txttop
templeft = templeft + Label1(0).Width + 60
txtleft = txtleft + Label1(n).Width + 60
templeft = 120
temptop = temptop + Label1(0).Height + 60
txtleft = 125
txttop = txttop + Label1(n).Height + 60
rsload.MoveNext
Loop
then i use this code to view what has been entered in my text box taht was dynamically created
For i = 0 To Text1().UBound
ind = Text1(i).Index
taxval = Text1(i).Text
next
hope i have made my problem clear now
HanneSThEGreaT
October 5th, 2005, 05:58 AM
Try this:
For i = 0 To Text1.Count
ind = Text1(i).Index
taxval = Text1(i).Text
next
If you get the same error, then the problem is somewhere else :wave:
dimpsoo
October 5th, 2005, 06:16 AM
now its giving me
control array element '12' doesnt exit
instead of control array element '1' doesnt exit
there are on 11 textboxes as there are only 11 records in my database.
the last inex value i get is 11
HanneSThEGreaT
October 5th, 2005, 06:25 AM
Do this
For i = 0 To Text1.Count -1
ind = Text1(i).Index
taxval = Text1(i).Text
next
I'm glad we're making progress :thumb:
rahul.kul
October 5th, 2005, 06:31 AM
absolutely Correct !!
dimpsoo
October 5th, 2005, 06:36 AM
For i = 0 To Text1.Count - 1
ind = Text1(i).Index
taxval = Text1(i).Text
next
first time goes thru fine
then again when i am adding the dynamically created text box values i get my first error
viz. control array element '1' does not exist
if i do this then
For i = 0 To Text1.Count
ind = Text1(i).Index
taxval = Text1(i).Text
next
then i get control array element '12' does not exist
the first time only
HanneSThEGreaT
October 5th, 2005, 06:46 AM
O K :confused:
Try
For i = 0 To Text1.Count
If i >= 11 Then Exit For
ind = Text1(i).Index
taxval = Text1(i).Text
next
If this doesn't work, I'm lost :eek:
Shuja Ali
October 5th, 2005, 07:10 AM
Basically what you are trying to do is add the controls at run-time and later you remove one of the controls. And when you loop through the Text Boxes you created, you are trying to check for that control too.
A better way would be to loop through the Controls Collection and checking for the TextBoxes. This way you don't have to worry about bounds. Even if you remove
'Instantiate a control
Dim txtTemp As Control
'Loop through the Controls collection on the form
For Each txtTemp In Me.Controls
'check if the control is Text Box
If TypeOf txtTemp Is TextBox Then
ind = txtTemp.Index
txtVal = txtTemp.Text
End If
Next
Now remember this will loop through all the controls present on the Form. Also For Each loop is always better than a For value....Next loop. It executes faster.
Another better way to do this is to use collections (it is even better than the 1 I showed previously). When you add Text Boxes to your form, add them to a collection and when you remove them, remove them from the collection. And when you need to loop through them, just loop through the text boxes present in the Collection.
I don't have time to show this, but you could look up for the help in MSDN.
PS: If I get some time, i will definetly post a sample.
dimpsoo
October 5th, 2005, 07:18 AM
please dont give up.. i ahve to fin this today, else dunno what will happ?????
this solution works fine for the first time.
i unload the page reload it ,then it doesnt work
my flow is
1)accept data from user on button click next page
2)depending on data entered i load the dynamically created label and text box.
3)then add this to database. 4)unload this page only not the labels and text box and go back to the first screen steps from 1-2 but it is here that on adding it gives me the error of control array '1'
do i need to unload the control arrays too. if yes where nd when
HanneSThEGreaT
October 5th, 2005, 07:30 AM
i unload the page reload it ,then it doesnt work
Why do you unload the Form ¿
I agree 100% with vb_the_best using For Each is actually the Preferres method to take. Have a look at his example as well
Shuja Ali
October 5th, 2005, 07:37 AM
i unload the page reload it ,then it doesnt work What do you man by Page here? Is it the Form that you are unloading. If so, the controls you created at run-time also get unloaded, so when you try to load the form again you again need to create/load the controls.
Have you tried the above posted code?
dimpsoo
October 5th, 2005, 07:58 AM
on using this code
'Instantiate a control
Dim txtTemp As Control
'Loop through the Controls collection on the form
For Each txtTemp In Me.Controls
'check if the control is Text Box
If TypeOf txtTemp Is TextBox Then
ind = txtTemp.Index
txtVal = txtTemp.Text
End If
Next
it gives me "Object not an araay on this line ind = txtTemp.Index
what do i do now
Shuja Ali
October 5th, 2005, 08:05 AM
it gives me "Object not an araay on this line ind = txtTemp.Index
what do i do now Either the controls you are adding are not a control array or there are other textboxes present on the form which are not part of the control array.
How are you adding the TextBoxes in the Form. Are you using control array?
Did you try using collections instead? Take a look at collections in your MSDN documentation.
HanneSThEGreaT
October 5th, 2005, 08:09 AM
on using this code
'Instantiate a control
Dim txtTemp As Control
'Loop through the Controls collection on the form
For Each txtTemp In Me.Controls
'check if the control is Text Box
If TypeOf txtTemp Is TextBox Then
ind = txtTemp.Index
txtVal = txtTemp.Text
End If
Next
it gives me "Object not an araay on this line ind = txtTemp.Index
what do i do now
The reason for the error is that txtTemp is not a control array, try removing that line, ind = txtTemp.Index
run again
dimpsoo
October 5th, 2005, 08:32 AM
if i remove taht line i will only get the text box values but i also need the
index values to associate the labels to the text box
HanneSThEGreaT
October 5th, 2005, 08:38 AM
if i remove taht line i will only get the text box values but i also need the
index values to associate the labels to the text box
Then it still boils down to
How are you adding the TextBoxes in the Form. Are you using control array?
As vb_the_best asked.
I think you should post your project, otherwise we'll get nowhere
Shuja Ali
October 5th, 2005, 08:40 AM
if i remove taht line i will only get the text box values but i also need the
index values to associate the labels to the text box
How are you creating the controls at run-time?
Are there any text boxes already present on the form?
Dan_H
October 5th, 2005, 09:22 AM
Hi Dimpsoo,
Just a thought; looking at the code you've posted, I can't see where you've declared your variable 'n'. You've not already set this to 1 elsewhere have you?
If you have, you increment n at the beginning of the loop, so the first control created would have an index of 2.
Also, some tips. Use With statements as they make code easier to read, as in
With text1(0)
.caption = "blah"
.visible = true
.left = 10
end with
And use code tags when posting code, its easier on the eye!
...
Dan_H
October 5th, 2005, 09:23 AM
Whoa...
Didn't spot the second page here. I hope this is still relevant
...
HanneSThEGreaT
October 5th, 2005, 09:33 AM
How are you creating the controls at run-time?
Are there any text boxes already present on the form?
Thanx Shujah for all your input and help as well, because to be honest I'm all out of ideas :eek:
As far I remember he said that he created a lable and a textbox, set their Index properties to 0, then he loads them..
n = n + 1 'supplies you with array index.
Load Label1(n)
Load Text1(n)
Then it also brings me to Dan's point
Just a thought; looking at the code you've posted, I can't see where you've declared your variable 'n'. You've not already set this to 1 elsewhere have you?
If you have, you increment n at the beginning of the loop, so the first control created would have an index of 2.
Shuja Ali
October 5th, 2005, 09:54 AM
If there are Text Boxes present on the Form that are not part of the control array, then they will also be checked in the Loop I have shown. So that might be the reason of the error that "Object not an array".
Now in this case, I would use a Collection and add my textboxes and labels to the collections. And when I unload any of the Textboxes/labels, I would remove the same from the collections too. When I need to loop through them, I would loop through the collections instead of looping through all the controls present on the Form.
rahul.kul
October 6th, 2005, 07:34 AM
u r VB_the_Best also?
rahul.kul
October 6th, 2005, 07:34 AM
congrates on your 1000 post !
pough
March 11th, 2007, 05:05 PM
i am getting thia error
runtime error '340'
control array element '1' doesnt exit
i am dynamically creating text boxes and their respective tables first time it goes throuh
but the second time when i amd loading the text boxes created at run time it throws this error.
please help
Make sure that on Unloading Form event all loaded at run time controles will be unloaded. Leave just the 0 indexed control of each type on the form was placed at the design time.
Some sample:
Private Sub Form_Unload(Cancel As Integer)
Dim lvControl As Control
For Each lvControl In Me.Controls
If lvControl.Name = "picDocBody" Then
If lvControl.Index > 0 Then Unload lvControl
End If
Next
End Sub
After that every time you will load the form the error will not raise.
Hope that this will help for others will read this thread as it is quite late answer for you.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.