all fields visible false

crowegreg

Registered User.
Local time
Yesterday, 22:45
Joined
Feb 28, 2011
Messages
108
Hello, What is the most efficient code to set all fields within a from visible = False? Thanks for your assistance!!
 
I suspect there will be more to it, but:

Code:
  Dim ctl As Control

  For Each ctl In Me.Controls
    ctl.Visible = False
  Next ctl
 
Thanks for the quick answer. I tried that code, but I receive an error. I don't have any controls on this form, Presently, my form only has labels and textboxes. I've tried making changes to this code, but I'm stuck.
 
Labels and textboxes are controls. What exactly do you want done, and what exactly was the error?
 
When the user opens the form, I don't want the fields to be displayed. I want them to select either to add a new item or update an existing item. Then after their selection, I will make visible the fields they need.

The error message is: run time error '2455'.
You entered an expression that has an invalid reference to the property Visible.

Thanks again for your assistance!
 
I suspect there's something on the form that doesn't have a Visible property. What I would probably do is use the tag property of the controls you want hidden. Put

HideMe

in the tag property of any textbox or label you want hidden, and try this code:

Code:
  Dim ctl As Control

  For Each ctl In Me.Controls
    If ctl.Tag = "HideMe" Then
      ctl.Visible = False
    End If
  Next ctl
 
THANKS!! So I know in the future, will controls refer to every item on the form?
 
Yes, Textboxes, Checkboxes, Comboboxes, Listboxes, Options Groups, Tab Controls, Command Buttons, Option Buttons, Toggle Buttons are all Controls. In Form Design View anything you can click on and place on the Form is a Control.

And as demonstrated here, all Controls do not have the same Properties! Setting the Values of all Controls to anything will pop an error, if there are Labels on the Form, because Labels do not have the Value Property. Likewise, setting the Caption Property to all Controls won't work, if you have Textboxes or Comboboxes, etc, on the Form, because they don't have Captions.

To use code, for instance, like this

Code:
Dim ctl As Control

  For Each ctl In Me.Controls
    ctl.Value = Null
  Next ctl
to set all Textboxes and Comboxes in a Record to Null, you have to tell Access to only do this for Controls that are Textboxes or Comboxes, you'd modify it to something like this:
Code:
Dim ctl As Control

  For Each ctl In Me.Controls
   If (TypeOf ctl Is TextBox) or (TypeOf ctl Is ComboBox) Then
    ctl.Value = Null
   End If
  Next ctl
Now Access will only assign a Value if the Control is a Textbox or a Combobox.

Paul's approach of using the Tag Property to pinpoint which Controls to deal with is also quite valid, of course. Sometimes I use one method, sometimes the other, depending on how many Controls I'm working with and what I'm doing.

Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom