Solved

Value not getting passed to Popup Form

Posted on 2014-01-13
10
406 Views
Last Modified: 2014-01-25
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
0
Comment
Question by:haident
10 Comments
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 39777471
What is the value you are trying to pass? Me.ContractDBNumber.value?
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39777474
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.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 39777580
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.
0
 

Author Comment

by:haident
ID: 39777619
Thanks for the info.  First time doing popups.

So in the BeforeInsert data event I added

Me.ContractDBNumber = Me.OpenArgs

Still get same error.  ?
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39777742
Did you check the arguments of the OpenForm?  The OpenArgs is the LAST argument.  You were filling in the Where argument.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:haident
ID: 39779778
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.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39779833
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.
0
 

Accepted Solution

by:
haident earned 0 total points
ID: 39794264
I ended up just making a subtable and tabbed forms to address this issue.
Nothins recommended helped me.
Thanks!
0
 
LVL 24

Expert Comment

by:Bitsqueezer
ID: 39799189
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
0
 

Author Closing Comment

by:haident
ID: 39808417
Nothing suggested helped me fix my problem.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now