Passing data from one form to another (1 Viewer)

mba_110

Registered User.
Local time
Today, 07:49
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
 

bob fitz

AWF VIP
Local time
Today, 15:49
Joined
May 23, 2011
Messages
4,724
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)?
 

Ranman256

Well-known member
Local time
Today, 10:49
Joined
Apr 9, 2015
Messages
4,337
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
 

mba_110

Registered User.
Local time
Today, 07:49
Joined
Jan 20, 2015
Messages
280
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
 

isladogs

MVP / VIP
Local time
Today, 15:49
Joined
Jan 14, 2017
Messages
18,219
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.
 

mba_110

Registered User.
Local time
Today, 07:49
Joined
Jan 20, 2015
Messages
280
Still have problem....

Error message
"Microsoft Access can't find the field 'l1' referred to in your expression"
 

isladogs

MVP / VIP
Local time
Today, 15:49
Joined
Jan 14, 2017
Messages
18,219
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:49
Joined
May 7, 2009
Messages
19,242
if you dont have subform on frmRecords:

Private Sub btnClose_Click()
If Not IsNull(txtReference) Then
Forms![frmRecords]!cboReference = Me.txtReference
...
...
 
Last edited:

mba_110

Registered User.
Local time
Today, 07:49
Joined
Jan 20, 2015
Messages
280
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.
 

June7

AWF VIP
Local time
Today, 06:49
Joined
Mar 9, 2014
Messages
5,470
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:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:49
Joined
May 7, 2009
Messages
19,242
Private Sub btnClose_Click()
If Not IsNull(txtReference) Then
With Forms![frmRecords]
.Requery
!cboReference = Me.txtReference
End With
...
...
 

June7

AWF VIP
Local time
Today, 06:49
Joined
Mar 9, 2014
Messages
5,470
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:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:49
Joined
Feb 19, 2002
Messages
43,267
There's a lot of air code here and most of it uses dot and bang incorrectly. Check the syntax.

The dot precedes Access properties or methods
The bang precedes user defined properties or methods.

The EXCEPTION - The fields of a form or report's bound RecordSource are added to the Form's field collection so they can be referenced with Me.fieldname so that you get intellisense.

In any procedure of a form's code module you can test this out:
On the form create a new text control, bind it to a field of your RecordSource. Name it:
txtFieldName

in some procedure type
Me.txtFieldName.
and
Me.FieldName.

Notice that for the control name, you get control properties and for the field name, you get field properties

If you don't use prefixes for your control names, then Me.FieldName. will show both control and field properties.

In earlier versions of Access, there was a distinction but the lines seem to have blurred somewhat or Access has gotten more user friendly. However, I still use the convention because it lets me logically distinguish between when my code reference the control or when it references the field from the recordsource.

Also note that the Forms! qualifier rigidly follows the above rules whereas the Me. and Me! are more flexible.
 

Users who are viewing this thread

Top Bottom