Passing data from one form to another

mba_110

Registered User.
Local time
Today, 13:53
Joined
Jan 20, 2015
Messages
280
Hi

I have frmRecords and pop up form frmAddRecords , i want frmAddRecords pop form to pass data to frmRecords for selected fields, frmRecords have all table fields but frmAddrecords have just 4 fields for tblRecords.

tblRecords
Primary Key is RecordID which is autonumber.

frmAddRecords have four fields from tblRecords which is [RecordID], [EmpID],[Reference],[SponsorID] four out of 3 fields are combobox.

I have following code but its not working.

Code:
 Private Sub btnClose_Click()
If Not IsNull(txtReference) Then

Forms![frmRecords].Form.[frmAddRecords].Form.cboReference = Me.txtReference
'Forms![frmRecords].Form.[frmAddRecords].Form.cboEmpID = Me.txtEmpID
'Forms![frmRecords].Form.[frmAddRecords].Form.cboSponsorID = Me.txtSponsorID
'Forms![frmRecords].Form.[frmAddRecords].Form.RecordID = Me.txtRecordID
Forms![frmRecords].Form.[frmAddRecords].Form.cboReference.SetFocus

End If
DoCmd.Close

End Sub
 
If both forms are bound to the same data source (table/query)
then you only need to re-query the main form (frmRecords) once you've closed the pop up.

But why bother with the pop up form (frmAddRecords)?
Why not just enter the data on the main form (frmRecords)?
 
did you use the BUILDER to get these path names?
always use the builder, it never gets the path wrong.

usu, the path is like:
forms!fMasterForm!subformName!form!cboBox
 
Sorry but not working,

Error message for Requery
"invalid or unqualified reference"

Code:
If Not IsNull(txtReference) Then

Forms![frmRecords].Form.[frmAddRecords].Form.cboReference .Requery
Forms![frmRecords].Form.[frmAddRecords].Form.cboReference.SetFocus

End If
DoCmd.Close

End Sub
 
Remove the first instance of .Form from each line.
It is only needed after the name of the subform container.

Also remove the space before .Requery

As you have no spaces the [] aren't needed...but do no harm.
 
Still have problem....

Error message
"Microsoft Access can't find the field 'l1' referred to in your expression"
 
Make sure you are using the name of the subform container

in design view, click on the outside border of the subform container then look in the property sheet for its name. Modify as necessary

If you are running the code from the main form then it can be:

Code:
If Not IsNull(Me.txtReference) Then
         Me.[B][COLOR="Red"]subformContainerName[/COLOR][/B].Form.cboReference.Requery
         Me.[COLOR="red"][B]subformContainerName[/B][/COLOR].Form.cboReference.SetFocus
End If
DoCmd.Close

The above assumes txtReference is in the main form and cboReference is in the subform. If that's not the case, then the code will need to be adapted

You may not need the requery
 
if you dont have subform on frmRecords:

Private Sub btnClose_Click()
If Not IsNull(txtReference) Then
Forms![frmRecords]!cboReference = Me.txtReference
...
...
 
Last edited:
Not working error message "Application define or object define error"


Their is no subform both forms are individual main form have New button that open the frmAddNewRecords and main form name is frmRecords

cboReference is on Main form which is frmRecords.

txtReference is on frmAddnewRecords

My code is on click button which is placed on frmAddnewRecords i want that data to go in main form which is frmRecords.


both are two different forms but tblRecords are common only the difference is frmAddnewrecords dont have all the fields of tblRecords but frmRecords have all the fields that tblRecords holds.

All fields of frmRecords is depending on cboReference to show same rows field data on text boxes on frmRecords.

Whatever fields start with txt is frmAddnewRecords field and whatever is start from cbo is main form fields.
 
See post 2. Is frmAddRecords bound to table? If yes then data is already in table when frmAddRecords closes. Attempting code to populate bound controls on frmRecords is duplicating record. Just Requery frmRecords to make new record(s) available.

What you might need is code to go to newly created record when returning to frmRecords.

Is cboReference a bound control? Or UNBOUND and used to input filter criteria?

Also wonder why not just use frmRecords to add new record.
 
Last edited:
Private Sub btnClose_Click()
If Not IsNull(txtReference) Then
With Forms![frmRecords]
.Requery
!cboReference = Me.txtReference
End With
...
...
 
Must commit record to table before requery. Assuming cboReference is UNBOUND:
Code:
With Forms![frmRecords]
   !cboReference = Me.txtReference
   Me.Close
   .Requery 
End With
Record is committed when: 1) close table/query/form; or 2) move to another record; or 3) run command to save.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom