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
Solved

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

Posted on 2010-11-14
6
717 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

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…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

840 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