I need to develop a way to copy contents from one sub-form to another subform.

I need to develop a way to copy contents from one sub-form to another sub-form.  I have three sub-forms on a main form.  The three are next to each other left-to-right.  The two left-hand sub-forms are bound to tables and the data being shown is locked.  The right-hand sub-form is bound to a table also but think of it as a new record that is being created.  If I double click one of the fields shown in one of the two left-hand sub-forms, I want that data to be copied over to the right-hand sub-form.

Actually, if I could click and drag the contents from one of the two left-hand sub-forms I would but I'm not sure that's possible, correct?  So I'm opting for the double-click method instead.

What would the VBA code look like to accomplish this?
SteveL13Asked:
Who is Participating?
 
Gustav BrockConnect With a Mentor CIOCommented:
You can use the RecordsetClone of the target subform:

Dim rs As DAO.Recordset

Set rs = Me.Parent!NameOfTargetSubformControl.Form.RecordsetClone
rs.AddNew
    rs!SomeField.Value = Me!FirstSourceField.Value
    rs!AnotherField.Value = Me!SecondSourceField.Value
    rs!YetAField.Value = Me!ThirdSourceField.Value
    ' etc.
rs.Update
rs.Close

Open in new window

The target will update automatically.
0
 
SteveL13Author Commented:
Additional comment. When I d-c on the field I need that fields content copied as well a hidden fields content in the same record copied.
0
 
SteveL13Author Commented:
Into 2 fields.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
mbizupCommented:
You'd run an INSERT query through the dblClick event:

Dim s As string
s = "INSERT INTO tblB (MyNumber, MyString) VALUES (" & me.MyNumericField & "," & chr(34) & me.MyTextField & chr(34) & ")"
currentdb.execute s, dbfailonerror
Me.Parent.SubformB.Form.Requery

Open in new window

0
 
SteveL13Author Commented:
This has changed.  Sorry about that.  I now have 4 text fields that need to be entered in the table.  Here is my code:

s = "INSERT INTO tblFoundation (MyStringA, MyStringB, MyStringC, MyStringD) VALUES (" & Forms!frmProjectBuilder.txtCategory & "," & Chr(34) & Me.txtSubCategory & "," & Chr(34) & Forms!frmProjectBuilder.txtJobNumber & "," & Chr(34) & Me.txtDescription & Chr(34) & ")"

Open in new window


But I am getting error:  Syntax error (missing operator) in query expression "Underslab Insulation, "Z999'!

All four fields are text so I have:
0
 
SteveL13Author Commented:
And when I Debug.Print I get:

INSERT INTO tblFoundation (MyStringA, MyStringB, MyStringC, MyStringD) VALUES (Design,"Height of Interior Drive-Thru to Adjacent Floor,"Z999,"Floor slab of Drive Aisle and Auto Loading Area to be set 4" below adjacent floor slabs. []")

It is like " marks are missing maybe?
0
 
mbizupCommented:
>> It is like " marks are missing maybe?

Yup... you need to delimit all text data with quotes. Also, the double quotes in the memo field data are problematic with this method.

Use the recordset method that Gustav suggested instead.  If you are unclear about which delimiters go where, the recordset approach has the advantage that no delimiters are required.
1
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.

All Courses

From novice to tech pro — start learning today.