Open form from subform

G

Guest

Hi
I have a main form (Company) with one field which is a combo box (unbound),
and a subform (Students). When the user selects a company from the combo box
the Students subform is populated with all the students who work for that
company (3 fields only - the table contains 8). This works great. If a
company is selected and there are no students associated with that company
the Students subform (datasheet view) displays empty (as it should). I want
the user to be able to add full student records. I thought that the user
could choose to add a new record by opening the Student Details form (the
full data entry form) from within this subform. Then repopulate the Students
subform with all students for this company. How do I do this? (Limited
knowlege). Many thanks.
 
G

Guest

Chrissy:

Yes, what you want to do is possible.

Here is what to do -

1 - Add a command button or label to the Company form OR the form header or
footer of the Student subform
2 - Name it 'Add Student' so this text displays to the user
3 - Add this code to the OnClick event of the control you chose to create
DoCmd.OpenForm "YourFormNameHere"
On the Student form -
4 - Add this code to the OnClose event of the form
If IsLoaded("CompanyFormName") Then
Forms!CompanyFormName!SubformName.Requery
End If
5 - Cause the same code to run as when a user selects a company in the combo
box by 'calling' the subprocedure from the OnClose event of the Student form
as follows -
If IsLoaded("CompanyFormName") Then
Forms!CompanyFormName!SubformName.Requery
Call Forms.CompanyFormName.EventName
End If
***There are a couple things to do here. You must set the event procedure
in the Company form you are calling to Public and not Private; just select
this text and type Public. You have to know the name of the event in the
Company form to enter it where I typed 'EventName'. You didn't say, but I
assume you are running code from the AfterUpdate event of the combo box or
from the OnClick event of a command button.

Seth

PS - Just in case you don't have the IsLoaded code it is -

Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet view.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <>
conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top