Open form from another form and enter data from first form in field on second form

I have a form1 (frmEmployee_Audits)  that has a button that opens a form2 (frmScorecard_Sections).

What I want to do is when the button on form1 (frmEmployee_Audits) is clicked and it opens form2, (frmScorecard_Sections),  I want it to take the data in the field cboEmployee on form1 (frmEmployee_Audits) and place that data in the field Employee on form2 (frmScorecard_Sections).

I have posted my code in the Code section, but I am getting the following error:

“Syntax error in string in query expression ‘Forms![frmScorecard_Sections].[Employee] = ‘Steven Jones – JONESST””.

What am I doing wrong?

Thanks,
gdunn59
Dim stDocName As String
Dim stLinkCriteria As String

 stDocName = "frmScorecard_Sections"
 'stLinkCriteria = "[Employee]=" & "'" & Me![cboEmployee] & "'"
    
 'stLinkCriteria = "Forms![frmScorecard_Sections subform].[Employee] = " & " '" & Me.cboEmployee & "'"
    
    
 DoCmd.OpenForm stDocName, , , "Forms![frmScorecard_Sections].[Employee] = " & " '" & Me.cboEmployee & "'"""

Open in new window

LVL 1
gdunn59Asked:
Who is Participating?
 
DockieBoyConnect With a Mentor Commented:
I would do it one of two ways.

DoCmd.OpenForm "frmScoreCard", acViewNormal
Forms!frmScorecard!Employee = Me.CboEmployee

Or use the open args parameter to pass the value.

DoCmd.OpenForm "frmScorecard", acNormal,,,,," " & Me.CboEmployee & " "

Then in the On Open event for frmScorecard

Me.Employee = Me.OpenArgs

Be aware that the value passed will be the bound column of the combo box.  If you want to reference a different column from the combo box you will need to refer to it as such;

CboEmployee.Column(Insert column number here)

Combo Box Column numbers start at zero.  :)
0
 
peter57rCommented:
use:
"[Employee] = '"  & Me.cboEmployee & "'"
0
 
Dale FyeCommented:
If that record does not already exist in form2, then using the Where clause of the OpenForm method will not work.

In that case, you will want to open Form2 to a newrecord:

docmd.OpenForm "form2",,,,acFormAdd,,me.cboEmployee

and pass it the value of the combo box in the OpenArgs parameter.  Then, in the load event (I cannot recall why I choose to use the load event over the Open event) use the line of code DockieBoy mentioned above.

me.Employee = me.OpenArgs

There is another way to do this as well.  You could leave off the openargs value from  the open form method:

docmd.OpenForm "form2",,,,acFormAdd

And use the Current Event of Form2 to reach back to form1 and grab the data from that form.  This is the method I generally use if I want to pass more than one piece of information from one form to another.

Private Sub Form_Current

    if me.NewRecord then
        me.Employee = forms("Form1").cboEmployee
    end if

End Sub
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Dale FyeCommented:
To be a little more accurate, and prevent errors, I would also test to make sure Form1 is loaded before executint the code at the end of my last post:

Private Sub Form_Current

    if me.NewRecord and CurrentProject.AllForms("Form1").IsLoaded then
        me.Employee = forms("Form1").cboEmployee
    end if

End Sub
0
 
gdunn59Author Commented:
This one worked:

DoCmd.OpenForm "frmScoreCard", acViewNormal
Forms!frmScorecard!Employee = Me.CboEmployee

But now after showing the person that was going to use the database, they decided they wanted the information on the main form as a subform.

So now I need to open a new question to determine how to get the combo box on the second form to populate with certain data depending on what is chosen in the Region combo box on the main form.

Thanks,
gdunn59
0
 
DockieBoyCommented:
Just want to make sure that you are aware that there is no error checking in that code.  You may want to implement what the experts have advised and check to make sure that the form is loaded etc.  :)
0
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.