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

inconsistent behavior of form / subform link

Hello all,
I am hoping that you can help me with a problem that has occurred numerous times on various forms in the two databases that I am working on. When I first add a subform to a form linking child and master fields (primary key and foreign key), the subform will automatically update when the master field on the main form is changed (it is in a combo box). However, when I work more on the form - making it a bit more complicated, adding code, search boxes, etc. The subform stops updating, but rather stays on one record even when the master field is changed (when a new value is selected from the combo box that has the master field as its bound column).

I have tried using Me.Requery in the AfterUpdate Event of the combo box, but this requeries the entire form's contents, resetting it to the first record. I have also tried Me.[SUBFORMNAME].Requery, but this does not seem to work.

Does anyone know why this feature – the automatic link between child and master fields does not always work? Or why requerying the subform itself does not work? I would appreciate any suggestions.

Many thanks, in advance.
Bridget
Oct 23 '08 #1
12 3000
NeoPa
32,556 Expert Mod 16PB
I'm not very experienced in this area I'm afraid Bridget, but I think you should probably be looking more closely at axactly what you are doing when you think it's no longer working. My guess is that what you are doing makes it stop working, rather than it being a flaw in the system itself. Pay particular attention when it next happens and find out what exactly is occurring. That way someone may be able to explain why for you.
Oct 24 '08 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi Bridget. I agree entirely with NeoPa - this is something that has been introduced by the actions you are taking. In extensive use of subforms I have never had issues relating to incorrect synchronisation of master-detail records the way you describe.

Could you clarify what you mean when you talk about selecting a new primary key value for the master field? It is most unusual to have to change the primary key for the one-side record at all, particularly when there are already many-side records in existence. It is not the job of a subform to change secondary keys in such circumstances. Even with Cascade Updates on for your 1-many relationship concerned such a change will not be reflected correctly into many-side records automatically, as Cascade Updates handles modifications to existing key values, not wholesale replacement of those values.

The parent-child relationship of a main-form sub-form combination takes care of the creation of related secondary keys automatically. However, if you change the primary key to a different one subforms cannot of themselves change the secondary key values to something else - and it is not reasonable to expect them to.

In any event, if you have relational integrity checking set for the relationship concerned (and you should) Access will simply not allow you to replace a primary key if to do so would leave 'orphaned' many-side records that no longer match to a one-side record.

-Stewart
Oct 24 '08 #3
Thank you NeoPa and Stewart for your responses. I think this may reveal a flaw in my understanding of how forms can /should connect. Let me give a specific example and see if that helps to shed some more light on it.

I have two forms “frmAddSites” and “sfrmAddr1” that are linked in a form/subform relationship:

“frmAddSites” has a control source, tblSites.
The table, tblSites, includes:
SiteID – primary key, autonumber
SiteName – text
Addr1LKUP – number, foreign key to an address table called “tluAddr1”.
(and a few other descriptive fields)

The table, tluAddr1, includes the fields:
Addr1ID – primary key, autonumber
Addr1 – text
ZipCodeLKUP – number, foreign key to a zipcode table tluZipCode.

The table, tluZipCode, holds the city and state information for each zip code:
Zipcode – primary key
PostalCityLKUP – number, foreign key to tluPostalCity
StateLKUP – number, foreign key to tluState
“sfrmAddr1” has a control source, qryAddr1.
The query, qryAddr1, holds all of the address information from tluAddr1 and associated lookup tables (see above):
Addr1ID
Addr1
PostalCity
State
ZipCode
“frmAddSites” has the following controls:
txtSiteID - text box for SiteID
txtSiteNm - text box for SiteName
cboAddr1 - combo box for Addr1LKUP.
The row source for the cboAddr1 is qryAddr1 (bound column is Addr1ID, visible column is Addr1)

“sfrmAddr1” has the following controls:
txtAddr1ID - text box for Addr1ID
txtCity - text box for PostalCity
txtState - text box for State
txtZipCode - text box for ZipCode

The two forms are linked by
Master Field Addr1LKUP
Child Field Addr1ID

The functionality I am hoping for is to be able to choose an address from the cboAddr1 on the main form (frmAddSites) and have the subform (sfrmAddr1) automatically display the associated City, State and ZipCode.

The code Me.Requery in the AfterUpdate Event of cboAddr1 requeries the entire form, but also jumps to the first record.
The code Me.sfrmAddr1.Requery does not seem to do anything.
If I change the selection in cboAddr1 on a record, then move to another record, and go back to the record where I changed the combo box selection, the correct record on the subform is displayed.

Does this make more sense?
Is this inherently a problematic way to link a form and subform?
Is there a way to make the subform display the correct record when the linked field changes in the main form?

Thanks again,
Bridget
Oct 27 '08 #4
NeoPa
32,556 Expert Mod 16PB
That's a lot to go through Bridget, but I did see your .Requery request for the subform (which failed) and I think that's related to referencing the item correctly. Check out (Referring to Items on a Sub-Form).
Oct 27 '08 #5
Thanks, NeoPa. Sorry for all the details before, I was trying to be thorough, but maybe it was too much...

I have looked at the "referring to items on a sub-form" and other postings about properly referencing subforms. Using these, I have tried many iterations of the requery code without success. I would like to requery the entire subform, and so my understanding is that I want to reference the form as a control of the main form, rather than a control within the subform. Here is what I have tried, without success.
Me.sfrmAddr1.requery
Me!sfrmAddr1.requery
Forms![frmAddSites]![sfrmAddr1].requery
Forms![frmAddSites]![sfrmAddr1].Form.requery

Do you see an obvious mistake in my referencing of the subform for requery?

I found the posting: http://bytes.com/forum/thread628289.html
in which someone has similar problems requerying a subform and he ends up scrapping requery altogether and instead resets the recordset clone. His code is more complicated than I need (and frankly than I understand.), though I have had success using code to set a recordset clone for other purposes (i.e. using a combo box to filter a form). I am going to try this tactic to see if it works better than the requery option.

Thanks again for any help.
Bridget



That's a lot to go through Bridget, but I did see your .Requery request for the subform (which failed) and I think that's related to referencing the item correctly. Check out (Referring to Items on a Sub-Form).
Oct 27 '08 #6
NeoPa
32,556 Expert Mod 16PB
...
Forms![frmAddSites]![sfrmAddr1].Form.requery
...
This would be correct if the SubForm/SubReport control on your form were named thus. I suspect this is actually the name of the form that you put IN the subform control.

Find the name of your SubForm/SubReport control on [frmAddSites] and use that instead.
Oct 27 '08 #7
Thanks, NeoPa.
Sigh. I just think I'm getting the hang of Access/vba and then realize how much I don't really get...
Thanks again
Bridget
Oct 29 '08 #8
NeoPa
32,556 Expert Mod 16PB
Did you manage to get it to work Bridget?
Oct 29 '08 #9
Thanks for asking.
My subform and the name of the subform control are both "sfrmAddr1". Is it problematic to name the subform control the same as the subform itself?

I am making some progress in understanding requerying, but this issue is still a mystery: how to call a requery of a subform from the AfterUpdate_Event of a control in the main form.

If I requery a subform from the subform itself (using Me.Requery), it works fine. However, I cannot get it to work by calling it from the main form.

I welcome more suggestions and will post if/when I find a solution.
Bridget
Oct 30 '08 #10
NeoPa
32,556 Expert Mod 16PB
My subform and the name of the subform control are both "sfrmAddr1". Is it problematic to name the subform control the same as the subform itself?
I'm only aware of the problem of following (understanding) the code. If it's easier for you I can't think it's too much of an issue. Generally it's a bad idea (using a name for an object that already has a meaning), but I can see that for something like a subform it makes a sort of sense.
I am making some progress in understanding requerying, but this issue is still a mystery: how to call a requery of a subform from the AfterUpdate_Event of a control in the main form.

If I requery a subform from the subform itself (using Me.Requery), it works fine. However, I cannot get it to work by calling it from the main form.
Have you tried :
Expand|Select|Wrap|Line Numbers
  1. Call Me.sfrmAddr1.Form.Requery
Oct 30 '08 #11
Thanks, NeoPa.
Unfortunately using "Call" didn't work either... I have ended up doing a Requery of the whole form. Then because the whole form Requery results in the form jumping to the first record, I have added a bookmark to return to the record that was being edited. It is frustrating that it cannot be more straightforward, but this works, so I'm going with it!

Here is my code to Requery subforms from the AfterUpdate Event of a combo box in the main form. (Really it Requeries the whole form and all of its objects.)

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboAddr1LKUP_AfterUpdate()
  2. 'start error check.
  3. On Error GoTo Err_cboAddr1LKUP_Click
  4.  
  5. Dim intSiteID As Integer
  6.  
  7.   intSiteID = txtSiteID
  8.  
  9.   Me.Requery
  10.  
  11.   'set recordset clone on main form
  12.   Set Rs = Me.Recordset.Clone
  13.   'find record that matches stored ID
  14.   Rs.FindFirst "[SiteID] = " & intSiteID
  15.   'set bookmark on main form to the record that matches the stored record.
  16.   Me.Bookmark = Rs.Bookmark
  17.  
  18. 'Exit the sub
  19. Exit_cboAddr1LKUP_Click:
  20.     Exit Sub
  21.  
  22. 'Exit the error check.
  23. Err_cboAddr1LKUP_Click:
  24.     MsgBox Err.Description
  25.     Resume Exit_cboAddr1LKUP_Click
  26.  
  27. End Sub
  28.  

Oh, and good tip about having the subform object name be different from the subform itself to avoid confustion. I plan to change that for my subform object names. Is there a naming convention typically used for subform objects?

Thanks again,
Bridget
Nov 3 '08 #12
NeoPa
32,556 Expert Mod 16PB
I have to admit at this stage that I have very little experience with subforms.

I would think that 'sfm' (or 'sfrm') would be a good prefix. I tend to use 'frm' for all forms, whether or not they are intended as subform forms. If that doesn't suit you, then what about 'sfc' for Sub Form Control?
Nov 4 '08 #13

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

Similar topics

5
by: Michelle | last post by:
Hi all Is it possible to have 2 subforms on a form but have the second subform linked to the 1st subform. I will explain the situation. My form contains details of teams and the shift the...
1
by: Richard Hollenbeck | last post by:
I'm getting errors. Access is telling me that it can't add a record. I have a table of college courses and a linked table of groups of activities in that course as a linked table (one course to...
1
by: Steve Miles | last post by:
I've got a form with a combo box and begin/end dates. When any of the three are changed I set a subform's recordsource so the records returned are filtered based on the three fields. The syntax...
4
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the...
14
by: alwayshouston | last post by:
Hi All! I am working on this very small database and I am confused in the designing a simple form. I only have three tables in the database. First Table: tblExpense Columns: ExpenseID ;...
4
by: jcazmail-groups | last post by:
I have a child form that has a combo box whose underlying query needs to be filtered by a value from a combo box on the parent form. I have succeeded in doing this by putting the following SQL in...
3
by: virtualgreek | last post by:
Dear all, I have a scenario that is driving me nuts. (MS Access 2003) I have a form/subform (Continuous form) where it gets its data from tables Order and Order_Details. In the footer...
2
by: darnel | last post by:
I have 4 hierarchical tables and want to display it all together as a form and 3 subforms, when subform displays (and allow to add/edit) only relevant items from each superior subform. Tables are:...
4
by: sparks | last post by:
TRYING TO USE SHORT NAMES might make more since. I have a main form and its tied to tblmain subform1 and it is tied to tbl1 subsubformA on subform1 tied to tbl2 MAIN---tblmain----autoid...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.