Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning Value to Subform

Status
Not open for further replies.

tbassngal

Programmer
Feb 18, 2003
74
0
0
US
First, I am having a hard time with the syntax for a subform. I can't even get the code to realize I am on a subform.

Next, I have a main form frmAddDoc with a field txtTitle and a subform subfrmReference that loads from the main with an on_click event button. I have a field txtTitle in the subform that should be assigned the value txtTitle when the form is loaded from the on_click event of the main. Can someone please provide to me an example of code that would do this? I haven't done it in such a long time and my syntax is just not working.

Bottom line - txtTitle in the subform = txtTitle in the main form

Me!subfrmReferences.Form!subtxtTitle = Forms![frmAddNewDoc]![txtTitle].Value

Here is what I have so far:

Private Sub cmdReferencesComplete_Click()
With Me!subfrmReferences.Form
.Visible = False
End With
End Sub

Private Sub Form_GotFocus()
subtxtTitle.SetFocus
Me!subfrmReferences.Form!subtxtTitle = Forms![frmAddNewDoc]![txtTitle].Value
End Sub

Private Sub cmdAddAnother_Click()
Dim MyDB As Database, rcsReferences As Recordset
Set MyDB = DBEngine.Workspaces(0).Databases(0)
Set rcsReferences = MyDB.OpenRecordset("tblReferences")

rcsReferences.AddNew
rcsReferences![Title] = Forms![subfrmReferences]![subtxtTitle]
rcsReferences![Reference] = Forms![subfrmReference]![cboReference]
rcsReferences.Update

If Nz([subtxtTitle], "") = "" Or Nz([cboReference], "") = "" Then
MsgBox "You must complete all fields or click References Complete!", vbCritical, "Incomplete Record!"
Exit Sub
End If

MsgBox "This Reference has Added!", vbDefaultButton1, "Reference Added!"

subtxtTitle = ""
cboReference = ""
End Sub
 
If you go to the properties of a subform control (i.e. txtTitle) and go to the control source, click the ... to build.

Choose the expression builder. Then on the left choose Forms | Loaded Forms | main form

in the middle frame choose the txtTitle field (which is from the main form) and double-click it. That will give you the syntax you need. You can copy that and cancel out of the build. Then paste that syntax into your code.

I use that trick all the time to get the right syntax between main forms, sub forms and sub-sub forms.

Code:
Private Sub cmdReferencesComplete_Click()
[syntax of subform control] = me.txtTitle
End Sub

If you are trying to update the value of the control source for a bound table field in that sub form control item, then you might take a look at the code I use in the post I have on a different issue: thread702-1654839 .

I can't take credit for it. I found it in a google search lol.


HTH

misscrf

It is never too late to become what you could have been ~ George Eliot
 
I am not sure where I am going wrong... I've tried to add the code under both forms - separately testing of course. I am certain I am just not identifying the subform correctly in the syntax. For example, in this example:

Private Sub cmdOpenRefSub_Click()
With Me![subfrmReferences].Form
.Visible = True
Me![subtxtTitle] = [Forms]![frmAddNewDoc]![txtTitle]
End With
End Sub

This button is on the main form and I thought since subfrmReferences was active, maybe its not that I could use a "With Me!" but that isn't working either. The value for [Forms]![frmAddNewDoc]![txtTitle] is showing up during debugging but it cannot find subfrmReferences through my code. Where am I going wrong?

Thank you in advance for your patience and help.
 
I obviously meant "incorrectly" vs correctly. "I am certain I am just not identifying the subform correctly in the syntax.
 
but is the button on the main form or sub form? If it is on the subform, then that should work, but if it is on the main form, you have your references backwards.

it needs to be

[Forms]![frmAddNewDoc]![Form]![subtxtTitle] = me.txtTitle

when you use me, just use me. not me![xxx] I think that is correct.

Also, please let me know if you are setting an unbound textbox control for display, or adding this value to a table by using the code to populate a bound text box. That will likely make a difference in the method that you want to use.


misscrf

It is never too late to become what you could have been ~ George Eliot
 
Misscrf's advice will keep you out of trouble. But if you do not use it, then you need to verify that the correct name of the subform control. If using the wizard it normally will have the same name as the source object, but there are many cases where it may not have the same name.
Also if you use dot notation and not bang notation intellisense will help
me.startTypingAndTheSubFormControlNameShouldAppear

If the name does not appear then you are obviously using the wrong name.


me.subFormControlName.Form.NameOfControlOnSubForm

Inside a subform control is a source object. Just because your source object is subFrmReferences, the subform control could be called anything (often it defaults to Child0). Click on the outside of the control to verify. Lets assume it is called "Child0".

with me.Child0.Form
.visible = true
.subTxtTitle = [Forms]![frmAddNewDoc]![txtTitle]
end with

I think you should also be able to drop
[Forms]![frmAddNewDoc]!
and just
.subTxtTitle = txtTitle

 
Out of curiosity why even do this? Why not do a standard subform and link the subform to the main form by
master field: txtTitle
child fields: subTxtTitle

It would then do all of this automatically
 
Duh, Maj. Lol. It goes to my first question. Is the control bound or not? i.e. are we setting a field to show the value, or updating a bound field in a table?

misscrf

It is never too late to become what you could have been ~ George Eliot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top