Making subform visible when button is clicked

H

hhusted

I would like to know what code I can use for a control button I am
placing in a form so when someone clicks on the button the subform
appears. Until that button is clicked, the subform is not visible.

Thanks for your help.

Harry
 
J

Jeanette Cunningham

hhusted,
On the property dialog of the subform control, set the visible property of
the subform control to No (format tab)
Note: the subform control is on the main form and often has a different name
from the subform inside it.
Be sure to choose the subform control, not the subform inside it.
Code the click event of the button like this:

Me.TheButton_Click()
If Not Me.NameOfSubformControl.Visible = True Then
Me.NameOfSubformControl.Visible = True
End If
End Sub

Replace control names with your control names

Jeanette Cunningham
 
H

hhusted

hhusted,
On the property dialog of the subform control, set the visible property of
the subform control to No (format tab)
Note: the subform control is on the main form and often has a different name
from the subform inside it.
Be sure to choose the subform control, not the subform inside it.
Code the click event of the button like this:

Me.TheButton_Click()
If Not Me.NameOfSubformControl.Visible = True Then
Me.NameOfSubformControl.Visible = True
End If
End Sub

Replace control names with your control names

Jeanette Cunningham

So let me make sure I understand what you say correctly. The button I
placed is near the top of the main form. By coding the control as you
stated the subform will be invisible until the button is pressed? In
other words, by placing visible as set to no, this will set up the
subform to be invisible and not the actual button? The button will
always be visible?

Thanks for your help. i really appreciate it highly.

Harry
 
N

Naeem Azizian

If you do not want the subform to be visible when you open the form
put this in the onload or onopen of your main form. sometimes, you do
some design changes and visible property is set = True and you save it
and then it'll be visible next time. so put the following line in the
onload to make sure it's not visible no matter what's the visibility
condition of the form:
me.NameOftheSubform.visible= False

and in the onClick button of the main form do this:

'if you want the button to visiable the subform by first click and
then hide it with next use the following code
if me.NameOftheSubform.visible = False then
me.NameOftheSubform.visible = True
me.ButtonName.Caption = "Hide"

else
me.NameOftheSubform.visible = False
me.ButtonName.Caption ="Show Subform"

end if
 
J

Jeanette Cunningham

Yes,
if you put Me.TheSubformControlName.Visible = False
the TheSubformControlName will be invisible (hidden)

if you put Me.TheButtonName.Visible = False
the button will be invisible (hidden)

Jeanette Cunningham
 
H

hhusted

Yes,
if you put Me.TheSubformControlName.Visible = False
the TheSubformControlName will be invisible (hidden)

if you put Me.TheButtonName.Visible = False
the button will be invisible (hidden)

Jeanette Cunningham

Where do I place this code:

Me.TheSubformControlName.Visible = False

Does the above code go in the properties of the subform or the button?
That is what I am confused about.
 
H

hhusted

Yes,
if you put Me.TheSubformControlName.Visible = False
the TheSubformControlName will be invisible (hidden)

if you put Me.TheButtonName.Visible = False
the button will be invisible (hidden)

Jeanette Cunningham

By the way, how do I get the subform to appear at the bottom of the
main form? Place the subform in the main form ahead of time and code
it to be invisible?
 
H

hhusted

By the way, how do I get the subform to appear at the bottom of the
main form? Place the subform in the main form ahead of time and code
it to be invisible?

This is the actual code I am using for the button:

Private Sub Option78_Click()
If Me.PerDiem.Visible = False Then
Me.PerDiem.Visible = True
Me.Option78.Caption = "Hide"

Else
Me.PerDiem.Visible = False
Me.Option78.Caption = "Show Subform"
End If
End Sub

When I run the form, I get a compile error. VB states Method or data
member not found.

This is the code I use when I load the main form so the subform is
invisible.

Private Sub Form_Load()
Me.PerDiem.Visible = False
End Sub

Please tell me why I get errors.

Thanks.

Harry
 
H

hhusted

This is the actual code I am using for the button:

Private Sub Option78_Click()
If Me.PerDiem.Visible = False Then
Me.PerDiem.Visible = True
Me.Option78.Caption = "Hide"

Else
Me.PerDiem.Visible = False
Me.Option78.Caption = "Show Subform"
End If
End Sub

When I run the form, I get a compile error. VB states Method or data
member not found.

This is the code I use when I load the main form so the subform is
invisible.

Private Sub Form_Load()
Me.PerDiem.Visible = False
End Sub

Please tell me why I get errors.

Thanks.

Harry

Oh, by the way, the Method or data member not found error stops
at .Caption = "Hide".
 
J

Jeanette Cunningham

hhusted,
Place the subform on the main form in design view and set its visible
property to No (the subform control, not the subform).
To see the difference between a subform control and a subform
--create a new form in design view
--on the toolbox, turn off the wizard
--select the subform tool
--use the tool to put a subform control on the form
--you will see a white rectangle
--the white rectangle is the subform control
--to put a subform in the subform control
--select the subform control
--open the properties dialog
--select the data tab
--click the drop down for Source Object
--choose any other form from the drop down list
--you can now see a form inside the subform control


To put code in an event, open the form in design view.
--select the control you want the procedure to run from (the button)
--open the properties dialog
--select the Events tab.
--select the event you want to use
--in this case, the On Click event
--click on the small command button to the right with the 3
dots.
--when the dialog opens, select Event Procedure.
--the VB editor will then open with the cursor positioned in the event sub.
--enter the code there

Jeanette Cunningham
 
H

hhusted

hhusted,
Place the subform on the main form in design view and set its visible
property to No (the subform control, not the subform).
To see the difference between a subform control and a subform
--create a new form in design view
--on the toolbox, turn off the wizard
--select the subform tool
--use the tool to put a subform control on the form
--you will see a white rectangle
--the white rectangle is the subform control
--to put a subform in the subform control
--select the subform control
--open the properties dialog
--select the data tab
--click the drop down for Source Object
--choose any other form from the drop down list
--you can now see a form inside the subform control

To put code in an event, open the form in design view.
--select the control you want the procedure to run from (the button)
--open the properties dialog
--select the Events tab.
--select the event you want to use
--in this case, the On Click event
--click on the small command button to the right with the 3
dots.
--when the dialog opens, select Event Procedure.
--the VB editor will then open with the cursor positioned in the event sub.
--enter the code there

Jeanette Cunningham

Thank you for that feedback. Now my next question is my client wants
me to create a button that when clicked on will not only make the
subform visible, but also place the cursor at the first field in the
subform. Is there any code I need to place in the button to do that.
Or do I add any code to the subform.

Thanks again.

Harry
 
J

Jeanette Cunningham

Harry,
you don't need any code.
On the subform, remember the name of the control that you want the cursor to
go to first.
Select the details section of the subform.
On the View menu | Tab Order |
--select the control
--drag it to the top of the list
--save

Jeanette Cunningham
 
J

Jeanette Cunningham

On the subform, change the tab order. View | Tab Order

find the control that needs to get focus first and drag it to the top of the
list for tab order.

Jeanette Cunningham
 
H

hhusted

On the subform, change the tab order. View | Tab Order

find the control that needs to get focus first and drag it to the top of the
list for tab order.

Jeanette Cunningham

I have a problem. When I enter data in one field, all fields in
database are populated. This is not supposed to happen. Each field
should be independent of each other. How can I make that happen in the
form?

Thanks.

Harry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top