Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Call two subforms on a Main Form with one buton

Posted on 2009-07-05
6
Medium Priority
?
202 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

0
Comment
Question by:Surveyor1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 24780362
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
0
 

Author Comment

by:Surveyor1
ID: 24780886
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

0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 24781073
Ok, If I'm understanding this correctly, you have two subform controls on your main form - and you want to be able to set either one of them to have any form as a source object - correct?

If that is the case, you would have to change the sub and the way the sub is called in each of your click events.

The sub would have to be changed like this  (I've shown you the syntax for a few here.  You need to do this with all of the calls to closesf:


Sub closesf(sForm As String, sFormControl as string)   '<--- Add sFormControl so that the name of the subform control can be specified
     Me.controls(sFormControl).SourceObject = sForm   '<-- This now sets the souce object of a specified subform control to a specified form
End Sub

''******Open MainFormSubform Start Screen********
'This opens Subform [CMSMainformMain] on subform sfsubform
Private Sub Command123_Click()
       Call closesf("CMSMainSubFormMain", "sfsubform")   '<--- sets the source object of sfsubform to CMSMainSubformMain
       Call closesf("CMSReportsModule", "sfsubform1")     '<--- sets the source object of sfsubform1 to CMSReporstModulei
End Sub

'''' **** Note that since you have added an argument to the sub, you need to include that argument in ALL of the calls to that sub.  ****

'******Open Mainform SubForm Option1********
'This opens Subform [CMSMainformSub1] on subform sfsubform1
Private Sub Command104_Click()
       Call closesf("CMSMainSubForm1", "sfsubform")   '<--- sets the source object of sfsubform to CMSMainSubForm1
End Sub
'******Open Mainform SubForm Option1********
'This opens Subform [CMSMainformSub1] on subform sfsubform1
Private Sub Command59_Click()
       Call closesf("FindProjectForm", "sfsubform")   '<--- sets the source object of sfsubform to FindProjectForm
End Sub
 
 
'******Open Mainform SubForm Option2********
'This opens Subform [CMSMainformSub2] on subform sfsubform1
Private Sub Command105_Click()
       Call closesf("CMSMainSubForm2", "sfsubform")   '<--- sets the source object of sfsubform to CMSMainSubForm2

End Sub
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Closing Comment

by:Surveyor1
ID: 31599911
Great Solution !!!! It works :-)  Many thanks for your quick and great help.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 24781200
Glad to help :-)
0
 

Author Comment

by:Surveyor1
ID: 24781223
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

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question