Functions in new module (1 Viewer)

MrHans

Registered User
Local time
Today, 07:59
Joined
Jul 27, 2015
Messages
147
Hi all,

Today my precious database got corrupted and I think the reason was that I put several functions in the same module.
I often read with functions that people post, paste the following code in a new module...

Are there any known issues putting several functions in the same module?
Is the only reason to use multiple modules to keep things organized, or is there a greater cause?
I noticed sometimes I had just Function etc and sometimes Public Function, does this make any difference?

I managed to fix my corruption by carefully importing all objects to a new database and split the modules and functions in several other modules.

Thx for the advise.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 00:59
Joined
Feb 28, 2001
Messages
27,245
You can put multiple functions in a single general purpose module. There are some limits to the size of a module (I think maybe 65k bytes of source code) but that is a compiler limit. The compiled code will be smaller than that and won't be an issue.

My personal preference is to group my functions into multiple general modules based on topic, so if I am working with multiple Office documents, all my support functions and subroutines for WORD are in one module, my EXCEL support code is in a different module, my Access functions in a third module, my file system support functions are in a fourth module, and so on. It is rare to have so many functions on a single topic that they exceed the 65k character limit for the module. Nor am I absolutely sure that 65k is the actual limit, but I've run across that limit in other posts in this form.

As to "PUBLIC" vs. "PRIVATE" vs. no qualification at all... If you put a function in a general module and mark it PUBLIC then any code in any part of your app can see/use it. If you mark it PRIVATE then only the code within that module can see/use it directly (though if you call a PRIVATE function from a PUBLIC function, callers of the PUBLIC function implicitly use the PRIVATE one, too). I don't recall the default for public/private, but I hardly EVER allow the public/private quality of some variable to be left to chance.

Hope that helps.
 

MrHans

Registered User
Local time
Today, 07:59
Joined
Jul 27, 2015
Messages
147
Thank you, yes that helps although I still don't understand why it corrupted my DB.
Does the combination of Subs and Functions mixed through each other make any difference?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 06:59
Joined
Feb 19, 2013
Messages
16,637
To add to Doc's suggestions

I don't recall the default for public/private
default is public

combination of subs and functions makes no difference.

Naming a function with a reserved word or the name of an existing public function in a different module (which could be a module associated with an open form) will cause conflicts which will seem like corruption.

not declaring variables or not typing them can cause issues - also ensure all modules have option explicit at the top.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 00:59
Joined
Feb 28, 2001
Messages
27,245
Subs and Functions can coexist quite peacefully.

As to corruption, it probably has NOTHING to do with the content of a module and EVERYTHING to do with having a problem when you were closing your application (MDB or ACCDB) file. I.e. corruption is unlikely to be caused by static content. It occurs as the result of an action, or of an unusual condition during an otherwise normal action.

Simplest likely culprit: Closing Access and then, because Windows was taking longer than normal to do something, turning off the machine with unflushed buffer contents pending writeback. See, Windows is designed to do deferred writebacks. Normally, if you write a record, the buffer will be written eventually and in the meantime, Access knows where the "live" copy resides. But when you close the application, all of the pending writebacks have to get written. If you hold down the power button for about 5 to 10 seconds and you had 15 seconds worth of data to write back, WHOOPSIE!

Before you ask how it could take 15 seconds to do a bunch of disk writes, you have to remember that you may have been using virtual memory, in which the contents of a block of virtual addresses were written to your swap/page file (the virtual memory file) and THEN you did your exit. Well, to put those virtual blocks in the right place, they have to be read from the page/swap file first, THEN written back to their permanent "backing store" area in the MDB or ACCDB file. AND to make matters worse, the priority of the tasks in question DO make a difference in writeback order. Higher priority tasks get written back first. Do you have an AntiVirus program with high priority in the mix? It has the same problem as anything else - if there is a need for writing back VM then there is a prioritized readback from disk-resident VM followed by a prioritized writeback to the appropriate place on disk, perhaps in a writeable program segment or perhaps in a disk buffer that had been used for logging.

Windows, to be blunt about it, has a LOT of crap going on under the covers - particularly at shutdown.
 

MrHans

Registered User
Local time
Today, 07:59
Joined
Jul 27, 2015
Messages
147
Ok, thank you both very for your detailed response.

It can be that one of the operators was in a hurry to go home, and as the last one shuts down, the DB will perform a compact on close.
Maybe this took too long for him/her and force closed it.

I checked the functions and I can't find anything wrong with them... They're also pretty basic and most were made MVP's. Also the explicit declaration is obviously there.

Thanks again for your detailed explanations.
 

missinglinq

AWF VIP
Local time
Today, 01:59
Joined
Jun 20, 2003
Messages
6,423
Many experienced developers are of the opinion that doing a C & R can, itself, cause corruption, and should only be done after a backup copy of the database has been made. Because of this, using the Compact on Close option is frowned upon.

This is obviously being used in a multi-user environment, so the question has to be asked...is this split into a front end/back end configuration, with each user having a copy of the front end on their hard drive?

BTW, the only caveat in placing Functions in Standard Modules is that you cannot name the Module with the same name as that of one of its Functions...this confuses the devil out of the Access Gnomes.

Linq ;0)>
 

Users who are viewing this thread

Top Bottom