Visibility

willlobb

Board Regular
Joined
Mar 29, 2002
Messages
103
Hi,

Can anyone tell me how to set the visibility of a field in a form to either true or false using a macro. ie can anyone give me the code?

thanks very much,

Will
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
To show:
FieldName.Visible = True

To hide:
FieldName.Visible = False

You probably want to put this code in the "On Load", "On Open, or " On Current" event of the form.
 
Upvote 0
Hi, thanks very much for your reply. I had tried that though and it just wouldnt work. Maybe I was just putting it in the wrong place...i'll try again!

Thanks again.

Will
 
Upvote 0
Yes, you need to make sure that the code is getting triggered and run in all instances that you want it to. This is usually done by putting the code in one of the events. If you view the Properties of your form, go to the Events tab to see all the possibilities, and click on the Code Builder of a particular event to enter your code.

If you need help, please explain the circumstances in which you want the code to run. Like I said, the most common events I use are "On Open", "On Load", and "On Current".
 
Upvote 0
Ok, thanks again for your reply. I have a 'Main' form with a load of SubForms on it. When the SubForms are being viewed in the 'Main' form, I want only certain fields to be shown. When the SubForms are being viewed as a form in their own right (if you get what i mean?), I want all the fields in the form to be visible.

Is this possible? because if i use the 'OnLoad' or 'OnOpen' events, the fields will hide when the form is loaded in the 'Main' form.

Or can i use the 'OnOpen' event of the 'Main' form to hide the fields of the subforms??

I hope this makes sense!

Thanks again,

Will
 
Upvote 0
In the open event for the subform test to see if the main from is open.

If it is hide the controls you want to.
 
Upvote 0
When the 'Main' form opens, can i set the field visibility property of a subform to false and then set it to true again when the subform is opened on its own?

What would the code be? Form!FieldName.Visibility?? cant seem to get it to work.

Thanks,

Will
 
Upvote 0
Hmmm...

I would be willing to bet there there is way that you can get them to act differently, depending on whether or not the Form is attached to another as a Subform, or is independent, but I am not sure how to do that.

What I would probably do is make a second copy of the form/subform with a different name and set the fields to show in one, but not in the other.
 
Upvote 0
Good idea. I'll do that, but if anyone else knows how to do it using one form could they let me know tho please!

Thanks for that!

Will
 
Upvote 0
Use something like this on the subform:

Code:
Private Sub Form_Open(Cancel As Integer)
Dim frm As Form
    
For Each frm In Forms
    If frm.Name = "Main Form" Then

' put code here to hide/show controls.

        MsgBox "Open"
    End If
Next frm

End Sub


You could also create a function like this:

Code:
Function IsFormOpen(frmName As String) As Boolean
Dim frm As Form
    
IsFormOpen = False

For Each frm In Forms
    If frm.Name = "Main Form" Then
        IsFormOpen = True
    End If
Next frm

End Function
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top