views:

383

answers:

3

I want to hide or show a column based on variable data from a users selection. How do you set a column to hidden in MS-Access 2003?

For Example,

After user change event...

For Each ctl In Me.FormNameHere.Form.Controls
    If (TypeName(ctl) = "Textbox") Then
        If InStr(GetTextList(), ctl.Name) > 0 Then
            ctl.hidden = True
        Else
            ctl.hidden = False
        End If
    End If
Next ctl
  • What is the best approach to this type of challenge?
  • Is there a more obvious solution?
+7  A: 

Hi, I answered a similar question to this not long ago to do with hiding columns on a datasheet. However you seem to want to hide textboxes arranged in a column on a form, is that correct?

Iterating over all the controls in the form could be slow if you have many controls. If you really need to use textboxes like that, then you could try group the 'columns' together then hide the groups. Another approach would be to use a listbox or datasheet to represent the data, where you can alter the layout of the columns directly.

Dale Halliwell
Thanks for the tip. I have a finite number of controls all of which are textboxes so thus far the performance is not an issue.
Curtis Inderwiesche
If you had an infinite number of controls, hiding them would be the least of your problems. =)
JohnFx
+4  A: 

Controls do not have a "hidden" property (no objects in Access have a hidden property). They do have a .Visible property.

For future reference, I suggest you familiarize yourself with the Object Browser in the VBE -- open the VBE and hit F2. You can then restrict your search to the individual libraries used in your project. It does take a while to get to the point where you understand the object model, though.

Also, you can rely on Intellisense to learn the properties/methods of an object, so in the code of the form you're working with, you can type "Me.MyTextBox." and the Intellisense dropdown will show you all the properties and methods of that particular control. It doesn't work for a generic control variable (as in your code) because different control types have different properties.

And, of course, the properties sheet gives the names of the properties, even though in code they don't always use the same orthography (usually they are the same with spaces removed).

Also, there are differences in how you might want to do this depending on whether it's a regular form or a datasheet form. In a datasheet, your controls also have .ColumnHidden and .ColumnWidth properties (setting those in any view other than datasheet view has no effect, and neither of those properties are available in the standard property sheet, but changes to them are retained when you save the form).

David-W-Fenton
Well, I found a reference to ctl.ColumnHidden = True which seemed to work well. Thanks for the idea of using the Object Browser, but as you already mentioned, this was not available in my case since I was iterating over several different objects using the ctl variable.
Curtis Inderwiesche
.ColumnHidden applies only to Datasheet view. .Visible applies to all views. The object browser would have done the job if you'd looked for the properties of a particular control type, e.g., TextBox. It's Intellisense that can't work for a variable of generic type Control.
David-W-Fenton
Consider credit given.. +1, +Answered. However, I find it interesting someone will see a question worthy of answering, however not vote on the question itself.
Curtis Inderwiesche
I didn't know I was supposed to vote on the questions. I give credit for answers by voting on those. I can't see the point of voting on a question, up or down.
David-W-Fenton
This tells other readers if you thought the question was a good question.
Curtis Inderwiesche
Hardly any of the questions are good questions. Indeed, most of them are questions that I've a bazillion times in various Access forums over the years. By not voting I'm saying "this is not a *bad* question."
David-W-Fenton
+2  A: 

I found the ColumnHidden property does the trick.

For Each ctl In Me.FormNameHere.Form.Controls
    If (TypeName(ctl) = "Textbox") Then
        If InStr(GetTextList(), ctl.Name) > 0 Then
            ctl.Columnhidden = True
        Else
            ctl.Columnhidden = False
        End If
    End If
Next ctl

I got a hint from this related question.

Curtis Inderwiesche
I gave you .ColumnHidden as one option in my original answer so why don't I get credit for that?
David-W-Fenton