[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-02-20
6
Medium Priority
?
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 77

Expert Comment

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

Accepted Solution

by:
DockieBoy earned 1000 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 48

Expert Comment

by:Dale Fye
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 48

Expert Comment

by:Dale Fye
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
 
LVL 1

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 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