How do I check if a form is a opend as a subForm?

marlan

Registered User.
Local time
Today, 21:03
Joined
Jan 19, 2010
Messages
414
Hi

I would like to visualize som controls according to a form being opend as a main form or a sub form.

my code rases an error if the form is a mainForm:
Code:
Private Sub Form_Load()
    If Len(Nz(Me.Parent, ""))=0 Then 'Some Code...

Thank You!
 
How about using Error Handling to catch the error?
 
I wish the Form.Parent property returned "Nothing" for a main form, but alas. What I commonly do is re-expose it with a custom property like . . .
Code:
property get ParentForm as form
on error resume next
  set ParentForm = me.parent
end property
. . . so now other code on my form can check for the nothing . . .
Code:
   if not me.ParentForm is nothing then 
[COLOR="Green"]      'some code[/COLOR]
   end if
. . . and my custom property handles the error.
 
Hi, and thank you all,
I see Error handeling is the only way, Error handeling it will be...

Thank you!
 
If a Form is opened then it is added to the Forms collection, but not so for a SubForm. Maybe you can take advantage of that fact.
 
i just use error handling

Code:
dim p as form
on error go to noparent
set p = me.parent
set p =nothing
etc
exit sub
 
noparent:
etc

this issue is always a bit irritating

eg testing for existence of a table with

exists = len(tabledefs("sometable").name)>0

also needs error trapping as well.
 
Hi Gemma,
here is code I found to knlow if an item is in a collection:
Code:
Function IsInCol(oCollection As Object, stName As String) As Boolean
' Checks if item is in collection.
'Found @ http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/
 
    On Error Resume Next
    IsInCol = Not oCollection(stName) Is Nothing
    
End Function

Glad I can help you for once...:)
 
Hi Gemma,
here is code I found to knlow if an item is in a collection:
Code:
Function IsInCol(oCollection As Object, stName As String) As Boolean
' Checks if item is in collection.
'Found @ http://www.dailydoseofexcel.com/archives/2005/06/07/isin-collection-or-array/
 
    On Error Resume Next
    IsInCol = Not oCollection(stName) Is Nothing
 
End Function

Glad I can help you for once...:)


thanks - that's the same idea that lagbolt posted above. It still needs error handling though. you would think there would be a way of doing this that doesn;t need error handling.
 
Last edited:
Hi again,

So, lets go in the same direction, and check if a "live" form has a parent:

Code:
Function isSubForm(MyForm As Form) As Boolean
On Error Resume Next
    isSubForm = (Len(MyForm.Parent.Name) > 0)
End Function
and call it like this:
Code:
If isSubForm(Me) Then 
'Do somthing...
End If
Tested, should work...

All the best!
 
Last edited:
If a Form is opened then it is added to the Forms collection, but not so for a SubForm. Maybe you can take advantage of that fact.

My thoughts exactly. The below code in OnOpen or OnLoad of the form/subform "myForm" does it:

Code:
    If SysCmd(acSysCmdGetObjectState, acForm, "myForm") = 0 Then
        MsgBox "myForm opened as subform"
    Else
        MsgBox "myForm not opened as subform"
    End If
 
were you trying to get rid of the error handler, because it's still there!
 
Hi everybody, and thanks for your help!

Yup Dave, I guess lagbolt's idea is the best, have a routine that does the work and handles the errors.
I don't like the idea of using error-handeling as method, but If it works so nicely for collections, lets use it for collections.
 
The problem, in my opinion, is with Access. The Access.Form.Parent property should return Nothing for a stand-alone form, but in fact an error is raised when the parent is not present. There is no choice left then, but to handle that error, and there is no other setting you can check.

Also, if you want more bells and whistles than you get using a VBA.Collection, you can take a look at a Scripting.Dictionary which does expose an Exists() method.
 
I tend to use a bit of code, like the example below, to stop a form being opened up if it doesn't have a parent ...

Code:
Public Function Form_Open (Cancel AS Integer)
  dim parentName as String
  On Error Resume Next
  parentName = "" ' Give it a known default value
  parentName = Me.Parent.Name
  Cancel = (Len(parentName) = 0)
End Function
 
Also, if you want more bells and whistles than you get using a VBA.Collection, you can take a look at a Scripting.Dictionary which does expose an Exists() method.

Hi lagbolt, how would I use Scripting.Dictionary, I would have to create one, and copy all of VBA.Collection Items to it? What could go wrong if my errors are handeld correctly? what is bad about ussing error handeling as a method?
 

Users who are viewing this thread

Back
Top Bottom