Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programatically Change Subforms within Main Form

Status
Not open for further replies.

JohnSteven

Technical User
Oct 30, 2002
27
I have a database that has one Main Form and I would like to have four different Subforms, calling up each Subform with it's own Command button. Is this scenario possible???

Please Post back, so other can benefit too.

Later
JohnSteven

"The only stupid question is the one you don't ask"
 
John, this is very simple to do.

Place ALL FOUR forms on the main guy. You can even lay them right on top of each other. Set the VISIBLE property of each of them to NO to start with.

I have an example of this here:


On your command button, simply reset the VISIBLE property of the appropriate subform.

Me!MySubForm1.FORM.Visible = true

The only thing you may want to consider is the "making them invisible" again thing. Do you want another subform opening to CLOSE the first one? So they don't stack around and over each other?

Then put code in the buttons to make all four forms INVISIBLE to start, and then VISIBLEize the wanted form.

Another thing to consider is that the data for a subform will be gathered during the main form's opening process - so there MAY be some discernable delay while the main form opens, 'cause JET has to go out and collect four sub-form's worth of data, even if/when those forms are invisible and do not have the focus or anything like that.

HTH

Jim





Me? Ambivalent? Well, yes and no....
Another free Access forum:
More Access stuff at
 
Hi,

You can, but I have a quick question - are you refering to subforms or another form?

If you are refering to subforms then you would have to create all 4 subforms in your main form and on your form's "On current" event do this:

Me!Subform1.Visible = False
Me!Subform2.Visible = False
Me!Subform3.Visible = False
Me!Subfomr4.Visible = False

Then you would create 4 buttons. Each button would use the "On Click" event and would do this:

Button 1:
Me!Subform1.Visible = True
Me!Subform2.Visible = False
Me!Subform3.Visible = False
Me!Subfomr4.Visible = False
Button 2:
Me!Subform1.Visible = False
Me!Subform2.Visible = True
Me!Subform3.Visible = False
Me!Subfomr4.Visible = False
Button 3:
Me!Subform1.Visible = False
Me!Subform2.Visible = False
Me!Subform3.Visible = True
Me!Subfomr4.Visible = False
Button 4:
Me!Subform1.Visible = False
Me!Subform2.Visible = False
Me!Subform3.Visible = False
Me!Subfomr4.Visible = True

HTH,

jbehrne

If at first you don't succeed, call in an airstrike. - Murphy's Laws of Combat Operations
 
Thanks for the replies - I'll give it a go and let you know my progress.....

Thanks again for the info..........

Please Post back, so other can benefit too.

Later
JohnSteven

"The only stupid question is the one you don't ask"
 
If you want to use less code, try the following:

#1 Create a function with the following code....

Public Function SetControls(myselect As Byte)

Dim i As Byte
For i = 1 To 4
Me.Controls("text" & i).Visible = (i = myselect)
Next i

End Function

#2 In your Current event of the form, place the following code...

Setcontrols 5 ' All controls will be hidden, no match.

#3 In the click event of each command button, place the following code...

Setcontrols <the number of the object you want visible>

EXPLANATION:

The visible property will set itself based on the true/false condition of the value passed to the function. If a false is returned by the expression, the control (ie. subform) will be set to false, or true if the numbers match.

Less code, less work, and reusable for higher volume of controls. The same principle can be applied to setting the &quot;state&quot; of a form and its controls. Yes, you could use a sub instead of a function, but who cares.

Hope this makes things easier and for future endeavors.

Gary
gwinn7e
A+, N+, I+
 
I forgot to mention, you will need to change the control name references. This was obvious, I know, but I wanted to make sure I wasn't accused of providing bad code!

:)

Gary
gwinn7
A+, N+, I+
 
Hi again,

Thanks WildHare and jbehrne. I'm using the info both of you proposed and it works great. jbehrne's idea about setting the other subforms to false when triggering the current subform is a very simple approach to what I thought was going to be a problem.

Thanks again for the GREAT info..........

Please Post back, so other can benefit too.

Later
JohnSteven

&quot;The only stupid question is the one you don't ask&quot;
 
Hi gwinn7n,

Thanks for the reply. I'll give your Idea a shot tomorrow. It looks like it could simpler, but I'll find out when my head is a little clearer...

Thanks again..........

Please Post back, so other can benefit too.

Later
JohnSteven

&quot;The only stupid question is the one you don't ask&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top