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

For Each through an array 1

Status
Not open for further replies.

GKChesterton

Programmer
Aug 17, 2006
278
US
I want some fields to become visible. I want to set the list of those fields in primitive fashion, using Array function. The part in red seems to be hard for me to get right.

Code:
Private Sub ShowFields()
    Dim ControlNames As Variant
    ControlNames = Array("textbox1", "textbox5", "textbox6")
    
    For Each [COLOR=red][element][/color] In ControlNames
        FieldName = ControlNames()
        Me![FieldName].Visible = True
    Next
End Sub

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
Can not be done as you suggested. For each works with a collection not an array. You will have to do it the old fashion way

dim i as integer
for i = 1 to 5
me.controls("textBox" & i).visible = true
next i
 
For each works with a collection not an array
Really ?
Code:
Private Sub ShowFields()
    Dim ControlNames As Variant, myElement As Strting
    ControlNames = Array("textbox1", "textbox5", "textbox6")
    For Each myElement In ControlNames
        Me.Controls(myElement).Visible = True
    Next
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I believed there was a simple way, and PHV's post looks right. And an array is a collection, I feel sure!

Unfortunately I can't see the result, because I have just, for no apparent reason, incurred the infamous error "The expression On Current you entered as the event property setting produced the following error: Object or class does not support the set of events." So I will let the computer think about it while I sleep, and in the morning download the .ocx file or whatever it is the Internet persuades me to do to fix this, since a decompile won't.

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
Okay, no real solution to my Error in Red, but after lots of Google, fiddling with the Library References, an .ocx download and a gratuitous foray into RegEdit, I rebuilt the form from scratch and VBA now works.

This gets me a "Subscript out of range" error:
Code:
    Dim ControlNames As Variant, myElement As Variant
    ControlNames = Array("rectFailure", "listDisposition", "listDisposition")

    For Each myElement In ControlNames()
        Me.Controls(myElement).Visible = True
    Next
Suggestions?

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
Does the current form (Me) have a control named rectFailure and another one named listDisposition ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ah! At one point, that was probably the issue. Later, an extra parentheses I'd tried inserting was the problem:
For Each myElement In ControlNames()
Many thanks and stars ahoy.

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top