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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I show/hide subform based using checkbox on mainform

Status
Not open for further replies.

03SCBeast

Programmer
Jan 26, 2005
36
0
0
US
MainForm = Me
SubForm = frmFCPSYCHMEDS
Checkbox = FC_PsychMeds


This code below works fine for showing/hiding the subform based on whether cbxFC_PsychMeds is checked. The problem is when I check cbxFC_PsychMeds for one record, it makes the SubForm appear for all records rather than the related record. I want the SubForm to be visible only on the records where the checkbox is checked.

Private Sub Psychotropic_Meds_AfterUpdate()
If Me.[FC_PsychMeds] = -1 Then
Me.frmFCPSYCHMEDS.Visible = True
Else
Me.frmFCPSYCHMEDS.Visible = False
End If
End Sub

 
How are ya 03SCBeast . . . . .

Try this:
[ol][li]Replace the code in [blue]Private Sub Psychotropic_Meds_AfterUpdate()[/blue] with:
Code:
[blue]   Call SubFrmViewCtl[/blue]
[/li]
[li]In the [purple]OnCurrent[/purple] event of the mainform, copy/paste the same as above:
Code:
[blue]   Call SubFrmViewCtl[/blue]
[/li]
[li]In the [blue]code window[/blue] of the mainform, copy/paste the following routine:
Code:
[blue]Public Sub SubFrmViewCtl()
   
   If Me!FC_PsychMeds Then
      Me!frmFCPSYCHMEDS.Visible = True
   Else
      Me!frmFCPSYCHMEDS.Visible = False
   End If

End Sub[/blue]
[/li][/ol]
Its the [purple]OnCurrent[/purple] event that controls the subform record by record of the mainform . . .

Calvin.gif
See Ya! . . . . . .
 
Thanks TheAceMan1...code works like a charm even though I am completely lost as to how it actually works...will have to go back and study your code some more.
 
03SCBeast . . . . .

[ol][li][blue]Private Sub Psychotropic_Meds_AfterUpdate()[/blue] occurs everytime you change state of the checkbox. Each time you click the checkbox is changing from On to off and viceversa.[/li]
[li]The [blue]OnCurrent Event[/blue] of a form occurs each time you change record![/li]
[li]Both events above need to do the same thing: [blue]Change the visibility of the subform, depending on the state of the checkbox.[/blue][/li]
[li][purple]I simply took your origional code and put it in a common routine that can be called by both.[/purple] Otherwise both events would have the exact same code![/li][/ol]
This is a good example of using a common routine. Later on in your development, you may come up with other code that needs to set the subform visibility in the same way. Instead of writing the code again, you just call the common routine!

Calvin.gif
See Ya! . . . . . .
 
TheAceMan1...thanks for taking the time to explain your code. I've learned so much from you guys since joining this forum. I will be looking at that common routine function as I'm sure I'm duplicating a lot of code unnecessarily.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top