Solved

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

Posted on 2010-11-14
6
705 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:mtrussell
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

771 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

11 Experts available now in Live!

Get 1:1 Help Now