Solved Function /Sub (1 Viewer)

oldaf294

Registered User.
Local time
Today, 09:29
Joined
Mar 29, 2002
Messages
91
Just for my info. I have two Functions that give a result for the last digit of a UPC-A and result for an alphanumeric for a UPC-A font. When I look in VB there is a Sub for each one also. Why?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:29
Joined
May 21, 2018
Messages
8,463
Code does not write itself. So someone chose to do that. Would have to see the code to guess what their intentions were.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:29
Joined
Oct 29, 2018
Messages
21,357
Sometimes, Subs are written to modularize the code/function. Just a thought...
 

Isaac

Lifelong Learner
Local time
Today, 02:29
Joined
Mar 14, 2017
Messages
8,738
Perhaps the sub calls the function.

But yeah--it doesn't make sense to ask strangers online "why" someone wrote code in your database, that is something only YOU can answer.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:29
Joined
Feb 28, 2001
Messages
26,996
Actually, there could be a gazillion reasons for having both. For instance, if macros are involved, a macro cannot directly call a Sub. It can only call a function. But perhaps the Sub was written first. That would imply that ONE of the arguments to the Sub would have to be "by reference" since otherwise that Sub couldn't return a value.
 

oldaf294

Registered User.
Local time
Today, 09:29
Joined
Mar 29, 2002
Messages
91
I didn't write the Subs. I wrote the Functions and used them in one form. When I noticed the Sub's I deleted them and they reappered.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:29
Joined
May 21, 2018
Messages
8,463
I have never heard of self generating code in Access might be a new feature:). Please post the code or the db.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:29
Joined
Oct 29, 2018
Messages
21,357
I didn't write the Subs. I wrote the Functions and used them in one form. When I noticed the Sub's I deleted them and they reappered.
Sounds like you may be talking about event handlers. Can you post an example of one of the Subs?
 

Isaac

Lifelong Learner
Local time
Today, 02:29
Joined
Mar 14, 2017
Messages
8,738
Sounds like you're confusing which files are being opened.
 

jdraw

Super Moderator
Staff member
Local time
Today, 05:29
Joined
Jan 23, 2006
Messages
15,361
Suggest you show readers some code - the functions and the subs. And tell/show us where these are invoked.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 20:29
Joined
Jan 20, 2009
Messages
12,849
I have never heard of self generating code in Access might be a new feature:). Please post the code or the db.
Nothing new. The Methods to build and edit code have been in the Module Object for at least as long as I have used VBA.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:29
Joined
Feb 28, 2001
Messages
26,996
The biggest trick is to get Access to compile its own code while it is running code. But if you had two databases, where the second one was treated as an application object, you could easily generate code. And the event wizards get away with that all the time (though there, the app has to be in design mode, not actually running.)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:29
Joined
May 21, 2018
Messages
8,463
Nothing new. The Methods to build and edit code have been in the Module Object for at least as long as I have used VBA.
Module object (Access)
Wow thanks! I had no idea what a module was now I know. Yes you can write code to write code such as using VBA extensibility. But again AFAIK there is no code that just sprouts on its own like a mushroom. I have built thousands of Access dbs and not once when I wrote a function a similar procedure magically appeared. So I am pretty sure there is something else going on.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 20:29
Joined
Jan 20, 2009
Messages
12,849
Wow thanks! I had no idea what a module was now I know. Yes you can write code to write code such as using VBA extensibility. But again AFAIK there is no code that just sprouts on its own like a mushroom. I have built thousands of Access dbs and not once when I wrote a function a similar procedure magically appeared. So I am pretty sure there is something else going on.
Perhaps you should be more explicit when you use sarcasm.
 

GinaWhipp

AWF VIP
Local time
Today, 05:29
Joined
Jun 21, 2011
Messages
5,901
Hmm, are you talking about lines that start with *Private Sub...*. If so, then they don't really *write themselves* but they do appear if you select Event Procedure from the Properties Window and will stay there even if you put nothing there. They do not *clean up* by themselves unless you remove them and Save when closing the VBE.
 

isladogs

MVP / VIP
Local time
Today, 09:29
Joined
Jan 14, 2017
Messages
18,186
Actually, in recent versions of Access, an empty event procedure is automatically deleted so it does 'clean up' by itself.
This is probably done to avoid issues whereby empty event procedures prevent projects being compiled as an ACCDE.

To prevent deletion of an empty procedure, add a comment or even just an apostrophe
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:29
Joined
Sep 12, 2006
Messages
15,613
The sub can't be called the same name as the function.

Maybe someone wrote it as a sub first, and then decided they needed a function to do a similar thing, but return a value, so they added a function version. Or vice versa, but probably less likely.

Out of interest, you said the sub returned a value - but how could it?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:29
Joined
Feb 28, 2001
Messages
26,996
Dave is correct and I should have asked about that...

You cannot have two entry points with the same exact name in the same module unless you are doing something like overloading the entry point for different inputs. That is something you CAN do in some languages with very strong type-control. However, in VBA you can't do that. You manage the "overload" case by just declaring the input as a Variant and then test what you got inside the routine.

Now it IS true that a function can be called as though it is a subroutine and in that case, Access WILL discard the return value. But I've not tried to call a sub as though it were a function. I'm not sure what it would return, either, because the return mechanism is rather specific. I would expect a null value returned as a Variant in that case but could be wrong. The thing is, it shouldn't even compile correctly since the data type of the return isn't declared. I.e. the call "template" is incomplete.

In the case currently at hand, if both the sub and function entry points are in the same module, THAT shouldn't compile either. I have accidentally created a duplicate routine name (fat-fingered the name so that it matched another name) and then tried to compile the module. Got a "Duplicate name" error during compilation.

Note that if they are in different modules, that CAN happen. In which case, unless you qualify the call to show which module's entry you wanted, the one that appears first in the module is the one that will be called.
 

GinaWhipp

AWF VIP
Local time
Today, 05:29
Joined
Jun 21, 2011
Messages
5,901
Actually, in recent versions of Access, an empty event procedure is automatically deleted so it does 'clean up' by itself.
This is probably done to avoid issues whereby empty event procedures prevent projects being compiled as an ACCDE.

To prevent deletion of an empty procedure, add a comment or even just an apostrophe
Thanks for that, never bothered to see if that still happens.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:29
Joined
May 21, 2018
Messages
8,463
The sub can't be called the same name as the function.
I think you meant to add "the sub can't be called the same name as the function in the same module". Not a good practice, but based on the scope private, public you could easily have this as long as in different modules. The OP was never clear where these subs and functions are located. So I think the user may have a procedure in a forms class and there is an old function in a standard module with the same name. The class is completely protected in scope so does not matter if the procedure is private or public. No problem with compiling or running. The OP is just likely confused at where they are looking.
 

Users who are viewing this thread

Top Bottom