Link to home
Start Free TrialLog in
Avatar of Haiden Turner
Haiden TurnerFlag for United States of America

asked on

Value not getting passed to Popup Form

I am trying to pass a value from one form to the next when selecting an option on a combobox.  The subform opens just fine, but when I try to save it, it tells me the key value is null.  The key value is the value I thought I passed in the arguments.  It is an automated number field in the main form and a number field in the subform.  What am I missing?

Private Sub Combo345_Change()
If Combo345.Value = "Specific Dates" Then
  DoCmd.RunCommand acCmdSaveRecord
  DoCmd.OpenForm "frmSpecificDates", , , "ContractDBNumber =" & Me.ContractDBNumber.Value
Else
  If Combo345.Value = "per Airing" Then
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.OpenForm "frmPerAiring", , , "ContractDBNumber =" & Me.ContractDBNumber.Value
  End If
End If
End Sub
Avatar of Anthony Berenguel
Anthony Berenguel
Flag of United States of America image

What is the value you are trying to pass? Me.ContractDBNumber.value?
Avatar of PatHartman
Let's start by getting our terminology straight.  A subform is a form that is embedded in a control on another form.  In addition to all the attributes it has as a form, the control it sits in has additional attributes that are used for passing information from the main form to the sub form.  A pop up form is a stand alone form and has no relationship to any other form.  It is called a pop up because it is opened by a different form which is left visible in the background and is usually modal so focus remains on it until you finish with it and close it.

You have a popup form and so there is no link with the form that opened it.  You are using arguments of the OpenForm method to pass in a where clause to control what record the form opens to.  But since this isn't a subform, there are no master/child links defined.  That means that if the popup is related to the main form and it needs to include a foreign key in its Record when it is saved, YOU have to handle that.

If the form is opened from multiple other forms, the best option is to pass in the FK using the OpenArgs argument.  Then in the popup form's BeforeInsert event, you would populate the FK with the value from the OpenArgs.

Me.FK = Me.OpenArgs

Open in new window


Make sure you use the Form's BeforeInsert event because you do not want to dirty the form before the user does.

PS - ALWAYS give your controls meaningful names BEFORE you add code events.  Now, when you rename the control, you will disconnect from any event code so you'll have to open the code module, find the event code, and change its name.

PPS - When referencing form fields the .Value property is optional since it is the default property.   And using Me. gives you the advantage of intellisense (Access recognizes what you are typing and offers help) and is also more efficient since Access knows immediately what class module it needs to access to fine the definition of a variable.
Where is the code opening the subform? Is it in the main form? Do we have 2 subforms?

Upload a sample database depicting the problem.
Avatar of Haiden Turner

ASKER

Thanks for the info.  First time doing popups.

So in the BeforeInsert data event I added

Me.ContractDBNumber = Me.OpenArgs

Still get same error.  ?
Did you check the arguments of the OpenForm?  The OpenArgs is the LAST argument.  You were filling in the Where argument.
Thanks.  I corrected that.  Now I get the errror message 2113
"The value entered isn't valid for this field."
with the system highlighiting the row below (one with>>>>>)

Private Sub Form_BeforeUpdate(Cancel As Integer)
>>>>>>>>>>>>>>Me.[ContractDBNumberSpecificDates] = Me.OpenArgs
End Sub

How can I see what I'm passing to make sure?
It should just be a number.  
THe file is data type number in the new table, and it is a auto number field in the original table.
Add a stop to the code.  Then print the value in the debug window.  

? Me.OpenArgs


Or use
Debug.Print Me.OpenArgs in the procedure to print automatically.
ASKER CERTIFIED SOLUTION
Avatar of Haiden Turner
Haiden Turner
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

if you supplied what you had before as Where argument now in the OpenArgs then you have a string like that in OpenArgs:

"ContractDBNumber = 1234"

Of course you cannot fill that value into a field which is bound to a number field. So the argument in DoCmd.OpenForm must be simply that:

DoCmd.OpenForm FormName:="frmPerAiring", OpenArgs:= Nz(Me.ContractDBNumber,0)

Open in new window


The shown syntax is useful in case of functions with a lot of optional arguments like OpenForm where you only use few of them. On this way you don't need to count the commas and look into the tooltip to see if you are at the right parameter, moreover it is easier to read.
The Nz makes sure that a NULL will be converted into a 0 so OpenArgs always have a value in the opened form. Of course you can do the same without the Nz here and test the OpenArgs in the opened form instead if it is NULL (which would for example be the case if you open the form directly and not with your code).

A hint for future questions: If you create a quick example database file containing the needed tables with some demo data and the needed forms and code and upload that here like recommended by hnasr above you would get quicker answers. You should also answer questions and always describe the error exactly and nothing like "did not work" or "got an error" or something similar not very useful for anyone who tries to help you.

It is also strange that you wrote that this returns an error:
Me.[ContractDBNumberSpecificDates] = Me.OpenArgs

where you want to use a field Me.ContractDBNumber to be filled with the value above...

Cheers,

Christian
Nothing suggested helped me fix my problem.