Can a form know if it is being used as a subform ?

R

Richard

For development and debugging purposes,

Is it possible for a form to use the Parent property to test if it is
being used 'stand-alone' versus embedded in another form ?

Question arise in part for this situation.

A form dynamically lays itself out vertically based on the row counts
of several subforms.
When a record of a subform is deleted, the parent form should run its
dynamic layout again.

I could swear this was working, but it is not now (sandbox issues
maybe?)

private sub Form_Load
subform1.form.onAfterDel = "=Parent.Form.ComputeLayout"
subform2.form.onAfterDel = "=Parent.Form.ComputeLayout"
subform3.form.onAfterDel = "=Parent.Form.ComputeLayout"
subform4.form.onAfterDel = "=Parent.Form.ComputeLayout"
subform5.form.onAfterDel = "=Parent.Form.ComputeLayout"
end sub

Public Function ComputeLayout()
....
.... algorithmically layout subform controls based on record counts
....
end sub

When a record is deleted, Access throws a dialog "The expression
AfterDelConfirm you entered as the event property setting produced the
following error: Microsoft Access can't find the field 'ComputeLayout'
referred to in your expression"

In an experiment I had .onAfterDel is "=MsgBox(Parent.Form.Caption)"
and things worked fine.
 
G

George Nicholson

Simply try to get Parent.Name. If an error occurs, its not a subform. In a
general module:

Public Function IsSubForm(frm As Form) As Boolean
' Using the Parent property, this checks to see if the
' specified form is being used as a subform.
Dim strNameRef As String

On Error Resume Next
strNameRef = frm.Parent.Name
IsSubForm = (Err.Number = 0)
Err.Clear
End Function

Note the function argument is an object reference, not the name of a form
(since that would be useless in the case of a subform).

HTH,
 
A

Anthos

Also make sure the Function on the parent form is a public sub.
Otherwise, it can't be called from an external element (i.e. another
form)
 
R

Richard

Simply try to get Parent.Name. If an error occurs, its not a subform. In a
general module:

Public Function IsSubForm(frm As Form) As Boolean
' Using the Parent property, this checks to see if the
' specified form is being used as a subform.
Dim strNameRef As String

On Error Resume Next
strNameRef = frm.Parent.Name
IsSubForm = (Err.Number = 0)
Err.Clear
End Function

Big help! Thanks!
 

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

Top