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

Posted on 2014-08-26
Last Modified: 2014-08-26
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.
Question by:SteveL13

    Author Comment

    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..."
    LVL 84
    Is the recordset for Form2 updateable?

    If Form2.txtHospitalVisitID bound to a field in the form's underlying recordset?
    LVL 84
    Also, do this in the Open or Load event of Form2:

    Msgbox Forms!frmAddMemberHospitalVisit.txtHospitalVisitID

    Does a message pop up with the correct value?

    Author Comment

    I moved the code to the onload event and it worked.
    LVL 33

    Accepted Solution

    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.

    Author Comment

    Great suggestion.  Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    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!

    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

    737 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

    16 Experts available now in Live!

    Get 1:1 Help Now