testing whether a for is a subform or not

G

Guest

Hi,
I have a form that searches a lot of records. It is both a standalone form
when the user wants to search, but when the user wants to modify the records,
it is embedded in another form that can then edit the records. The editing
process involves much more than a simple unlocking of the tables.

I would like to run some extra code depending on whether the form is a
subform or not. I.e i would like to say something like

if Parent.Name = "EditForm" then
do some extra stuff
else
do this instead
end if

the problem is, when there is no parent form, parent.Name returns an error.
I tried using the Nz function, but the method doesnt return null either,
just throws an invalid refrerence error.

The other option that i have thought of is just adding all of the code from
the lookup onto the other form, but i do not want to do this, as the code is
quite large, and then any changes would have to be done twice.

Any help would be much appreciated,
Ben
 
B

Bob Howard

So why not simply trap that error and that'll tell you whether you're a
subform or not... you can then set a global variable in the error routine
that can be tested throughout the module.

Bob
 
A

Allen Browne

If a form is not a subform, referring to its non-existent parent will give
an error, i.e. it's not Null (something you can fix with Nz) -- it's a bad
call. So you need to use error handling to recover.

You could write a function like the one below, and then in your code:
If IsSubform(Me) Then ...

Function IsSubform(frm As Form) As Boolean
Dim varDummy As Variant
On Error Resume Next
varDummy = frm.Parent.Name
IsSubform = (Err.Number = 0)
End Function

If error handling is new, here's an introduction:
Error Handling in VBA
at:
http://allenbrowne.com/ser-23a.html
 

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