473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to get the control name of my subform, from within the subform?

TheSmileyCoder
2,322 Expert Mod 2GB
I have a subform in a form, and that subform is repeated 3 times within the same Parent form. When the subform opens, I want to set the recordsource of a combobox within the subform, depending on where the subform is placed (and whether is subform 1, 2 or 3)

What I do currently is that I know they open in a specific order, and simply use a public counter, to keep track of which subform is opening, and set my recordsource according to that.

What I would like to do, is to access the control name within the parent, I.E the name that the subform has been givin in the parent form. Anyone know how to get this?
May 7 '10 #1

✓ answered by NeoPa

Check out this code in the OnLoad event procedure of my general purpose form that goes in the subform.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Form_Load()
  5.     Dim strOldTag As String
  6.     Dim ctlThis As Control
  7.  
  8.     With Me
  9.         strOldTag = .Tag
  10.         .Tag = "ActiveForm"
  11.         For Each ctlThis In .Parent.Controls
  12.             If ctlThis.ControlType = acSubform Then
  13.                 If ctlThis.Form.Tag = "ActiveForm" Then
  14.                     .lblTest.Caption = ctlThis.Name
  15.                     Exit For
  16.                 End If
  17.             End If
  18.         Next ctlThis
  19.         .Tag = strOldTag
  20.     End With
  21. End Sub
To see it working download the very small db attached.

9 2900
NeoPa
32,556 Expert Mod 16PB
The term Subform is often used to refer to the Access Form that is contained within the .Form property of a Subform control. This is not correct usage and can cause confusion.

Can you say exactly how you're using the term here. Is it a subform control you're referring to, or a form within such a control?

You can be assured that whatever your answer is, we will try especially hard to help you find a solution, due to the efforts you've already made on behalf of our members.
May 7 '10 #2
TheSmileyCoder
2,322 Expert Mod 2GB
On my main form, I have a Control, within that control is a form (what I call my subform). From within my subform, I need to know the name of the control, within which it resides.
May 7 '10 #3
NeoPa
32,556 Expert Mod 16PB
Me.Parent gives you access to the form that the subform controls are on. It doesn't tell you which of the subforms the current form is attached to though I'm afraid (at least not anywhere I know about or could find). If you have multiple subform controls, all with the same form attached to them, then I see no way of determining which is the attached one.

It could be determined with some fiddly code I expect. Set the .Tag property to a value that doesn't occur naturally, then check through all the subform controls in Me.Parent to find one whose attached form also has that .Tag setting. It's certainly not direct, but can determine the correct one for you.

Referencing items can be done using Referring to Items on a Sub-Form.
May 7 '10 #4
TheSmileyCoder
2,322 Expert Mod 2GB
In the end I decided to put the code in the parent form's Load event.
Im unsure whether this is the best place to put it, but its working now, so I don't wish to spend any more time on it.

Thank you for taking the time to look at it.
The .Tag approach wouldn't work, any approach that requires looping through the parents controls would not work, since I need to know which ctrl im "in" before I can use the tag approach.

Basicly what I was looking for is to be able to write in the forms open/load event:
Expand|Select|Wrap|Line Numbers
  1. If Me.Parent.MyName="ctrlPreparedBy" Then
  2.   Me.RecordSource="SELECT * FROM tbl_QA WHERE ID_QAType=1"
  3.   Me.lbl_QA.Caption="Prepared By"
  4.   Me.cmb_QAType.DefaultValue=1
  5. End If
Because that would allow me to reuse the same form once for Prepared By, once for Checked By, and once for Approved by. Now as I said, I just do it from the parent form.
But seems there is no .MyName property :)
May 10 '10 #5
NeoPa
32,556 Expert Mod 16PB
I'm not sure I see your problem with the .Tag approach Smiley. There are .Tag properties on the form as well as on most of its controls. This could easily be set in the code, and also checked with reference to the parent form. It's your choice of course, but I think it should work.

As for the .MyName property, there is a .Name property of course. Is this what you need or do I misunderstand what you're talking about.

PS. I love the idea of re-using a form intelligently rather than creating three of them with minor differences.
May 10 '10 #6
TheSmileyCoder
2,322 Expert Mod 2GB
I have noticed that as I get more and more experience in VBA the amount of code I choose to attach to a form increases as well. And having to keep 3 forms uptodate, when they only have minor differences was really starting to annoy me. Thats when I started looking for a more re-usable approach.

Imagine a form frm_DocumentQA containing 3 controls, each control containing a sub form (the same form). Lets name the subfrm frm_QA, and the 3 controls named ctrlFrm_PreparedBy, ctrlFrm_CheckedBy, ctrlFrm_ApprovedBy , each having the same sourceobject frm_Qa. They all have the same Link Child Fields and Link Master Fields properties, namely Link Master Field: KEY_Document and Link Chield Fields: ID_Document.


Even giving them tags, I could just as well cycle through the .Name property as the .Tag property. The problem remains that from within the sub form, I don't know which control my subform is "stored" in.


The .MyName was an imaginary name for the property I was wishing for, the .Name would just return frm_QA for all 3 subforms.
May 10 '10 #7
NeoPa
32,556 Expert Mod 16PB
Check out this code in the OnLoad event procedure of my general purpose form that goes in the subform.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Form_Load()
  5.     Dim strOldTag As String
  6.     Dim ctlThis As Control
  7.  
  8.     With Me
  9.         strOldTag = .Tag
  10.         .Tag = "ActiveForm"
  11.         For Each ctlThis In .Parent.Controls
  12.             If ctlThis.ControlType = acSubform Then
  13.                 If ctlThis.Form.Tag = "ActiveForm" Then
  14.                     .lblTest.Caption = ctlThis.Name
  15.                     Exit For
  16.                 End If
  17.             End If
  18.         Next ctlThis
  19.         .Tag = strOldTag
  20.     End With
  21. End Sub
To see it working download the very small db attached.
Attached Files
File Type: zip Smiley.Zip (16.9 KB, 126 views)
May 10 '10 #8
TheSmileyCoder
2,322 Expert Mod 2GB
Brilliant!

I haven't tried it yet, but im sure it will work. I never thought of actually setting the tag of the subform, I always thought you were setting the tag of the control in the parent form, containing the subform.

Now I see why it will work. Thank you NeoPa!
May 10 '10 #9
NeoPa
32,556 Expert Mod 16PB
I'm please to help of course Smiley :)

I think your confusion was probably related to using the terms incorrectly. This can so easily happen. Anyway, the important thing is that you understand now.
May 10 '10 #10

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

Similar topics

5
by: Ellen Manning | last post by:
Using A2K. I need the syntax to set the recordsource for a subform within a subform. The "main" subform's recordsource changes based on user input. I need to change the recordsource on the...
1
by: MP | last post by:
I have a main form that has a subform which also has a subform: the main form is the first subform is the second subform is When I click on the button »AddNewSubSubRecord« (add a new record...
3
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
6
by: ryan | last post by:
I created in design view a subform within a subform. However the subform within the subform in only visible in design view. In form view the subform within the subform is not visible. I have...
3
by: Mike Jakes | last post by:
I hope that someone can offer a little advice on this one - I've searched the group but can't find an answer. I think that I'm doing something really stupid or missing something trivial, but see...
0
by: Charlie | last post by:
I can get Nunit Asp.Net to work fine with standard textboxes and buttons. However when the rendered HTML contains controls that are named something like <input name="_ctl0:tbUserName" ... ...
2
by: VMI | last post by:
In my Windows Form, is it possible to get the control name through object sender in an event handler? For example, in private void dataGridView_zip_KeyPress(object sender, KeyPressEventArgs e), how...
1
by: Dave | last post by:
Hello, I know that I can use the Parent property to get the name of the form that a subform is on, but what if I need the name of the subform/ subreport "box" on that form that contains my...
5
by: bplantes | last post by:
I have a "Dashboard" in a tool I am building which has a list of different buttons. Clicking on each button will display different subforms which show up in a window to the right of the menu. One...
2
by: angi35 | last post by:
I hope this is an easy question for someone out there. In Access 2000…I have a MainForm with a tab control (MAIN TABS) with 7 tabs. Within each tab is a SubForm. Within each SubForm is a tab...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.