Computer Learning Zone CLZ Access Excel Word Windows

Beware of the Vulcan greed for knowledge.

-79th Rule of Acquisition
 
Home   Courses   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Back to Visitor Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
VBA Problem with Subform
Koen Delmot 

3 months ago
Let me give you some context. I have 2 forms, DossierF and PerceelF. Both work fine as a seperate form, no errors.

I now tried to make PerceelF a subform from DossierF. The key between both form is a field called DossierID and it is present in both tables I used to base my forms on (it is also a hidden field on my form).

I dragged PerceelF into DossierF but Access always gives me a message that it cannot find PerceelF. When I go to debug, I end up at the following VBA-code that I entered with the "on load" event.
This is the code that is supposedly give the problem:

Private Sub StandstillYN()
    If Standstill = True Then
        Forms.perceelf.Geldigheid = DMax("verzendingpublicatie", "verbintenistermijnq") + Forms.perceelf.Verbintenistermijn + 15
    Else: Forms.perceelf.Geldigheid = DMax("verzendingpublicatie", "verbintenistermijnq") + Forms.perceelf.Verbintenistermijn
    End If
    Forms.perceelf.Geldigheid.Requery
    
End Sub


"Standstill" is a checkbox, "geldigheid" is a non-bound field on my form (it does not appear in the table), "verbintenistermijn" is a field in PerceelF with a numerical value in it.
"PerceelF" still is called "PerceelF" when it is used as a subform in DossierF.

I tried asking chatgpt and copilot for advice and i made some changes to the code but I still keep getting the error. Couldn't find any answers going through Richards videos.

This is the last code I tried based on what chatgpt gave me as input.

Private Sub StandstillYN()
    DoCmd.GoToControl "perceelf"
    DoCmd.GoToControl "opmerkingperceel"
    
    If Standstill = True Then
        Forms!dossierF!PerceelF.Form!Geldigheid = DMax("verzendingpublicatie", "verbintenistermijnq") + Forms!dossierF!PerceelF.Form!Verbintenistermijn + 15
    Else: Forms!dossierF!PerceelF.Form!Geldigheid = DMax("verzendingpublicatie", "verbintenistermijnq") + Forms!dossierF!PerceelF.Form!Verbintenistermijn
    End If
    Forms!dossierF!PerceelF.Form!Geldigheid.Requery
    
End Sub


Donald Blackwell  @Reply  
       
3 months ago
If your running StandstillYN() from the Dossier form, you could remove some excess code:
Details
Private Sub StandstillYN()
    DoCmd.GoToControl "PerceelF"
    DoCmd.GoToControl "opmerkingperceel"

    If Standstill Then
        PercellF.Form!Geldigheid = DMax("verzendingpublicatie", "verbintenistermijnq") + PerceelF.Form!Verbintenistermijn + 15
    Else
        PerceelF.Form!Geldigheid = DMax("verzendingpublicatie", "verbintenistermijnq") + PerceelF.Form!Verbintenistermijn
    End If
    PerceelF.Form!Geldigheid.Requery

End Sub


Beyond that, I would just start commenting out the lines until you see which one is throwing the error. For Example, comment out everything from just after your go to control commands to just before the end sub and see if it throws an error and then just reduce the comment block until the error shows again.
Koen Delmot OP  @Reply  

3 months ago
Thanks Donald for your answer.

What you suggest may help with this specific error on an "on load" event but it will not help me completely because I have other VBA-code that generates the same problem that cannot be placed in DossierF. For example, On PerceelF, I open a form with a button and after updating the info on said form, the same StandstillYN() code runs because my input might have affected the date in "geldigheid". I run into the same problem there so I need to understand what I'm doing wrong so I can fix these kind of errors.

I tried your suggestion of commenting out the lines and try to do a step-by-step fix. This is the code that got me the fartest but now Access gives the yellow line to the "end sub" line to indicate that my error is on that line.

Private Sub StandstillYN()
    DoCmd.GoToControl "perceelf"
    DoCmd.GoToControl "opmerkingperceel"
    
    If Standstill = True Then
        Forms.PerceelF.Geldigheid = DMax("verzendingpublicatie", "verbintenistermijnq") + Forms.PerceelF.Verbintenistermijn + 15
    Else: Me!Geldigheid = DMax("verzendingpublicatie", "verbintenistermijnq") + Forms!dossierF!PerceelF.Form!Verbintenistermijn
    End If
    Me!Geldigheid.Requery
    
End Sub


I messed around with my code so much and tried so many different things over the past 3 weeks that I've run out of ideas to try.
Donald Blackwell  @Reply  
       
3 months ago
If the highlighted error just moves to the end of the sub, then I would look before the sub to make sure there isn't some "unfinished" blocks of code that might be causing the problem. What you describe seems similar to there being another procedure that doesn't have an "End If", or "Wend" or "End Sub" somewhere so it's running straight through to your "StandstillyYN()" routine. But without seeing the code module in full, I can't say for sure.

As for getting it to run from multiple sources. That too will probably resolve itself with appropriate notation if this error gets cleared up.
Koen Delmot OP  @Reply  

3 months ago
I don't think it is a problem of a procedure somewhere else giving a problem because I don't get any errors when I run both forms seperately. The problem just comes up when I try to make it a subform.
I'm doing something wrong in my StandstillYN()-code, but I can't seem to figure out what.
Sami Shamma  @Reply  
             
3 months ago
Donald Blackwell  @Reply  
       
3 months ago
As Sami said, we need more specificity. We need to see your full code and possibly your form(s) in design view to be able to help. Otherwise, we're just guessing and not helping.

Another guess to look at without more information:
At the top of your code, in your declarations area, do you have:

Option Compare Database
Option Explicit


If not option compare database, VBA might be doing a stricter comparison of code, i.e. case sensitive if it's option compare binary and you don't have option explicit, you could have a misspelled variable or control name not being detected that we don't know about.

You've probably already checked that, but, since we can't see anything else, those are the only other guesses I can provide.
Koen Delmot OP  @Reply  

3 months ago
I can't post pictures but I've uploaded my full "onLoad" event code and also screenshots of my forms. You can see them through the following link. If you need more info, let me now. I appreciate your effort to help me.

https://www.dropbox.com/scl/fi/w2eslm0r7sd9dc0dpcl4z/PerceelF-OnLoad-Event-and-screenshots.docx?rlkey=t0h84v3tr1h26f1j96feyz9pd&st=sycpke9b&dl=0
Sami Shamma  @Reply  
             
3 months ago
@[Koen-Delmot] consider becoming a member
Donald Blackwell  @Reply  
       
3 months ago
@0[Koen-Delmot] So just trying to work through this after looking at your images and reviewing what you've said so far:

1) if you use DossierF and ParseelF as standalone forms, the code in PerseelF works without any problems - This is because the the form notation Forms.PerceelF is a top-level form that is open

2) If you use DossierF with ParseelF as a subform, the code faults - This is because Access can't find Forms.ParceelF because it isn't a top-level form

3) You also need the code to be able to be called from other form(s) that open from a button in ParceelF

So, the next question, will ParceelF, when your project is complete, always be a subform of DossierF? This will impact the notation needed, regardless of whether being called internally from ParceelF, from parent form DossierF, from DossierF as a standalone form, or any other form trying to interact with the code.

And a final, for now, follow-up question, the forms opened from ParseelF, are they just opened as standard forms or are they modal/dialog forms?
Koen Delmot OP  @Reply  

3 months ago
@Donald Blackwell
1) that is correct

2) That is correct. I hoped the GoToControl might solve the issue but it didn't.

3a) Yes, I use the same code in the "OnClose"-event of another form because my input on that form might have an impact on the value of Geldigheid. I'm hoping/assuming that if I can find the solution to my current problem, I'll be able to fix that as well

3b) Ideally, PerceelF will reamin a subform of DossierF if I can get it to work properly. I have other places in my database where I currently summon PerceelF but that would need to change to DossierF with PerceelF as the subform. But that is step 2, first I need to get step 1 right.

3c) It opens as a standard form, it isn't designed as a modal form.
Donald Blackwell  @Reply  
       
3 months ago
OK, here is what I think shouldwork in any case if this procedure is in ParceelF:

DetailsPublic Function StandstillYN()

    DoCmd.GoToControl "perceelf"
    DoCmd.GoToControl "opmerkingperceel"

    ' We'll set Geldigheid to the base value just to save logic later that way, we only have to modify what's different
    Me!Geldigheid = DMax("verzendingpublicatie", "verbintenistermijnq") + Me!Verbintenistermijn

    ' Only if Standstill is true do we have to change anything
    ' We don't need the " = true" because that is assumed if it's yes/no value
    If Standstill Then Me!Geldigheid = Me!Geldigheid + 15

    Me!Geldigheid.Requery

Exit Function

Donald Blackwell  @Reply  
       
3 months ago

Now, to use this, you simply call it as needed:

In ParceelF for example as an after-update event of the form, in the AfterUpdate event property of the field, just add "=StandStillYN()" or in VBA just StandStillYN

In DossierF, for example as part of VBA of the OnLoad event:
Me!PerceelF.Form.StandstillYN (assuming PerceelF is the name of the subform control)

In VBA of another open form:
If CurrentProject.AllForms("DossierF").IsLoaded Then
    If Not Forms("DossierF")!PerceelF.Form Is Nothing Then
        Forms("DossierF")!PerceelF.Form.StandstillYN
    End If
End If


Also, the last one can be even cleaner with a better function such as Richard's "IsFormLoaded" function which is available to TechHelp Gold Members
Koen Delmot OP  @Reply  

3 months ago
I tried adding the code but I keep getting errors, I added screenshots in the document above

https://www.dropbox.com/scl/fi/w2eslm0r7sd9dc0dpcl4z/PerceelF-OnLoad-Event-and-screenshots.docx?rlkey=t0h84v3tr1h26f1j96feyz9pd&st=sycpke9b&dl=0
Donald Blackwell  @Reply  
       
3 months ago
OK, first thing I need to apologize, somehow I had a typo in the StandstillYN function. the "Exit Function" line was supposed to be "End Function" which you added after. Technically, having it doesn't hurt, but it can be removed.

Beyond that, without having the database to look at and try to poke around to see if there's more being "lost in translation", the only other notes making me scratch my head:

1) just calling StandstillYN in your onload event as you originally were should work from within PerceelF

2) Just a reminder that the last code I gave (If currentproject...) would go in the close event of other forms opened by a button from PerceelF - it wouldn't be needed in perceelf itself

3) If Geldigheid is an unbound field with no default value and no control/rowsource, I'm not sure why the need. From what I can see in your pictures, it has no control source and no default value and you've indicated it is an unbound control. Requery retrieves the data from the recordsource but since there is no source (either table, query, or formula) it really does nothing. While it shouldn't be the cause of the error, it could be preventing that database from finishing loading everything since it's trying to requery a ghost.

4) Calling StandstillYN in both the on load and on current events is also redundant. On current typically will fire after on load when the form opens and again on every record change so there should be no need to duplicate the code.

5) Also, when DossierF loads, does it modify anything in PerceelF? The reason I ask is that the subform will load first so it might be better to add the call to StandStillYN to the on current event of DossierF if it adds any relevant data to the subform (Me!PerceelF.form.StandStillYN) so that you don't need to call it multiple times
Koen Delmot OP  @Reply  

3 months ago
There is no need to apoligise, I'm grateful that you're taking the effort to try and help me.

1) I tried it in PerceelF

2) It was added to the OnClose event

3) Geldigheid is the Dutch word for Validity. I want to show on the form till what date an offer is still valid. I don't need it in the table (unless it would fix my problem), it is just for an informational purpose.
ok, i can delete the requery, thanks for the tip.

4) Don't I need it on the OnCurrent event as well for when I switch between recors?

5) DossierF doesn't modify anything to PerceelF. All the code that impacts PerceelF, is placed in PerceelF itself.
Donald Blackwell  @Reply  
       
3 months ago
You can just put it in onCurrent as that fires when the form loads, too.

The most common events when a form open are discussed in Form Opening Events.

A quick summary of the order of events for a form with a subform:
1. Subform onopen - when the subform 1st opens, typically little or no data, can be cancelled
2. Subform onload - when the subform is open and has data, good for modifying layout or setting field values, can't be cancelled
3. Subform oncurrent - After the subform is loaded andcurrent record active. Also every time it moves to a different record
4. Form onopen - after the subform(s) load, again, typically little or no data, can be cancelled
5. Form onload - after form onopen and dataset is loaded (if a continuous form, typically all the data currently visible and maybe a bit more)
6. form onresize - after the form has actually rendered and any changes have been made to the size of the form. Also fires if the form is manually resized or via code
7. form onactivate - when the main form becomes the active window or when focus is changed back to the form if multiple forms are open
8. form oncurrent - when first record is loaded or when navigating between records

Richard also discusses the order events fire in in Quick Queries # 66.
Koen Delmot OP  @Reply  

3 months ago
There is no need to apoligise, I'm grateful that you're taking the effort to try and help me.

1) I tried it in PerceelF

2) It was added to the OnClose event

3) Geldigheid is the Dutch word for Validity. I want to show on the form till what date an offer is still valid. I don't need it in the table (unless it would fix my problem), it is just for an informational purpose.
ok, i can delete the requery, thanks for the tip.

4) Don't I need it on the OnCurrent event as well for when I switch between recors?

5) DossierF doesn't modify anything to PerceelF. All the code that impacts PerceelF, is placed in PerceelF itself

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Visitor Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 2/6/2026 8:41:49 PM. PLT: 1s