I really have a BIG problem in making control arrays.
The following is what i fed to the PC
sub form_load()
const n=15
dim Sam(n) as textbox
for n=1 to 15
sam(n).text="SamY"
next n
end sub
It won't work.

Dani AI

Generated

Short summary and practical fixes for the control-array trouble above.

Declaring an array of TextBox variables only makes space for references — it does not create controls. Trying to read or write a member of an element that still points to Nothing produces the runtime error 91 you saw. and pointed to two correct ways to get real controls at runtime (dynamic creation or duplicating a design-time control), and ’s original bug also shows why you should never reuse a constant name as your loop variable.

Checklist of things to verify (step-by-step):

  • Add Option Explicit at the top of the module so typos and undeclared names are caught. Use distinct loop variables (i, j) instead of reusing other identifiers.
  • Make sure your loops match the array bounds. Use LBound/UBound or explicitly set Option Base so you don’t get off-by-one errors when you try to access index 11 but your array only goes to 10.
  • When generating control names by concatenating numbers, avoid ambiguous collisions. If you glue two numbers together the pair (1,11) can produce the same text as (11,1); include a separator or fixed-width formatting so every cell name is unique.
  • Before using an array element, test whether it is Nothing (and only then assign properties). Also check the Controls collection for an existing name before attempting to add another control with the same name.

Notes on events and spreadsheet needs:

  • If you want events from many runtime-created controls, either create a design-time control array (so the framework gives you an index in the event) or use a small class module with WithEvents to route events for each instance.
  • For anything resembling a spreadsheet, consider using a grid control (MSFlexGrid/DataGrid or a third-party grid). A grid handles cell navigation, editing and performance much better than hundreds or thousands of individual TextBoxes.

Debugging tips: print loop indices and names to the Immediate window while creating controls; stop at the first failure and inspect whether the array element is Nothing or whether a name collision occurred.

Recommended Answers

All 7 Replies

hmm got it half-fixed:

sub form_load()
const n=15
dim Sam(n) as textbox
for a=1 to 15
sam(n).text="SamY"
next a
end sub

But now it's coming up with something being wrong with the 'sam(n).text="SamY" line.

The error is:

vb6 runtime error 91: Object Variable Not Set

That's exactly what i got.
I was trying to create something like a spread sheet.

What have you got on the form itself? The names of the objects would be helpful too.

Hi

Yuo can not create a control just like that. You have to set the control to something. That is the error that you are getting.

This will do the trick:

Dim sam(15) As TextBox
    
    For i = 1 To 15
        Set sam(i) = Controls.Add("vb.TextBox", "sam" & i)
        sam(i).Visible = True
        sam(i).Text = sam(i).Name
        sam(i).DragMode = 0
        
        If i <> 1 Then
            sam(i).Left = sam(i - 1).Left + sam(i - 1).Width
        End If
    Next i

the last part of the code is so they don't overlap and you can see them all next to each other.

regards

You could also use load... which works just as well. Start a new project, and add a textbox... leave it's name alone (Text1). In The Properties Box, change index to 0. Now, in your form load (or in a button) add this:

For I = 0 To 9
    If I <> 0 Then
        Load Text1(I)
        Text1(I).Top = Text1(I - 1).Top + Text1(I - 1).Height
    End If
    
    Text1(I).Visible = True
Next I

Thanx again it worked. You guyz are really helpful!!!!!!!!!

ok the code for spread sheet is

sub main()
dim sam(10,12) as textbox
for i=1 to 10
for j=1 to 12
set sam(i,j)=controls.add("vb.textbox","sam" & i & j)
' the the code to prevent em' from overlaping
next j
next i
end sub

that code has an eeror when you reach 11 for i and 11 for j
but i want to try other new like after that you could
dim othe textboxes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.