Solved

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

Posted on 2011-02-20
6
272 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:gdunn59
6 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 34936662
use:
"[Employee] = '"  & Me.cboEmployee & "'"
0
 
LVL 3

Accepted Solution

by:
DockieBoy earned 250 total points
ID: 34936667
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
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 34937755
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 34937760
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
 

Author Closing Comment

by:gdunn59
ID: 34946408
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
 
LVL 3

Expert Comment

by:DockieBoy
ID: 34946556
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

746 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

15 Experts available now in Live!

Get 1:1 Help Now