Full Version: pass value from Form A to subform in Form B
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
Acess_help
I have two forms - FrmOrderInfoMain to enter order details and FrmItemSearch to search for items in the inventory list. FrmItemSearch is opened by clicking a button on FrmOrderInfo. Users search for the items they want in the form and then click on the record's "Select" button to pass over the value to FrmOrderInfoMain.

My code for the "Select" button is,
CODE
Dim strItemDesc As String

strItemDesc = txtItemDesc



DoCmd.SelectObject acForm, "FrmOrderInfoMain"

Form!FrmOrderInfoMain.Form!SubFrmOrderInfo!txtItemDesc = strItemDesc


However I get an error that says Access cant find the field "FrmOrderInfoMain" referred to in your expression. Does anyone knw what does this mean?
ChrisO
G’day Swee

Can’t easily test this so please take it with a grain of salt.

You did not say which line of code raises the error.

If it’s the first line of code that raises the error then…

This line of code is probably not required.
DoCmd.SelectObject acForm, "FrmOrderInfoMain"
Don’t know what it does because I’ve never had to use it.

If it’s the second line of code that raises the error then…

Just as a start the Forms collection is plural: -

Form!FrmOrderInfoMain.Form!SubFrmOrderInfo!txtItemDesc = strItemDesc
Forms!FrmOrderInfoMain.Form!SubFrmOrderInfo!txtItemDesc = strItemDesc

But I doubt if that will fix it because FrmOrderInfoMain does not have a Form property so: -
FrmOrderInfoMain.Form
should be incorrect.

Forms!FrmOrderInfoMain!SubFrmOrderInfo!txtItemDesc = strItemDesc

That should still be incorrect because SubFrmOrderInfo is a Form container and does not have a control named txtItemDesc.

It is the Form in the SubForm container that has the control txtItemDesc.

Therefore it looks like it should be: -

Forms!FrmOrderInfoMain!SubFrmOrderInfo!Form!txtItemDesc = strItemDesc

But this is ‘airware’ because I seldom use the bang (!) and prefer the dot (.)
(And I don’t use the code that ‘build’ might create.)

Here is a link to the subject…

http://www.advisor.com/Articles.nsf/aid/BAROA06

The way I read that article is… Dot(.) when you can and Bang(!) when you can’t.

I don’t go that far but use the Dot(.) for properties and the Bang(!) for field references.

I think it’s mostly a personal preference but the consistent use of a preference yields dividends.

Hope that helps.

Regards,
Chris.
Acess_help
Hi Chris,

I tried your code using Bangs but it doesnt work still. Got the same error. Any ideas?
ChrisO
G’day Swee

CODE
Option Explicit
Option Compare Text


Private Sub cmdTestIt_Click()
    Dim strItemDesc As String
    
    strItemDesc = "Help"

    Forms("FrmOrderInfoMain")("SubFrmOrderInfo").Form.txtItemDesc = strItemDesc

    [color="green"]'  or ...[/color]

    Forms![FrmOrderInfoMain]![SubFrmOrderInfo].Form.txtItemDesc = strItemDesc
    
    [color="green"]'  I prefer the first method because the form names can be variables.[/color]

End Sub

Hope that helps.

Regards,
Chris.
Acess_help
Hi Chris,

I used this line of code and it works great now,
CODE
[Forms]![FrmOrderInfoMain].[SubFrmOrderInfo].[Form]![ItemDesc]


Phew! Thanks for your time!
ChrisO
G’day Swee

Well I’m glad it is now working but I don’t know how.

In the original question the final control was called txtItemDesc

Now it appears that you are writing to ItemDesc, which is probably the bound field to the control txtItemDesc.

I would recommend looking at the: -

Forms("FrmOrderInfoMain")("SubFrmOrderInfo").Form.txtItemDesc = strItemDesc

line of code because, even though it may not be understood at this stage, it will prove more flexible in the future.

"FrmOrderInfoMain", "SubFrmOrderInf” and even the control “txtItemDesc” can become variables and that is where the power lays.

In any language, including VBA, the ability to substitute ‘hard coded’ names with a variable is important.

It allows for the name to be defined in one place and therefore fixed in one place.
It also allows for the passing of names as arguments to procedures.

I therefore strongly recommend looking at the code again.

Just because some code happens to work does not mean we should stop looking at it.
Our coding skills develop over time and time starts today.

Regards,
Chris.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.