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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to create a control array 1

Status
Not open for further replies.

mike777

Programmer
Jun 3, 2000
387
US
I am trying to create a control array like this:
Code:
dim a_cbtn(1 To 12) As Control, ctl as control
x = 1
For Each ctl In frm.Controls
    If ctl.Name Like "cbtn*" Then
        a_cbtn(x) = ctl
        x = x + 1
    End If
Next ctl

I keep getting "Object variable or with block variable not set". Any help?
Thank you.
michael.kemp@gs.com
 
Controls are objects. Object variables have to be assigned with the Set statement, like this:
Set a_cbtn(x) = ctl

Your statement attempts to assign the default property of ctl (probably ctl.Value) to the default property of a_cbtn(x). But a_cbtn(x) contains Nothing, because you haven't Set it to anything yet. That's why you get the "Object variable or with block variable not set" message. Rick Sprague
 
If this actually creates a control array, It doesn't place them where they may be seen/manipulated. I think that even with the addition of the "SET" (which IS necessary - but Perhaps not sufficient), all that is actually being accomplished is the creation of additional references to the existing controls.

Ms. Access, in my experience does not support actual control arrays, although you can somewhat mimic them.

MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
I am also trying to figure out how to do a control array in Access. You said you can mimic them...please explain. Thanks!
 
Taken from the Access Web

We can create an array of controls, but not a control array. One workaround is to append a number as a suffix to all similar controls. For example, if we had controls named "txtUser1", "txtUser2", "txtUser3"......"txtUser10", then we could refer to all these controls from a loop as

Dim intI as integer
For i=1 to 10
debug.print me("txtUser"& cstr(i)).Name
Next
Regards,
Graham
 
Thanks, Graham! I will see what I can do with this. Appreciate the quick reply. Want to simplify my coding and this will help.
 
From a different perspective, the control array (in "VB") often mis-used and does NOT simpllify, but only adds confusion. Many VB programmers just throw all the textboxes into a (control) array, even though the contents they assign have no real relationship. When anyone attaempts to (later) relate the control to the function, they cannot use the name as even a clue, but must wade through the code to begin to understand what is going on.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Can't argue with that (got the T-shirt!).
B-) Regards,
Graham
 
wait.. I'm confused, I have the following at the top of my form: Dim NewCheckBox as Control

And the following code in my Form_Load section:
Set NewCheckBox = CreateControl(frm.Name, acCheckBox, acDetail, "", "", 200, 200, 200, 200)

NewCheckBox.Visible = True


I am using the Set command, but I am still getting the "Object variable or with block variable not set." error message. Can someone tell me what I'm missing? Thanks!
 
Well usually, I just drop a checkbox onto a form and don't bother about this, but I can see where you got the code in help.
In the help example, it is creating a new form and has defined 'frm' as a new form, which you haven't.
If that's NOT what you are trying to do , then either drop a checkbox onto the form and get rid of the code (set it to true in the properties box) or change the frm.name to either me.name or "actualformname".

HTH
(Not sure what this has to do with control arrays though) Regards,
Graham
 
You're right, I should have put Me.Name in the code instead. Now I'm getting the error "You must be in design view to create or delete controls." Hm. Oh well I guess. Why would the code exist if you can't ever use it? I guess if you're using on a report, you can code in quasi design mode. Thanks anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top