Need MS Access VBA code to requery subform while open in a main form.

stephenlecomptejr
stephenlecomptejr used Ask the Experts™
on
I need the correct syntax for a Microsoft Access VBA set of code that would allow a main form - requery a subform's information.
This used to work for me in the past (Access 2000-2013) but I'm using Office 365 now.

Forms!fMainMenuTimeSheetMonthly!sfTimeSheetWeekly.Form.Requery
Forms!fMainMenuTimeSheetMonthly!sfTimeSheetWeekly.Visible = True

Open in new window


Primarily I have a lot of Me.Controls("txtBox1") in the subform to control from the main form.

This works within the subform itself:

Me![sfTimeSheetWeekly].Requery
Me![sfTimeSheetWeekly].Visible = True

Open in new window


But I need to manipulate it from outside the main form.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012
Commented:
This:

Forms!fMainMenuTimeSheetMonthly!sfTimeSheetWeekly.Form.Requery

 is correct.   Just watch that sfTimeSheetWeekly is the name of the sub form *control*, not the name of the subform.

Jim.
Mark EdwardsChief Technology Officer
Commented:
I'm using Office 365 Access (2013 and 2016) and I have no problem requiring a subform with this:
Me.sfTimeSheetWeekly.Form.Requery

If this doesn't work for you, I'd like to say tuned and see why.  I don't know of any reason why a change from store-bought Access to O365 Access would change this behavior.
Mark EdwardsChief Technology Officer

Commented:
Come to think of it, I think I remember an issue between using the bam (!) and using the dot (.) between form and control names.  (A change?)
I've also had this work fine:
Forms("fMainMenuTimeSheetMonthly").sfTimeSheetWeekly.Form.Requery
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
You know what it actually was.  I was calling this from within the subform only..... and thus the main form wasn't open... thus the error... can't find the main form.

My apologizes.  But if it comes up - at least this will be in the search

Author

Commented:
Appreciate the replies.  Sorry for the confusion.  Should have posted (and read) the error message - not just say it wasn't working.  LOL.

Author

Commented:
Hey Mark,

I see how you could pass a variable name as the form with that above code doing:

Dim sFormName as String
sFormName = "fMainMenuTimeSheetMonthly"
Forms(sFormName).sfTimeSheetWeekly.Form.Requery 

Open in new window


But how do you also pass the subform name along with the formname in as a variable with the above?
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
<<But how do you also pass the subform name along with the formname in as a variable with the above>>

  You want control name, and it would be:

Forms(sFormName)(ssfFormName).Form.Requery

and a small fact; under the hood, Access converts all references to this format, which it then executes.   So using this format is actually a tad faster as it won't have to translate.

and a lot of objects will support that syntax.

Jim
Mark EdwardsChief Technology Officer

Commented:
If you want to combine them as one name, you could set the subform as an object variable.  For example:
Dim objSubform as form
Set objSubform = Forms("fMainMenuTimesheetMonthly").sfTimesheetWeekly.form
Then all you have to do is use the object variable:
objSubform.Requery

I've tested it and it works!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial