Results 1 to 6 of 6
  1. #1
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30

    Create Function that can be called from VBA on any form.


    Correct me if I am wrong, but each form has its own block of VBA code within an Access application.

    What I want to do is create an e-mail function in my Access Application that I can pass address, subject line, and message to and have it use CDO to send the SMTP message to my mail server. I have found the correct CDO e-mail code, but I am wondering how (if possible) to create on callable function in my Access project that can be called from any form, so I don't have to put the same e-mail code in each form {like to keep things clean if I can}.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Yes, forms and reports can have associated code modules.

    The application also has general modules. This is where you want the generic procedure.

    Build the function or sub with arguments. What those arguments should be depends on your requirements. Wherever the function is called from, pass the appropriate values with the arguments so the function has required information to execute properly. You mention 3 arguments, so something like:

    Sub SendEmail(strAdd As String, strSub As String, strMsg As String)
    'code to send email
    End Sub

    Call the procedure from form module:
    Sub btnEmail_Click()
    SendEmail("toAddress", "subject text", "message text")
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ts_hunter is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    30
    In my project when I am in the VBA programming area, I right click and select Insert... I have the option of Class Module and Module. Which of these is the global one that can be called from anywhere in my project?

    Click image for larger version. 

Name:	snip3.JPG 
Views:	15 
Size:	42.0 KB 
ID:	15175

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I think either can but I have never used Class Module.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    If you just want to create a public function use a general module. That would be, "Module". A class module is associated with a class. For instance, forms are a class and they have class modules. You do not see their class modules in the area you are looking. They are listed with the forms. Class modules in the section you are for objects that you create and define or instantiate. An object that is created via VBA will have a name and you may want some code to fire when that object is used/created or even when your code is finished with the object. You could have a class module associated to this object by name.

    PS I have used class modules but I have never bothered to create them myself.

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Note that when prompted to name the Module that your Function resides in, name it anything except the same name as the Function; doing that irritates the Access Gnomes no end, and they'll throw error messages at you, to show their displeasure!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Unload called when opening a form?
    By Kirtap in forum Programming
    Replies: 6
    Last Post: 12-11-2013, 06:23 AM
  2. Replies: 6
    Last Post: 11-19-2013, 01:38 AM
  3. Replies: 3
    Last Post: 01-08-2013, 11:51 AM
  4. Replies: 4
    Last Post: 08-16-2011, 10:04 AM
  5. Replies: 4
    Last Post: 08-05-2011, 07:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums