Does all the Forms have a VBA code assigned to them

SachAccess

Active member
Local time
Today, 05:59
Joined
Nov 22, 2021
Messages
421
Hi,

Please pardon my ignorance. I have a doubt. Does all the Forms have a VBA code assigned to them.
Can we have a form which has not VBA code assigned to it. Thanks.
 
when you first create the Form, there is no VBA in it.
you add it manually.
 
Hi @arnelgp thanks for the help. Is it necessary for a form to have VBA code.
If I am trying to study any form, is it safe to assume that it must have some VBA code assigned to it.
Or in another words, what is a method to check if the form has any VBA code assigned to it.

I have few forms and few VBA code modules in DB.
I have added break-point for each VBA module then am trying to click each form in the DB.
Some forms are taking me to related VBA code, some forms are simple opened without showing any code assigned to it.
That is why I have this confusion.

Have a nice day ahead. :)
 
It is not necessary/mandatory for a form to have VBA code.

If you create a new form from scratch, it won't have a code module created by defualt, so definately no code present;
1660292173169.png


You can test this for your self easily.
 
Their is a readonly form property called .hasModule that confirms whether or not there is a module.
 
Last edited:
What @Minty shows is the "Project Explorer" window of the VBA development environment. It shows all the places you "could" have code. You can have code in form and report modules, standard modules, and class modules. Code in form and report modules is encapsulated in that form and report and can only be referenced through the form or report. Standard modules allow you to share code throughout the project. Class models allow you to create objects that can have properties, events, and methods.
ProjectExplorer.jpg


I use the word "could" since it is possible to create any of these modules and not type (or delete) the code. I occasionally see it. But you can click on any of the icons in the treeview of the Project Explorer and see what code is in the module.
 
Although all VBA code will appear in code modules as explained above, it is also possible for forms to use saved and embedded macros.
Embedded macros are (for want of a phrase) pseudo-code based on a macro structure that is attached to form or control events.
These are shown in the form property sheet as shown below

1660311439229.png


Many experienced developers avoid using embedded macros
 
In forms and reports, code can only be executed as the result of an Event. In a truly compiled program, you write a MAIN section and can add one or more sections that contain subroutines. But for Access, you cannot write a MAIN because Access itself is the MAIN code segment. Your code in the form/report class module is based on the kindness of Access to provide you with HOOKS - places where you can declare specialty code to in some way customize the class object appearance and/or behavior.

There is a form event called FORM_OPEN - which is to say, when you launch a form, Access has to open it, and so what it kindly does is that it does its part and then gives you a place where you can identify a subroutine entry point. IF you do identify an entry point, Access calls that subroutine at the time that it finishes with ITS part of opening a form. The most common use of FORM_OPEN is to see whether you wanted to cancel the OPEN action (prevent the form from launching) because at that point, you haven't even loaded the controls of the form. You have just opened that form's form descriptors.

The question was asked, "Is it necessary for a form to have VBA code?" The answer is "No." And in fact, statistically speaking, the odds are quite good that most of the places where a form (OR a report) COULD have code, WON'T have code.

If you look at all of the EVENT properties of forms and reports and controls, you would realize that every visible thing on forms and reports (which includes controls on either) has anywhere from half-a-dozen to over fifty event entry points, most of which you will never use. But if you DID declare code for each one, Access would manage the stated event and then, before going on to the next thing to do, it would call your code. Whether you used the code for security, appearance, data or procedural validation, or general behavior issues, that code changes what users see or experience.

But do you need it? No, because remember that Access "does its thing" first, and THEN calls your event code before going on to the next thing. All events occur whether you have entry points or not. And if you are content that the "vanilla" behavior of Access does what you wanted anyway, you never need to write code behind those events.
 

Users who are viewing this thread

Back
Top Bottom