About Sub Main( )

M

March

Hello,


Anyone know about sub main( ) VBA in MS Access. I used to write some code
on VB6. I wrote

Option Explicit

Global db as database

Sub main( )

source = "C:\<filename>.mdb"
share = false
Set db = dbengine.workspaces(0).opendatabase(source, share)
<formname>.show

End Sub


above I could use "db" anywhere in my application.

Any idea to declare the "Global" in VBA.

I try to covert the source code to vba. I know that may have some
differences from VB6.

Also, I try to find reference websites.

If anyone have ideas, please give me suggestion.
 
M

March

I paste it in Module , are there any suggestion. I try to write all the code
in VBA without using Macro.

And could you explain what differences between Class Module and Module in VBA?

Thank you so much

March
 
R

Robert Morley

There isn't really an equivalent to Sub Main() in VBA. The two ways of
achieving the same functionality in Access are:

1) As Alex suggested, create a macro called AutoExec, which then calls your
startup code. Any macro named AutoExec will be run every time an Access
database is opened (at least through normal means).
2) Similar to the above, create a startup form. The name of the form is
irrelevant (though "Startup" and "AutoExec" are common choices), but you can
then execute code in the On Open event. You can either leave the form open
(and often invisible) or just change the Cancel parameter to True to cancel
the form open once your code has executed. Then from the menu bar, select
Tools, Startup, and in the "Display Form/Page" menu, select the name of your
startup form.

Another tactic is to check variables on use. The advantage is that even if
your code crashes at some point, the variables will be automatically
re-initialized. So...

3) You can create a sub and use that to check your variables each time you
use them, instead of always using just db on its own with no check. It'll
minutely slow your code down compared to not checking at all, and it's an
extra line of code every time you want to use the db variable, but is fairly
reliable.
4) The most reliable method, and the slowest one, is to rename the db
variable to something else, let's say dbvar, then create a db function to
replace it. In the function, you check for and initialize the db variable
if necessary. This is generally overkill, but it WILL make sure that db is
always valid unless something is horribly horribly wrong. :)



Rob
 
M

March

Thank you

Robert Morley said:
There isn't really an equivalent to Sub Main() in VBA. The two ways of
achieving the same functionality in Access are:

1) As Alex suggested, create a macro called AutoExec, which then calls your
startup code. Any macro named AutoExec will be run every time an Access
database is opened (at least through normal means).
2) Similar to the above, create a startup form. The name of the form is
irrelevant (though "Startup" and "AutoExec" are common choices), but you can
then execute code in the On Open event. You can either leave the form open
(and often invisible) or just change the Cancel parameter to True to cancel
the form open once your code has executed. Then from the menu bar, select
Tools, Startup, and in the "Display Form/Page" menu, select the name of your
startup form.

Another tactic is to check variables on use. The advantage is that even if
your code crashes at some point, the variables will be automatically
re-initialized. So...

3) You can create a sub and use that to check your variables each time you
use them, instead of always using just db on its own with no check. It'll
minutely slow your code down compared to not checking at all, and it's an
extra line of code every time you want to use the db variable, but is fairly
reliable.
4) The most reliable method, and the slowest one, is to rename the db
variable to something else, let's say dbvar, then create a db function to
replace it. In the function, you check for and initialize the db variable
if necessary. This is generally overkill, but it WILL make sure that db is
always valid unless something is horribly horribly wrong. :)



Rob
 
G

George Nicholson

A Class Module is much the same as it is in VB: it represents an object that
can have Properties, Methods, etc.
Technically, code modules attached to Forms and Reports are Class modules,
however those are generally refered to as Form Modules or Report Modules and
the term Class Module is usually reserved to refer to a Custom Class Module
(i.e., a Class Module of your creation).

A general module contains code that falls outside of any existing Class
(Report, Form or Custom) and is available to the entire application (subject
to Public/Private declarations). It can often be useful to organize module
contents into related procedures/functions (String functions, Math
functions, Form handling, Array handling, Import/Export routines,
ErrorLogging, etc.) but that isn't necessary except for the preservation of
the developer's sanity. Bottom line is that any procedure/function that is
placed into a general modules is placed there to minimize redundant coding
and maximize code re-use, including portability between projects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top