473,382 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,382 developers and data experts.

How to find out which report or form a control belongs to

TheSmileyCoder
2,322 Expert Mod 2GB
Problem
Today I had an issue, trying to discover which report/form a given control belongs to. I needed to know this for some other code I am using.

At first I thought it would be so simple. Just use the .Parent property of the control.

More problems
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.


Solution
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.
Expand|Select|Wrap|Line Numbers
  1. Public Function GetControlParentObject(ctl As Control) As Object
  2. '---------------------------------------------------------------
  3. '---------------------------------------------------------------
  4. ' Procedure : GetControlParentObject
  5. ' Author    : AEC - Anders Ebro Christensen / TheSmileyCoder
  6. ' Date      : 2013-04-12
  7. ' Version   : 1.0
  8. ' Purpose   : Return the Form or report to which a control of any type belongs.
  9. '---------------------------------------------------------------
  10. '---------------------------------------------------------------
  11.    If (TypeOf ctl.Parent Is Form Or TypeOf ctl.Parent Is Report) Then
  12.       Set GetControlParentObject = ctl.Parent
  13.    Else
  14.      'Call self recursively on parent object
  15.       Set GetControlParentObject = GetControlParentObject(ctl.Parent)
  16.    End If
  17. End Function
Hope someone else might find it useful.

Feedback, comments and discussions are always welcome.

If you did find this useful and want to see more like it, please take the time to drop a comment here.
Remember comments and feedback are the fuel that keeps the fire going.
Apr 12 '13 #1
2 3394
Lysander
344 Expert 100+
I can't imagine how I would use this or why, but obviously you had a need, and this solution is simple and elegant. Love it.
Apr 16 '13 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Hi Lysander
Stephen Leban has a bit of code that can be used to estimate the size of a textbox. Parts of his code needs to get to the form/report handle, and in its original form it did not take into account unbound labels, or controls nested within other controls (such as a textbox in a tab control).

If you have looked at my Progress Monitor it also uses the above code to get the form object of a control, as part of a resizing process, in which controls beneath the expanding control are moved (similar to how reports object can grow). Yes you could workaround the issue by also passing the form as a parameter but this seemed more elegant (at least to me).
Apr 19 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Rajeev | last post by:
I have a assembly which creates a form with a set of controls.Is there any way we can find out from which assembly the form/control is created. control.GetAssembly will usually give the assembly ...
1
by: Peter | last post by:
Dear all, Could you give me a hint? a) I generate a report from a query table, then I want to use this report to show the total numbers of the customers who buy the goods. There is a string...
2
by: JollyK | last post by:
Hi friends, This is my question.... From the Page Load event (or Page Init event), I would need to find which event had occurred that caused a PostBack, for example was it a event fired from...
0
by: Marc Gravell | last post by:
Is there any (good) way to stop the designer trying to load a specific form/control when I double-click it in the solution explorer? Basically, I have some controls that either use generics or are...
0
by: neeraj | last post by:
Hi people's I developed One Windows application with some crystal Report I used my predefined crystal reports files for my application. I just take one crystal report viewer on form and set...
0
by: aakash | last post by:
Hello Guys I am upsizing ms access project to give it a ms sql connectivity I am having problem in accessing form control values in ms sql function CREATE FUNCTION "ReportList DateRange"() ...
17
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I get the value of a form control? -----------------------------------------------------------------------...
3
by: Dave Hughes | last post by:
Hi, I've run across a problem styling form controls (quel surprise), or rather in this case, "unstyling" them. I'm trying to put together a theme for a web application which has a fairly...
12
by: Sol | last post by:
Hi gurus, I work for an oil company and need to fix an old DB, which used to work once. It will help us poor operators to do a safer job. What it does: When maintenance on a vessel is planned, the...
3
by: Matthew Kennedy | last post by:
I'm not quite a noob, but I'm self taught and so I have some holes in my coding skills. I know that this should be possible but i cannot figure out what the syntax is. this is my first post to any...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.