Solved

Open a form and go to a specific record in a subform

Posted on 2010-11-14
6
708 Views
Last Modified: 2012-06-27
In the database a form open when I click on a button from another form.  It should d go to a specific record in a subform.  Right now I am able to open a form and go to a record on a main form using the code:

 DoCmd.SearchForRecord , "", acFirst, "[x] = " & Str(Nz(Screen.ActiveControl, 0))

How do I do this but go to a record in a sub-form?  The main form is called Company and the subform is called Employee.

 I need to open a form and go to an employee in a particular company (an contact can work for multiple companies so it is more than a 1-to-1 relationship in this which is making it a bit tricky from a coding perspective so I need to use the company ID also).  
0
Comment
Question by:mtrussell
  • 3
  • 2
6 Comments
 
LVL 84
ID: 34132363
You can move to a record like this:

Dim rst As DAO.Recordset
Set rst = Me.Employee.Form.Recordset
rst.FindFirst "EmployeeID=" & YourEmployeeID

If not rst.NoMatch Then Me.Employee.Form.Bookmark = rst.Bookmark

If you need to use CompanyID as well, just alter the FindFirst to include all needed fields.

Note also that you must use the correct name for the Subform CONTROL. I'm assuming it's named "Employee", but it may be named differently. This is the Subform CONTROL on the Main form, the one that holds the Employee Data.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34134073
A subform is designed to display info from a One to Many relationship.

...One Customer (Main)-->many Orders (sub)

If you are using a suboform for any other relationship (1 to 1, Self Join, many to Many, or the infamous "more than a 1-to-1") you need to be very careful on how your tables are related and how the Linking fields are set up for the subform.

With this in mind, can you tell us a bit more about this mysterious "more than a 1-to-1 relationship"? and how the main/subform is designed?

;-)

JeffCoachman
0
 
LVL 84
ID: 34135290
I think what they're saying is the Conact-Company relationship is a Many-to-Many relationship. If that's the case, you do need to insure that your Master/Child link fields are setup correctly (you'll have more than one).
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:mtrussell
ID: 34143703
Let me provide some more detail.  I have my contacts in one table with personal information and the contacts are then are assigned to companies as employees.  The contact can work for multiple companies.  On the contact screen I am using a subform that pulls information from a query and displays the company names where the contact is assigned.

What I want to do is from the contact screen  if you click on the company name, the companies form opens and go to the specific record of the contact in the employees subscreen.  

The parent/child relationship works fine.  
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 34144019
So you're opening the Company form, and you then want to move to a record in THAT form's subform?

Open the form like this:

DoCmd.OpenForm "MyCompanyForm", , , , "CompanyID=" & ME.CompanyID

Now use the code as above to navigate to the record in that form's subform. You'll have to refer to the form a bit differently:

Dim rst As DAO.Recordset
Set rst = Forms("MyCompanyForm").Employee.Form.Recordset
rst.FindFirst "EmployeeID=" & YourEmployeeID

If not rst.NoMatch Then Forms("MyCompanyForm").Employee.Form.Bookmark = rst.Bookmark

You'd obviously have to change the names of Forms, Tables, Fields, etc to match those in your project.
0
 

Author Closing Comment

by:mtrussell
ID: 34144723
Thanks!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 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