Help Understanding Arrays in For Each Statement (1 Viewer)

Heatshiver

Registered User.
Local time
Tomorrow, 04:46
Joined
Dec 23, 2011
Messages
263
I'm trying to understand how I can make an Array and For Each statement work together. I normally don't use either, but it would be good so I could write less code where plausible. Here is the example I have:

Code:
Dim Named As Variant
Dim Ctl As Control

Named = Array(Me.Namee, Me.ID, Me.Title)

For Each Ctl In Me.Controls
    If TypeOf Ctl Is TextBox Then
        If Len(Ctl) = 0 Then
            With Ctl
                .Visible = False
            End With
        End If
    End If
Next
I am unsure how I include the array in this statement. I have tried replacing "Me.Controls" with the array name, but I get a 424 runtime error (no object). What I am trying to accomplish is for each item in the array I want to make it not visible if it has a null value.

Thanks!
 

DavidAtWork

Registered User.
Local time
Today, 22:46
Joined
Oct 25, 2011
Messages
699
Why do you want to use an array here? Arrays are data containers more suited to holding data elements that can be accessed by their array index, which is a number, unless I'm missing something, why would you want to store a control's visibility state in an array.

David
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:46
Joined
Feb 28, 2001
Messages
27,285
Is it possible that you really meant "collection" when you said "array" ?

A collection is a "virtual" array in that you can select its elements by number - OR by name - OR by the For Each enumeration.

A "real" array - as in "Dim A(1 to 20) as Integer" - is not a collection in that the For Each and "selection by name" enumeration mechanisms don't apply. You are confusing yourself and us by your question.

Did David and I help you sort out your question or did you not tell us the intent of using an array to remember something?
 

Heatshiver

Registered User.
Local time
Tomorrow, 04:46
Joined
Dec 23, 2011
Messages
263
Thank you for the clarification as I do mean collection.

So in a For Each case statement, you would not need to declare the virtual array? What if I had 5 fields and only wanted to go through 3 of them?

Other than that, is my code valid?
 

Heatshiver

Registered User.
Local time
Tomorrow, 04:46
Joined
Dec 23, 2011
Messages
263
Sorry about that, MS Access was giving me some crazy errors and I thought that somehow it was my code. However, the knowledge on the array is much appreciated!

Is there a way to specify a collection of names though?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:46
Joined
Feb 28, 2001
Messages
27,285
You are still confused. You don't create or declare the collection array, Access does that when you open the object that contains the potential collection. You don't set the number of dimensions in a collection, Access does. (OK, technically, MS did that when they coded up the COM interface for ActiveX.)

You can declare a "normal" array using a DIM statement and that can have however many dimensions as required. However, the For Each syntax doesn't work for that type of array (at least, I don't recall that it does.)

Your goal is still not clear enough to know what you are really asking.

Let me expound slightly.

Let's say you have 5 controls on the form, 3 of which are text boxes. Is THAT what you meant? If so, then first, the collection of controls will be set up correctly, and second, by testing the type of control first and only "doing your thing" with controls of the proper type, your sample code is correct at least in concept. Whether it actually does what you want is another question that only you can answer.
 

Heatshiver

Registered User.
Local time
Tomorrow, 04:46
Joined
Dec 23, 2011
Messages
263
Thanks for that.

I guess I was hoping that if I had something like 10 controls, 5 of which were textboxes, I could somehow define (for example) 3 of those texboxes in which to go through and apply whatever values as wanted.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:46
Joined
Feb 28, 2001
Messages
27,285
You do that with code. If the 3 out of (however many) are always the same, you can just code them by asserting the values in some convenient event. The problem is that you usually don't have anything you can to to mark the boxes unless you put something in a property of the box that you can test in code.

Say, for example, that of the 10 controls, 5 are text boxes but only three of them are unbound text boxes. You can test for the controlsource property and set values for the boxes that have null or blank controlsources. You could make the boxes have special border colors and search for that color if you wanted to get tricky with it.

However, if the choice of which three of the boxes get mucked about is somehow variable, perhaps your problem is that you haven't yet defined your problem well enough to express that definition to us - or to yourself. Typically, this kind of problem solves itself with a good enough problem statement.
 

Users who are viewing this thread

Top Bottom