How to execute a VBA access module?

Ok, I'm using Access 2003, and I just started learning Access and VBA a few days ago.
I wrote the code in the module and there are no errors when I click the play button on the debugger toolbar up.
How I really execute this code in my database so that it does something.
In other words, how can I use a module?

Please help me, I try my best, and the deadline for this is Thursday!

Thanks!

+4
source share
3 answers

Well, it depends on how you want to call this code.

You call this by clicking the button on the form, if then in the properties for the button on the form go to the "Event" tab, then click "On", select [Event Procedure]. This will open the VBA code window for this button. Then you call your Module.Routine, and then it will fire when the button is clicked.

Similar to this:

Private Sub Command1426_Click() mdl_ExportMorning.ExportMorning End Sub 

This button click event Module mdl_ExportMorning and Public Sub ExportMorning .

+7
source

If you just want to run the function for testing purposes, you can use the Immediate Window in Access.

Press Ctrl + G in the VBA editor to open it.

Then you can run your functions as follows:

  • ?YourFunction("parameter")
    (for functions with a return value - the return value is displayed in the Immediate window)
  • YourSub "parameter"
    (for subs without return value or for functions when you don't care about return value)
  • ?variable
    (to display the value of a variable)
+9
source

You do not use the module - you execute the routines / functions that are stored in the modules.

If you put the code in a stand-alone module and do not specify a scope in the definitions of your routines / functions, they will be publicly accessible by default and can be called from anywhere in your application. This means that you can call them using RunCode in a macro, from form / report class modules, from stand-alone class modules, or for functions from SQL (with some caveats).

Given that you were trying to inject into VBA what you thought was too complex for SQL, SQL is the likely context in which you want to execute the code. Thus, you should simply be able to call your function in an SQL statement:

  SELECT MyTable.PersonID, MyTable.FirstName, MyTable.LastName, FormatAddress([Address], [City], [State], [Zip], [Country]) As Address FROM MyTable; 

This SQL calls a public function called FormatAddress (), which takes address components as arguments and formats them accordingly. This is a trivial example, because for this purpose you most likely will not need a VBA function, but the fact is that this is how you call the functions from the SQL statement.

Subprograms (i.e. code that does not return a value) cannot be called from SQL statements.

+1
source

All Articles