Module Newbie - Pelase help!

branston

Registered User.
Local time
Today, 15:13
Joined
Apr 29, 2009
Messages
372
Hi,

I am trying to get a macro to run a bit of code. I want to put that code in a module, but ive never created one from scratch before.
Ive done quite a lot of code, so i know exactly what i want to put IN the module, but im a buit confused with the top & tail of it all. (eg. Private sub .../ public function...??)
Any help on how to start it all (and anything else i should know before i start) would be greatly appreciated.

Thank you!
 
Branston,

If you put code in a form module, its contents are only available while the
form is open. Most of the Subs and Functions are labeled "Private" as they
pertain only to the form.

If you look at the Modules collection, most of the variables, subs and
functions are labeled Public. They are always available.

If you have functions that you will use in queries or macros, they will
reside in modules.

If you want to establish global variables, they'll most likely reside in a
module, probably initiated in a Startup form or an Autoexec macro.

Hope that gets you started,
Wayne
 
Thanks Wayne,

Ok, I dont want it to be specified to one form, so i guess that means i want a Public one, yes? So the 1st line would be:

Public Sub xxx()

But what would I have to put in the xxx and ()'s?
Im looking at an example i found and for the functions it seems to put the name that will be called where the xxx is, and then the prompts in the bracket.
Can I just call it anything i want?

Also, (something i really should know) what is the difference between Function, sub and variable?

Thanks!
 
Branston,

A function returns a value --> Public Function ABC() As SomeDataType

A sub returns nothing, but can have arguments and is just "reusable" code.

A variable just holds a value, it can be any datatype and it has a scope
defined by when/where it is instantiated. If it's a form variable then it
will only be valid when the form is open. If it is in a Public Module, then
it is available globally provided that you set an initial value.

Code:
Public Function AnyNameAtAll(Argument1 As String, Argument2 As Integer) As Boolean
   '
   ' I'm a Public function called AnyNameAtAll
   '
   ' I need two inputs: Argument1 (as String)
   '                    Argument2 (as Integer)
   '
   ' I will return a Yes/No result (As Boolean)
   '
   ' I can be called from anyplace:
   '
   '    Query --> NewField: AnyNameAtAll([Field1], [Field2])
   '
   '    VBA --> MyStatus = AnyNameAtAll(strTest1, intTest2)
   '
   ' Note that I will ALWAYS have a path that leads to --> AnyNameAtAll = SomeThing
   '
   '
   If Len(Argument1) > 0 And Argument2 > 0 Then
      AnyNameAtAll = True
   Else
      AnyNameAtAll = False
   End If

End Function

hth,
Wayne
 
Wayne, thank you so much! That looks like tis going to be really helpfull.
Thank you!
 

Users who are viewing this thread

Back
Top Bottom