Find out which report or form a control belongs to

Today I had an issue, trying to discover which report/form a given control belongs to. At first I thought it would be so simple. Just use the .Parent property of the control.

However I soon realised that the .Parent does not always refer to the form/report. For a label for instance it refers to the Control to which the label is attached, and for a control such a textbox or combobox placed inside a tabcontrol the .Parent property refers to the page on which the control resides, and the pages .Parent property is actually the tab control  So in some cases to get the form/report you might need to go through several objects to actually get the form/report.

So I wrote this short bit of code, which will go recursively check the parent of the control, until it finds a Form or Report object, and then return that.

Hope someone else might find it useful.



For more information on the TypeOf you can look over here at MSDN

One comment on “Find out which report or form a control belongs to
  1. Ben says:

    Excellent concept! It’s great when recursion can be used to simplify a task.

Ask a question, leave some feedback, post a reply!

This site uses Akismet to reduce spam. Learn how your comment data is processed.