473,395 Members | 1,516 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,395 software developers and data experts.

Subform query only retrieving records for one parent form dependent item

Hello,

I'm confused with my latest issue in Access 2013 because at one point in time it's worked exactly as intended. I'm putting together a pretty large database application so finish one piece once it's working to work on something else; it worked when I finished developing it but now that I've returned it no longer works as designed.

I have a form that contains 2 subforms:
  1. tblItemTypesItemColorJunction subform1
  2. tblItemDescription subform

#1 is populated with a query via the Property Sheet | Form | Data | Record Source. The Recordset Type of each subform is Dynaset and I'm sure all other properties are the defaults.

Capture0.jpg
Capture.jpg

This form populates just fine with the entire contents of tblItemTypesItemColorJunction.

The idea is that when you select a record in subform #1 then subform #2 is populated with those records that belong to this. In my database every item in subform #1 has at least one record that should appear in #2. However, only the 1st item in #1 populates #2 and I don't understand why.

Initially I had a query in Property Sheet | Form | Data | Record Source that used the ItemTypesItemColorJunctionID from #1 to pull the correct record but replaced this with VBA code in the OnCurrent event of #1 as follows in an attempt to debug:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.     'Confirm we're getting the correct ID from subform #1
  3.     MsgBox ItemTypesItemColorJunctionID
  4.     Dim GetQuerySQL As String
  5.     Dim RST As dao.Recordset
  6.     Dim iItemDescriptionID As Integer
  7.  
  8.     'query to get records based on item selected in subform #1
  9.     GetQuerySQL = "SELECT tblItemDescription.ItemDescriptionID, tblItemDescription.tblItems_ID, tblItemDescription.tblItemTypesItemColorJunction_ID, tblItemDescription.QtyInPackage, tblItemDescription.tblUnitOfQuantity_ID, tblItemDescription.Description, tblItemDescription.Price, tblItemDescription.Months, tblItemDescription.tblItemVendor_ID, tblItemDescription.WebPage, tblItemDescription.ShippingFee, tblItemDescription.SalesTax " & _
  10.     "From tblItemDescription " & _
  11.     "WHERE (((tblItemDescription.tblItemTypesItemColorJunction_ID)=" & ItemTypesItemColorJunctionID & "));"
  12.  
  13.     'this is just here for debugging to ensure that the record selected in subform #1 returns records that should appear in subform #2 - and it does in every instance!
  14.     Set RST = CurrentDb.OpenRecordset(GetQuerySQL)
  15.  
  16.     With RST
  17.         'loop through all tblEventTasks records with current EventTaskID and return TaskQuantity
  18.         Do While Not .EOF
  19.             'get TaskQuantity
  20.             iItemDescriptionID = !ItemDescriptionID
  21.  
  22.             .MoveNext
  23.         Loop
  24.     End With
  25.  
  26.     'set record source property of subform #2 and requery and form should contain same records as would be found in above Do While loop - but they're not???
  27.     Me.Parent![tblItemDescription subform].Form.RecordSource = GetQuerySQL
  28.     Me.Parent![tblItemDescription subform].Requery
In the above code I set the query and then have a Do While loop which I can step through to confirm that, based on the record selected in subform #1, records are found that should be displayed in subform #2 - and in every case they are. After the Do While loop I set the RecordSource of subform #2 to the query and force a requery - when return to the form subform #2 should contain as many records as found in the Do While loop code but, unless it's the first subform #1 record that was selected, 0 records are displayed.

Any advice greatly appreciated.

Thank you.
Aug 27 '15 #1

✓ answered by jforbes

You may want to check the LinkMasterFields and LinkChildFields Properties of the SubForm to make sure they are blank.

4 2232
jforbes
1,107 Expert 1GB
You may want to check the LinkMasterFields and LinkChildFields Properties of the SubForm to make sure they are blank.
Aug 27 '15 #2
jforbes,

I cleared those 2 cells and everything worked - thank you!

Some related questions if I may:

I removed the OnCurrent event from subform #1 so the VBA code to populate subform #2 was omitted and reinstated the query in the Record Source field of subform #2 but subform #2 returned to not being populated, even for the first item in subform #1. I reinstated the OnCurrent event to subform #1 with all code commented out except that to requery subform #2 and still no content in subform #2. Do you know why this might be the case? The query was exactly the same as in the VBA code with the exception of the WHERE clause so I guess this could be the issue:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblItemDescription.ItemDescriptionID, tblItemDescription.tblItems_ID, tblItemDescription.tblItemTypesItemColorJunction_ID, tblItemDescription.QtyInPackage, tblItemDescription.tblUnitOfQuantity_ID, tblItemDescription.Description, tblItemDescription.Price, tblItemDescription.Months, tblItemDescription.tblItemVendor_ID, tblItemDescription.WebPage, tblItemDescription.ShippingFee, tblItemDescription.SalesTax FROM tblItemDescription WHERE (((tblItemDescription.tblItemTypesItemColorJunction_ID)=[Forms]![frmMaintainItems]![tblItemTypesItemColorJunction subform1].[Form]![ItemTypesItemColorJunctionID].[Text])); 
Without a debugger how is one supposed to debug queries?

With the VBA method fully reinstated and functioning well, the first time I open the form I get the following error:
Capture.JPG

From looking online it appears to be caused by the subform #1 OnCurrent event being fired before subform #2 has been built (if you click End and return to the parent form everything works just fine for any subform #1 record selected). I've not been able to find a solution to this. Do you have any advice - maybe how to delay the OnCurrent event or put a check in the OnCurrent code to confirm that subform #2 has been built?

Finally, with my VBA in place, if I add a record using subform #2 it isn't added to the database (which I seem to recall did occur when the SQL in the Record Source field was functioning correctly). If I'm unable to get the Record Source method working again, would the recommendation be to add an event to subform #2 with an APPEND query?

Thank you to you or anybody else for their continued assistance.

Jason.
Aug 28 '15 #3
jforbes
1,107 Expert 1GB
There are three ways to populate a SubForm with records that I can think of:
  1. Define the Subform with a Static RecordSource then use a the Master/Child Linking to link the SubForm on a Foreign Key to the MainForm to allow Access to maintain the SubForms Filter.
  2. Define the SubForm with a Static RecordSource, then in the MainForm, set the SubForms Filter Property to limit the records of the SubForm.
  3. Define the SubForm with whatever RecordSource is handy, then the MainForm replaces the RecordSource of the SubForm with the full Select Statement.

The First option is the Default and it was probably what you started with. It works well if you can use it. It can also take care of populating the Foreign key for you in the SubForm when adding records.

The Second and Third options are great if the filtering is more complex than the basic Master/Child link on a FK. Of these options, I usually use the Second option of Filtering the Records as it seems quicker. To me it feels like it is less disruptive to Access as it doesn't have to rebind the fields on the SubForm, just get new data.

The Second option can be done on the MainForms OnCurrent or OnLoad events without throwing an error. Since I don't use the Third Option, I'm really not sure how or why the error is occurring.

When Inserting Records with the Second or Third options, I'm pretty sure you can still get the UI to allow you to add records through the SubForm. But, you may need to provide a Default (from the MainForm) for the FK as Access can't populate this for you in the scenario. An easy way to do this is to set the Default for the SubForm FK to a Function call that looks up the MainForm's FK. It sounds confusing, but it really isn't.

I think the last thing to address it about the Debugging of SQL. If you were to put a breakpoint on line 14 of your code, you could then type ?GetQuerySQL into the immediate window and Access will spit out the contents of your SQL String. You could then copy that SQL string onto your clipboard, then paste it into a new Query Editor (SQL View of this) and attempt to run it. ...That is SQL Debugging in Access.
Aug 28 '15 #4
Hi jforbes,

Thank you for all the additional detail and sorry for delay in coming back to you. Following your explanation of the 3 different scenarios I was able to determine the root cause of my issue and resolve.

Unfortunately I don't recall the exact Form Properties I had prior to going down the VBA route in an attempt to get the form to work properly so don't know where I initially went wrong, but the key to the fix was to create a text box on the Parent form to contain the Index from subform #1 and enter this into the Link Master Fields input of subform #2 (and eliminate all events & VBA code).

With this done the two subforms interact beautifully for both read & write.

Thanks again for your help.

Jason.
Aug 31 '15 #5

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

Similar topics

25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
2
by: c.kurutz | last post by:
Hello everyone. I have a problem with looking up pricing. Here is what I have so far: TABLES tblItems: itemid itemdescription itemunit (each, roll, square foot) location1price
1
by: dohminator | last post by:
Hello Experts, I have two systems written in Access 2003 that I'm in the process of merging. A user will be given a specific screen depending on his user name. I have a form (let's call it...
6
by: solom190 | last post by:
Okay this is the situation I have two forms and they have a 1:M relationship. I don't have enough space screen-wise to do a traditional "drag form to form" to create a subform so what I did was...
5
by: hbaf208 | last post by:
I have a combobox on a subform that is based on an SQL that uses a listbox on the unbound parent form as the criteria. When the form is first loaded, it works perfectly, limiting the dropdowns to...
0
by: sudhirb | last post by:
Hello I am using MS Access 2003. I have a parent form based on parent table which contains unique ID field( primary key) which i enter first on the parent form. Parent table also contains fields...
21
sueb
by: sueb | last post by:
I have a form/subform arrangement where the parent form has a Patient_Type that I want to translate into a Hospital_Category based on an existing lookup table. The relationship between Patient_Type...
3
sueb
by: sueb | last post by:
I have a parent form (Patients) that has a subform (Accounts Subform). On my parent form is a button that opens an Explorer window pointed to a location determined by the control ChartNum. A...
5
by: Sedrick | last post by:
What would be the best way apply a filter to a subform and then apply that recordset to the parent form? For example. Parent Form - Groups: ID, GroupName Subform - Members: MemID, GrpID,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.