2nd form won't get record number from 1st form

I have a form we'll named frmAddMemberHospitalVisit.  On this form is a command button that opens form 2.  On form 2 I have a field that I want to populate with the data from a field on form 1.  

In the default value setting for the field on form 2 I have:


But it is not populating with anything.  I've done this countless times before but can't figure out why it isn't working this time.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveL13Author Commented:
I also tried this in the onopen event of the 2nd form...

Me.txtHospitalVisitID = Forms!frmAddMemberHospitalVisit.txtHospitalVisitID

But I get an error...  "You can't assign a value..."
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Is the recordset for Form2 updateable?

If Form2.txtHospitalVisitID bound to a field in the form's underlying recordset?
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Also, do this in the Open or Load event of Form2:

Msgbox Forms!frmAddMemberHospitalVisit.txtHospitalVisitID

Does a message pop up with the correct value?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

SteveL13Author Commented:
I moved the code to the onload event and it worked.
A better event to use is the BeforeInsert event.  That event runs as soon as you dirty the first field on the form and it only runs once for each new record.  Using this event prevents you from dirtying the form.  When the program dirties a form, the user can get confused because he knows he didn't touch anything and yet, he will get bombarded with error messages if he tries to close the form without entering anything else.

Knowing when events fire will go a long way toward gaining control over how bound forms work.  The code wouldn't work in the Open event because the form's RecordSource had not yet been loaded.  The code sort of works in the Load event where you have it now but it will only work ONCE.  If the user adds a second record, the code will not fire.  By using the correct event - BeforeInsert, it doesn't matter how many records the user enters.  Each one will have the foreign key populated and you won't be dirtying the form and causing user confusion.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveL13Author Commented:
Great suggestion.  Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.