Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem expanding control array (load not being cooperative)

Status
Not open for further replies.

Kavius

Programmer
Apr 11, 2002
322
CA
I know I am going to feel embarassed when I show this to people, but I can't see what I am doing wrong. I've done this before, and compared this to code that works and can't see a difference.

A am attempting to get a list of files from a user. There is a textbox to enter the path to the file into and when they enter any data into it, I would like for another textbox to appear so they can enter another.

Unfortunately, everytime I attempt to load the next object in the control array I get the error "control array element '1' doesn't exist"
Code:
'*** txtFile_Change ************************************************************
'*
'*******************************************************************************
Private Sub txtFile_Change(Index As Integer)
  If (Index = txtFile.UBound) Then
    If (txtFile.Item(Index) <> "") Then
      AddFile
    End If
  End If
End Sub
'*** txtFile_Change ************************************************************



'*** AddFile *******************************************************************
'*
'*******************************************************************************
Private Sub AddFile()
  Dim i As Long
  
  i = cmdBrowse.Count
  Load txtFile(i)
  LayoutTextBox

End Sub
'*** AddFile *******************************************************************
I realize that the txtFile_Change could use a rework, but this should give the basic idea. I asume I am missing something really obvious since, this looks like every example I've seen online, in books, and my own code.

Any ideas?
 
In the AddFile sub, where is the value of cmdBrowse.Count coming from?


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Oops...

From the control that I deleted from the code to simplify my sample. That should be
Code:
Private Sub AddFile()
  Dim i As Long
  
  i = txtFile.Count
  Load txtFile(i)
  LayoutTextBox

End Sub
 
First ... the ".Count" property usually reports one more than the largest subscript because control arrays are zero-based. You probably want

i = txtFile.Count - 1

Second ... it is possible for an index to be missing. If, for example, you create a control array with 3 members (0, 1, 2) and then delete the second one, you will then have a control array with only two members (0, 2) and attempting to reference txtBox(1).Text will give you an error.
 
I don't think so.

Array element 0 already exists and is loaded, so the array range is currently 0 to 0. I need to extend the array to a range of 0 to 1, so I am trying to load element 1.

Given an array with one elemnent in it (txtFile(0)), I should be able to do:

load txtFile(1)
load txtFile(2)
load txtFile(3)
load txtFile(4)

and end up with a 5 element array.

Or so I thought.

I know there is only one element in the array because this is the first thing I do after rendering the form, and I haven't written the delete functionality yet.
 
Interestingly enough, I just created a blank project and put a text box and a button on a form. It works fine. Here is the code:
Code:
Private Sub Command1_Click()
  Dim i As Long
  i = txtFile.UBound + 1
  Load txtFile(i)
  txtFile.Item(i).Top = txtFile.Item(i - 1).Top + txtFile.Item(i - 1).Height
  txtFile.Item(i).Visible = True
End Sub
I don't know what is going on with my one example, but I think I will just rebuild the form from scratch and see if that fixes it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top