Checking if form is a sub-form

D

dgy

I designed a form that should only ever be used as a sub-form in another
(parent) form.
It all works well except that I thought I would ensure it is never opened
directly and only ever as part of the parent form.

In the sub-form's Form_open event I thought I'd be able to just check
whether the sub-form's Parent property is Null or Nothing or Empty (or
something) but when I do, I immediately get a 2452 error. I had planned to
check the parent's name if it wasn't nothing (or null or empty).

Is there a clever way to ensure that the sub-form is only ever opened on a
particular form?
I suppose I could do it by trapping for the 2452 error but that doesn't seem
very elegant.

TIA,

David Young
 
A

Allen Browne

Private Sub Form_Open(Cancel As Integer)
Dim varDummy As Variant
On Error Resume Next
varDummy = Me.Parent
If Err.Number = 2352 Then
Cancel = True
MsgBox "This form can only be opened as a subform."
End If
End Sub
 
D

Dirk Goldgar

dgy said:
I designed a form that should only ever be used as a sub-form in another
(parent) form.
It all works well except that I thought I would ensure it is never opened
directly and only ever as part of the parent form.

In the sub-form's Form_open event I thought I'd be able to just check
whether the sub-form's Parent property is Null or Nothing or Empty (or
something) but when I do, I immediately get a 2452 error. I had planned to
check the parent's name if it wasn't nothing (or null or empty).

Is there a clever way to ensure that the sub-form is only ever opened on a
particular form?
I suppose I could do it by trapping for the 2452 error but that doesn't
seem
very elegant.


That's what I would do, and have done in similar cases. Here's a simple
version of a function that works that way:

'----- start of example code #1 -----
Function FormIsASubform(frm As Access.Form) As Boolean

On Error Resume Next

If IsError(frm.Parent) Then
FormIsASubform = False
Else
FormIsASubform = True
End If

End Function
'----- end of example code #1 -----

You'd call it, from the form in question, like this:

If FormIsASubform(Me) Then
...
End If

The only alternative I know of is to go through the Forms collection to see
if any of its members is this form. Here's an alternate form of the above
function:

'----- start of example code #2 -----
Function FormIsASubform(frm As Access.Form) As Boolean

Dim frmMain As Access.Form

For Each frmMain In Application.Forms
If frmMain Is frm Then
FormIsASubform = False
Set frmMain = Nothing
Exit Function
End If
Next frmMain

FormIsASubform = True

End Function
'----- end of example code #2 -----
 
D

dgy

Thanks Dirk (& Allen).

I thought that is what I might have to do.
But I hadn't considered a separate re-usable utility function like your
example 1. Thanks for that - I'll add it to my library of useful little
functions.

David Young
 

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