We help IT Professionals succeed at work.

Call two subforms on a Main Form with one buton

Surveyor1
Surveyor1 asked
on
230 Views
Last Modified: 2013-11-28
Experts I have a Main Form which has two (Unbound) Subforms [SFsubform] and [SFsubform1] which call subforms depending on which button is clicked however  each button only calls one subform  (i.e.) button1 on a click event calls a subform to [SFsubform] only and I am looking for a button on a click event to be able to call different subforms to both subforms [SFsubform] and [SFsubform1].

I have attached the code I use for one button 1 subform procedure can someone please show me how i convert it to one button 2 subforms please.

Thanks in advance.
Option Compare Database
Option Explicit
'MainForm and SubForm Visability Control ************
'
Sub closesf(sForm As String)
Me.sfsubform.SourceObject = sForm
End Sub
 
'******Open Mainform SubForm Option1********
'
Private Sub Command104_Click()
Call closesf("CMSMainSubForm1")
 
End Sub

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013

Commented:
Just add a similar line of code to your sub, and one more parameter for the second subform:

Sub closesf(sForm As String, sForm1 as string)
    Me.sfsubform.SourceObject = sForm
    Me.sfsubform1.SourceObject = sForm1
End Sub


Private Sub Command104_Click()
    Call closesf("CMSMainSubForm1", "CMSMainSubForm2")  '<--- Change the name of the second form's source object as needed.
End Sub

Author

Commented:
Hi I have tried your solution but I cannot get to work I keep getting

Compile Error:

Argument not optional

I have attached my final code.  The only other thing that might be causing a problem is that I have more than one button which calls a subform to [SFsubform] and [SFsubform1] which works well when one button opens one subform but not when I try to cahnge code as suggested.

I

Many thanks again in advance for your help.
Option Compare Database
Option Explicit
 
'MainForm and SubForm Visability Control ************
'
Sub closesf(sForm As String, sForm5 As String)
Me.sfsubform.SourceObject = sForm
Me.sfsubform1.SourceObject = sForm5
End Sub
 
''******Open MainFormSubform Start Screen********
'This opens Subform [CMSMainformMain] on subform sfsubform
Private Sub Command123_Click()
Call closesf("CMSMainSubFormMain", "CMSReportsModule")
End Sub
 
'******Open Mainform SubForm Option1********
'This opens Subform [CMSMainformSub1] on subform sfsubform1
Private Sub Command104_Click()
Call closesf("CMSMainSubForm1")
End Sub
 
'******Open Mainform SubForm Option1********
'This opens the Find Project Form on subform sfsubform1
Private Sub Command59_Click()
Call closesf("FindProjectForm")
End Sub
 
'******Open Mainform SubForm Option2********
'This opens Subform [CMSMainformSub2] on subform sfsubform1
Private Sub Command105_Click()
Call closesf("CMSMainSubForm2")
End Sub
 
'******Open Mainform SubForm Option3********
'This opens Subform [CMSMainformSub3] on subform sfsubform1
Private Sub Command106_Click()
Call closesf("CMSMainSubForm3")
End Sub
 
'******Open Mainform SubForm Option4********
'This opens Subform [CMSMainformSub4] on subform sfsubform1
Private Sub Command107_Click()
Call closesf("CMSMainSubForm4")
End Sub
 
'******Open Mainform SubForm Option5********
'This opens Subform [CMSMainformSub5] on subform sfsubform1
Private Sub Command108_Click()
Call closesf("CMSMainSubForm5")
End Sub

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Great Solution !!!! It works :-)  Many thanks for your quick and great help.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013

Commented:
Glad to help :-)

Author

Commented:
Final code for other users to understand how problem was solved !


'*****************************************************************************************************************
'New Mainform Subform window switching code 5th July 2009
'*****************************************************************************************************************
Sub closesf10(sform As String, sformcontrol As String)
Me.Controls(sformcontrol).SourceObject = sform
End Sub
Private Sub Command166_Click()
Call closesf10("CMSMainSubFormMain", "sfsubform")
Call closesf10("CMSReportsModule", "sfsubform1")
End Sub
'This opens Subform [CMSMainformSub1] on subform sfsubform and Reports Module on sfsubform1
Private Sub Command104_Click()
Call closesf10("CMSMainSubForm1", "sfsubform")
Call closesf10("CMSReportsModule", "sfsubform1")
End Sub
'This opens Subform [CMSMainformSub2] on subform sfsubform and Reports Module on sfsubform1
Private Sub Command105_Click()
Call closesf10("CMSMainSubForm2", "sfsubform")
Call closesf10("CMSReportsModule", "sfsubform1")
End Sub
'This opens Subform [CMSMainformSub3] on subform sfsubform and Reports Module on sfsubform1
Private Sub Command106_Click()
Call closesf10("CMSMainSubForm3", "sfsubform")
Call closesf10("CMSReportsModule", "sfsubform1")
End Sub
'This opens Subform [CMSMainformSub4] on subform sfsubform and Reports Module on sfsubform1
Private Sub Command107_Click()
Call closesf10("CMSMainSubForm4", "sfsubform")
Call closesf10("CMSReportsModule", "sfsubform1")
End Sub
'This opens Subform [CMSMainformSub5] on subform sfsubform1 and Reports Module on sfsubform1
Private Sub Command108_Click()
Call closesf10("CMSMainSubForm6", "sfsubform")
Call closesf10("CMSReportsModule", "sfsubform1")
End Sub
'This opens Subform [CMSMainformMain] on subform sfsubform
Private Sub Command123_Click()
Call closesf10("CMSMainSubFormMain", "sfsubform")
Call closesf10("CMSReportsModule", "sfsubform1")
End Sub

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.