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

Changing a subform on a main from, from the original subform

Hi

I have created a database that uses a menu form which selects difference subforms depending on the button click. That's no problem.

I've now been asked to update an existing database to be in the same format. This database has about 45 different forms so I can’t fit them all on a single menu besides which in many it's a cascade, you have to go from one form to the next which in the original database I achieve by simply opening the next form. So in the updated version I need to find a way to click on a button on the subform which closes that subform and opens another. Thus far the closest I can get is:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command8_Click()
  2.  
  3. ‘ close the sub form
  4.     DoCmd.Close
  5. ‘close the main form
  6.     DoCmd.Close
  7.  
  8. ‘Open the main form
  9.     DoCmd.OpenForm "frmUIMain"
  10.  
  11. ‘change the subform
  12. Dim strSourceSubForm As String
  13.  
  14. strSourceSubForm = "frmUpdate"
  15.  
  16. With Me!Child20
  17.     .SourceObject = strSourceSubForm
  18.     .LinkMasterFields = ""
  19.     .LinkChildFields = ""
  20.     .Requery
  21.  
  22. End With
  23. End Sub
  24.  
But when I run this, the error I get is that it can't find the Child20 to update. I suspect this is because it has moved from frmUIMain into the default subform already, but I'm not sure. Can anyone suggest a way forward? Or confirm it can't be done?

Thanks
Apr 26 '12 #1

✓ answered by TheSmileyCoder

I will start with a few general comments on your code:
Expand|Select|Wrap|Line Numbers
  1. close the sub form
  2. DoCmd.Close
  3. close the main form
  4. DoCmd.Close
  5.  
While this works I would consider it bad practice to use it to close anything but the form itself. I also dont think you can close a subform, but I have never really looked into it.

Expand|Select|Wrap|Line Numbers
  1. Docmd.Close acForm, Me.Parent.Name
could be an alternative.



Expand|Select|Wrap|Line Numbers
  1. With Me!Child20
  2.   .SourceObject = strSourceSubForm
  3.   .LinkMasterFields = ""
  4.   .LinkChildFields = ""
  5.   .Requery
  6. End With
  7.  
How can you expect ME to work, when you have closed the object?
Furthermore
Expand|Select|Wrap|Line Numbers
  1. LinkMasterFields 
and
Expand|Select|Wrap|Line Numbers
  1. LinkChildFields 
can ONLY be set in design view.
Expand|Select|Wrap|Line Numbers
  1. .Requery 
is not needed, a form is queried when it is loaded.



An alternative approach:
Expand|Select|Wrap|Line Numbers
  1. With Forms("frmUIMain").Child20
  2.   .SourceObject = strSourceSubForm
  3. End With

I would suggest a rename of child20 to for example ctrlFormContainer or something else meaningfull!.

Hope you can use this post as constructive criticism!

4 1500
TheSmileyCoder
2,322 Expert Mod 2GB
I will start with a few general comments on your code:
Expand|Select|Wrap|Line Numbers
  1. close the sub form
  2. DoCmd.Close
  3. close the main form
  4. DoCmd.Close
  5.  
While this works I would consider it bad practice to use it to close anything but the form itself. I also dont think you can close a subform, but I have never really looked into it.

Expand|Select|Wrap|Line Numbers
  1. Docmd.Close acForm, Me.Parent.Name
could be an alternative.



Expand|Select|Wrap|Line Numbers
  1. With Me!Child20
  2.   .SourceObject = strSourceSubForm
  3.   .LinkMasterFields = ""
  4.   .LinkChildFields = ""
  5.   .Requery
  6. End With
  7.  
How can you expect ME to work, when you have closed the object?
Furthermore
Expand|Select|Wrap|Line Numbers
  1. LinkMasterFields 
and
Expand|Select|Wrap|Line Numbers
  1. LinkChildFields 
can ONLY be set in design view.
Expand|Select|Wrap|Line Numbers
  1. .Requery 
is not needed, a form is queried when it is loaded.



An alternative approach:
Expand|Select|Wrap|Line Numbers
  1. With Forms("frmUIMain").Child20
  2.   .SourceObject = strSourceSubForm
  3. End With

I would suggest a rename of child20 to for example ctrlFormContainer or something else meaningfull!.

Hope you can use this post as constructive criticism!
Apr 26 '12 #2
Thanks for the response, I'll give these suggestions a go and post if it works.

Only one point I have to raise, TheSmileyCoder said:

"How can you expect ME to work, when you have closed the object? "

I expected it to work because after the close I had in fact opened the main form with:
Expand|Select|Wrap|Line Numbers
  1.     DoCmd.OpenForm "frmUIMain"
As I said, I'll give it a go and let you know.

Thanks again
Apr 26 '12 #3
Hi

Thanks to TheSmileyCoder, I have followed his advise and now got it working. The code ended up as:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command8_Click()
  2.  
  3. Dim strSourceSubForm As String
  4. strSourceSubForm = "frmUpdate"
  5.  
  6. With Forms("frmUIMain").Child20
  7.   .SourceObject = strSourceSubForm
  8. End With
  9.  
  10. End Sub
Thanks again for the assistance.
Apr 26 '12 #4
NeoPa
32,556 Expert Mod 16PB
Gail:
I expected it (Me.) to work because after the close I had in fact opened the main form with:
Me. refers to the form associated with the module that the code is running within. Although it cannot close until the code has completed executing, it isn't logical to refer to items on the form after a close has been requested. Me. will not allow you to access items on a separate form, no matter which form has the focus.

Subforms cannot be closed. They can be replaced, and also unloaded (by setting the .SourceObject of the subform control to something else), but never closed.
Apr 27 '12 #5

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

Similar topics

4
by: chris vettese | last post by:
Is there a way I can grab a value from my main form and use it in a calculation on a subform?
1
by: Susan | last post by:
I have a bound form and subform and am trying to enter the data in both forms programatically. The Linkmaster and Linkchild properties are set. My intent is to be able to look at the data before it...
1
by: dBNovice | last post by:
Please help! I have 3 forms: Task, Subtask, Elements. Elements is a subform of Subtask and Subtask is a subform of Task. I am able to navigate from Task to Subform to Element and from Element to...
1
by: Eric | last post by:
There are two sub forms in main form. One has values: Outlet Equipment 2 ABCBCBD 3 DKJDKJD 4 KDJKDJK Second has also a variable name outlet. I need help how to i display the...
0
by: keri | last post by:
I have a form for a user to enter appointments with a customer. Currently - The user enters search terms (eg name, address, town etc) in text boxes - and this filters a subform (all customers...
1
by: Drum2001 | last post by:
Hello All, This seems like it would be simple. Is there a way to have an "After Update" event from Subform1 trigger an "add new record" on Subform2? I see posts to go from Mainform to...
3
by: Richnep | last post by:
Hi all, I have tabbed subforms where I need to copy one field value from one subform over to another subform. Although I can run an update query to accomplish this I would like to do it through...
8
by: Neekos | last post by:
I have a form called frmPaxInfo, and on this form is a subform called subfrmPaxInfo. This subform (which is filtered by cabin) contains the passengers name along with all charges associated with each...
2
by: John Torres | last post by:
I have an existing Database which I have modified to new dbase with the same structure (1 Main and 2 subforms- 2nd subform corresponds to the 1st subform) but I have changed the names/ labels (not...
1
by: cmbirk | last post by:
I have a main form that contains 2 subforms. One subform is a regular single form, but the other is a multi-item subform. Both forms have a project ID field that I would like to link. What I would...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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
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...

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.