Run time error 340

I have a  form on which I have several control arrays, labels and shapes. If I click on the enabled control array an instance of another form is displayed.

I can have several instances of this form open at any one time but only ever four visible. The maximum that can be open at any one time is 18.

The form consists of  20 labels, 4 lines, 2 shapes and a text box array consisting of 5 elements (0 to 4).

The programme works fine until a condition is reached when I try to enter data into or process a line of code with reference to, the text box array, I get the following message:-

     Run-time error '340'
     Control array element '0' doesn't exist?

It seems to occur when the twentieth instance of the form is active.  Although this doesn't occur on every occasion and there is never twenty instances open at one time.

When I debug, all other elements (eg 1 to 4) exist but for some reason element '0' does not.  My question is why??????  How can it exist for many instances and then on the 20th  call it says it does not exist.
abdulvbAsked:
Who is Participating?
 
DanRollinsConnect With a Mentor Commented:
Hi abdulvb,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Refund points and save as a 0-pt PAQ.

EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
0
 
rspahitzCommented:
Without knowing the index and how it relates to the object, this is difficult to solve.

When you run this in the VB environment, what is the index value: 0, 19, 20, ???

Maybe the index is invalid because it's a variable?

At what point was this particular object created: design-time or run-time?

0
 
rspahitzCommented:
It appears that the ?0? is an EE interpretation of '0'?

As such, I would propose that whatever index variable you are using to specify the textbox index is either never being set or is being lost along the way.

When this situation occurs, you'll have to track back and find out why the variable is not set correctly.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
abdulvbAuthor Commented:
The index value is '0' through to (4) and is set up at design time. There is no variable used to determine the value of the index at run-time.

There are five textboxes on the form and the focus is set on the first member, IE textbox(0) when an instance of the form is created at run-time.

Data is entered into the textbox and the focus moves on to the next member.

When the error occurs and I debug, textbox(1) through to textbox(4) exist but textbox(0) does not.

The error only occurs after several of the instances of the form have been created and data is processed succcessfully.
0
 
Valliappan ANSenior Tech ConsultantCommented:
how do you create the instances of the form/text box? could you place some code?
0
 
abdulvbAuthor Commented:
Yes I can. I will extract relevant sections of the code and post it as a comment tomorrow. Thank you for your responses to date.
0
 
abdulvbAuthor Commented:

Below is the procedure that opens the forms.

It is a little hard to read in this textbox but if you paste it into Word I am sure it will make more sense.

Local variables are INT1, INT2 & INT3. All other variables are global variables except for the variables BattingIndex & InningsIndex , which are passed to the procedure when it is called.

Sub Display_Template(BattingIndex As Integer, InningsIndex As Integer)
 
Dim INT1 As Integer
Dim INT2 As Integer
Dim INT3 As Integer

INT1=0
INT2=0
INT3 = ScoreSheet(CurrentSheet).lblBatting(0).Left + ScoreSheet(CurrentSheet).lblBatting(0).Width + (((ScoreSheet(CurrentSheet).ScaleHeight - 1565) / 9) * Innings(CurrentSheet) + 300)


For INT1 = FirstBatter(CurrentSheet) - 1 To BattingIndex
    If ScoreSheetMatrix(CurrentSheet, INT1, Innings(CurrentSheet) - 1).Tag = "Display Template" Then
        ScoringTemplate(CurrentSheet, INT1).Top = 700 + INT2 * 900                                                                ' Position for top of template
        ScoringTemplate(CurrentSheet, INT1).Left = INT3 + INT2 * 900                                                             ' Position for left of template
        ScoringTemplate(CurrentSheet, INT1).Caption = ScoreSheet(CurrentSheet).BattingOrder(Str(INT1 * 2 + 1)).Text     ' Label for template
        If Not OpenTemplate(CurrentSheet, INT1) Then
            ScoringTemplate(CurrentSheet, INT1).Tag = Trim(Str(BattingIndex) + Str(InningsIndex))    ' Unique identifier for template
            BatterinBox = BattingIndex + 1                ' Set current Batter at Bat
            LastBatter = BattingIndex                         ' Set previous batter
        End If
        ScoringTemplate(CurrentSheet, INT1).Show                            ' Show template
        OpenTemplate(CurrentSheet, INT1) = True                              ' Set indicator to open
        INT2 = INT2 + 1
    End If
Next INT1

End Sub




The procedure is called by a click on the corresponding label array . A typical section of code that calls the procedure is listed below.



Private Sub lblArrayInnings1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
 
  Dim INT1 As Integer
  Dim INT2 As Integer

 
Select Case Button
    Case 1         ' Left Mouse Click used to set up or display active scoring template
         If CurrentSheet = 0 Then
            If Innings(1) > Innings(0) Then
               Call Display_Template(Index, (Innings(CurrentSheet) - 1))                               ? Open scoring template
            Else
               Call cmdSwitch_Click                                        ? Switch to other sheet
               Call Display_Template((Batting(CurrentSheet) - 1), (Innings(CurrentSheet) - 1))     ? Open scoring template
            End If
        Else
            If Innings(0) = Innings(1) Then
               Call Display_Template(Index, (Innings(CurrentSheet) - 1))                    ? Open scoring template
            Else
               Call cmdSwitch_Click                                        ? Switch to other sheet
               Call Display_Template((Batting(CurrentSheet) - 1), (Innings(CurrentSheet) - 1))     ? Open scoring template
            End If
        End If


End Select


End Sub


0
 
abdulvbAuthor Commented:
That is confusing -- the EE interpretation of a single inverted comma seems to end up as a question mark.

0
 
Valliappan ANSenior Tech ConsultantCommented:
I could not locate where is your form instancing code and/or text box control arrays creation part, if you do it in run-time.
0
 
Valliappan ANSenior Tech ConsultantCommented:
Also, it is a good idea, to put a prefix to identify variables/object names, like "frm" for form names, "p" or "g" for global variables, etc.

This makes your code more readable.
0
 
abdulvbAuthor Commented:
I accept that comment. I have still not solved the problem but will continue to find an answer. If I do I will post the result.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.