English 中文(简体)
Hide a column programmatically in MS-Access
原标题:

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?
最佳回答

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).

问题回答

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.

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.

A one-liner approach is using:

forms(fname).Controls(ctrlname).columnhidden = false

where fname is name of your form ctrlname is name of your control





相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

access query to filter and combine count

i have two access tables tableA num count 1 7 2 8 3 9 4 9 5 13 6 6 tableB num count 0 1 1 14 2 12 3 5 4 5 5 11 6 5 how can i create an access query that ...

How to show File Picker dialog in Access 2007?

I want to show a dialog where the user can pick a file, click OK, and then the path to the file will be saved in the database. I have just one problem, I can t figure out how tho show the dialog ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...

Returning row number on MS Access

I have 4 tables, from which i select data with help of joins in select query...I want a serial no.(row number) per record as they are fetched. first fetched record should be 1, next 2 and so on... In ...

热门标签