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

Open a SubForm with a Command Button

Status
Not open for further replies.

GTLoco99

Programmer
Sep 2, 2003
59
US
Hi all,

I have a form that has a command button on it. I want this button to open another form and make it a subform to it.


Then names of the two forms is Program_Entry and Program_Cost

What i have so far is this:

The command button on Program_Entry would have the following code

DoCmd.OpenForm "Program_Cost"

The subform (Program_Cost) would have this as an Form_Open()

Dim FrmA as SubForm
Dim FrmB as Form

Set FrmA = Forms("Program_Cost")
Set FrmB = Forms("Program_Entry")

FrmA.LinkChildFields = [Record Number];[Speaker ID];[Speaker]

FrmA.LinkMasterFields = [Record Number];[Speaker ID];[Fee]

My Question is how do i make the subform (Program_Cost) understand that the Form (Program_Entry) is the Parent Form

If I confused you or you need more information, just ask

Thanks
GTLoco
 
A subform is only a subform when it is in another form.
It can also be opened by itself.

so you need to approach it the other way
The Main form needs to open a particular subform when you click a button or something.

Now you could try this.
Have a text box on the main form that the subforms recordsource is looking at.
Select * From YourTable Where something = forms![mainform].form!textbox1

But that will only work if the subform is opened in the same main form.

You could have two different forms that both have the exact same thing but each has a different subform then tell the user to click this button to do this and click that button to do that.


DougP, MCP
 
As Doug says, a subform is defined as a form embedded another form, so you can't open it, then embed it, however, if you already have a subform embedded on a form (say frmForm1 is embeded on frmMain in a control called frmContent, but want to change frmForm1 to frmForm2 then this works:

Forms!frmmain!frmContent.SourceObject = "frmForm2"

Download the full Accessweb database from to see this in action.

hth

Ben

----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
I figured out how to do this, here is the code if anyone finds its useful. Basically what i have done is code what access does when it opens a subform.

Code is here:
____________________________________________
Private Sub Form_Load()

'your Forms
Dim FrmA As Form
Dim FrmB As Form

'linking fields In this Case 3 fields have similar data

'Form A fields
Dim RecordNumA As TextBox
Dim SpeakerIDA As TextBox
Dim SpeakerFeeA As TextBox

'Form B Fields
Dim RecordNumB As TextBox
Dim SpeakerIDB As ComboBox
Dim SpeakerFeeB As TextBox

'Define Forms
Set FrmA = Forms("Program_Cost subform") 'Subform
Set FrmB = Forms("Program_Entry2") 'Parent Form

'Define SubForm Fields
Set RecordNumA = FrmA.[Record Number]
Set SpeakerIDA = FrmA.[Speaker ID]
Set SpeakerFeeA = FrmA.[Speaker]
'Define Parent Form Fields
Set RecordNumB = FrmB.[Record Number]
Set SpeakerIDB = FrmB.[Speaker]
Set SpeakerFeeB = FrmB.[Fee]

If FrmB.CurrentView = 1 Then 'If Parent Form is Open

GoTo Continue_Sub 'Continue
Else 'Else Form is Closed
MsgBox "Parent Form Closed", vbCritical, "ERROR!"
GoTo Close_Sub 'Exit

End If

Continue_Sub:

If RecordNumA.Value = 0 Then 'If This field is 0, meaning new record
RecordNumA = RecordNumB 'Set these fields =
SpeakerIDA = SpeakerIDB 'Set these fields =
SpeakerFeeA = SpeakerFeeB 'Set these fields =
Else
If RecordNumA.Value = RecordNumB.Value Then 'Else Check Value to see if it matches Parent
SpeakerIDA = SpeakerIDB 'Set these fields =
SpeakerFeeA = SpeakerFeeB 'Set these fields =
Else
MsgBox "RECORDS DO NOT MATCH", vbCritical, "Error!" 'If they dont match, return error
GoTo Close_Sub 'And Exit
End If
End If

GoTo Close_Sub:

Close_Sub:

End Sub
___________________________________________________

If you add a record, add the following code to Form_Next()

RecordNumA = RecordNumB 'Set these fields =
SpeakerIDA = SpeakerIDB 'Set these fields =
SpeakerFeeA = SpeakerFeeB 'Set these fields =

This will duplicate the functions of a subform, but in a seperate form instead. This may be useful if you are cruched for space on a form like I am

Thanks all for the help

GTLoco

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top