473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Link subform A on Tab 1 to subform B on Tab 2

Hello, I have 3 Forms.
My MainForm is linked to Subform A and now I want to link Subform A to Subform B;

My problem is now, that for design reasons I have to put subform A and B in Tabbs.
May 29 '15 #1
1 946
jforbes
1,107 Expert 1GB
There is an Easy way and a Better way to do this. For this example, there is a MainForm which is a SalesDashboard with SubFormA listing Companies for the Currently Selected SalesPerson. Then there is an additional SubForm, SubformB, a Datasheet of Projects that are limited to only show Projects for the Currently Selected Company.


Easy Way
Base SubFormB on a Query that has criteria that references SubFormA on the MainForm. This is an example of this type of Query:
Expand|Select|Wrap|Line Numbers
  1. SELECT Project.*, Project.CompanyID
  2. FROM Project
  3. WHERE (((Project.CompanyID)=[Forms]![SalesDashboard]![SubFormA]![Form]![CompanyID]));

Better Way
Base SubFormB on the Table of Projects instead of a Query. Then create code to update the Filter of SubFormB whenever a different record is selected on the SubFormA. The update code, should be located in the common area between the SubForms, aka the MainForm. But then the question becomes about how and when to fire the code that sets the Filter. This is the fun part. In SubFormA, there is this some code like this:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Private nParent As String
  4.  
  5. Public Sub setParent(ByRef sParent As String)
  6.     nParent = sParent
  7. End Sub
  8. Private Sub Form_Current()
  9.     If Len(nParent) > 0 Then
  10.         If isLoaded(nParent) Then
  11.             Forms(nParent).subFormCurrent
  12.         End If
  13.     End If
  14. End Sub
This code provides a way to let SubFormA know who it's Parent is, as well as call back to the Parent Form (MainForm) when it has changed records.
This uses the isLoaded() function that comes in handy and should be put in a Module:
Expand|Select|Wrap|Line Numbers
  1. Function isLoaded(ByRef sFormName As String) As Boolean
  2.     ' Determines if a Form is loaded
  3.     Dim i As Integer
  4.  
  5.     isLoaded = False
  6.     For i = 0 To Forms.Count - 1
  7.         If Forms(i).FormName = sFormName Then
  8.             isLoaded = True
  9.             Exit Function
  10.         End If
  11.     Next
  12. End Function
With these two pieces in place, all that is left is to write the code for the MainForm:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Private Sub Form_Load()
  4.  
  5.     ' Setup SubForms
  6.     Call Me.SubFormA.Form.setParent(Me.Name)
  7.     Me.SubFormB.Form.Filter = "1=0"
  8.     Me.SubFormB.Form.FilterOn = True
  9.  
  10. End Sub
  11.  
  12. Public Sub subFormCurrent()
  13.  
  14.     ' Filter SubForm
  15.     Dim sFilter As String
  16.     sFilter = "CompanyID=" & Nz(Me.SubFormA.Form!CompanyID, "")
  17.     Me.SubFormB.Form.Filter = sFilter 
  18.     Me.SubFormB.Form.FilterOn = True
  19.  
  20. End Sub
Hopefully this code is accurate Syntax wise as it was knitted together from a couple different places.
May 29 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ivan | last post by:
I have a form (CustomerImfo) that has a subform within it (order). Within that subform is another subform (OrderDetail). That subform (OrderDetail) is displayed in Datasheet view. When I am...
1
by: Bill Strass | last post by:
Problem: Access main form/subform as front end to SQL Server backend. Add/edit via subforms work fine. Not so with main form. Set up a master-detail form-subform using two views linked from SQL...
3
by: Ellen Manning | last post by:
Using A2K. I've got a mainform with a subform (subfrm1). There's a subform within that subform (subfrm2). When an amount is entered on the mainform, I want that amount to appear on subfrm1 and...
9
by: robert d via AccessMonster.com | last post by:
I'm not sure why the following isn't working. The subform loads correctly, but no data is displayed. I'm certain that there is data and I have checked that the SQL statement does in fact return...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
11
by: mrowe | last post by:
I am using Access 2003. (I am also using ADO in the vast majority of my code. I recently read a post that indicated that ADO is not all that is was initially cracked up to be. In the back of my...
5
by: John Torres | last post by:
I am trying to link second subform base on the 1st subform. I’ve read several threads and it’s just look too complicated for me (sorry I’m a newbie). I want to link shipping information (2nd...
5
by: samdev | last post by:
I have created two combo boxes in a subform.... For example 1. Combo Box State 2. Combo Box City 3. When a state from the Combo Box State is selected, the City combo box updates to reflect...
0
by: 20028431 | last post by:
I have a main form called ’frmIssues’ which contains 2 subforms ‘sbfmLog’ and ‘sbfmLogNote’ frmIssues is bound to table ‘tblIssues’ and has fields IssueId (PK- Autonumber), Title (txt), DateOpened...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.