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?
Check out this code in the OnLoad event procedure of my general purpose form that goes in the subform. - Option Compare Database
-
Option Explicit
-
-
Private Sub Form_Load()
-
Dim strOldTag As String
-
Dim ctlThis As Control
-
-
With Me
-
strOldTag = .Tag
-
.Tag = "ActiveForm"
-
For Each ctlThis In .Parent.Controls
-
If ctlThis.ControlType = acSubform Then
-
If ctlThis.Form.Tag = "ActiveForm" Then
-
.lblTest.Caption = ctlThis.Name
-
Exit For
-
End If
-
End If
-
Next ctlThis
-
.Tag = strOldTag
-
End With
-
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.
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.
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.
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: - If Me.Parent.MyName="ctrlPreparedBy" Then
-
Me.RecordSource="SELECT * FROM tbl_QA WHERE ID_QAType=1"
-
Me.lbl_QA.Caption="Prepared By"
-
Me.cmb_QAType.DefaultValue=1
-
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 :)
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.
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.
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. - Option Compare Database
-
Option Explicit
-
-
Private Sub Form_Load()
-
Dim strOldTag As String
-
Dim ctlThis As Control
-
-
With Me
-
strOldTag = .Tag
-
.Tag = "ActiveForm"
-
For Each ctlThis In .Parent.Controls
-
If ctlThis.ControlType = acSubform Then
-
If ctlThis.Form.Tag = "ActiveForm" Then
-
.lblTest.Caption = ctlThis.Name
-
Exit For
-
End If
-
End If
-
Next ctlThis
-
.Tag = strOldTag
-
End With
-
End Sub
To see it working download the very small db attached.
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!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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" ...
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
| | |