Solved

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

Posted on 2011-02-20
6
279 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 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 48

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 48

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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

729 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