Results 1 to 4 of 4
  1. #1
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273

    Passing a value between modules

    I thought I had a good idea going with my very limited knowledge, but it didn't work! Let me see if I can explain this...
    My database has 2 forms.
    Users complete frmPermit with an employees driving test information, and click the "Send to Control" button to email the results to a controller.
    Users fill out frmTraining with training course information that an employee has completed, and click the "Send to Control" button to email these results to the controller.
    The Send button on frmPermit goes to Public Sub MailPermit() that just says Dim Mail as String & Mail = "Permit", then onto another public sub called "EMail()"
    The Send button on frmTraining goes to Public Sub MailTraining() that just says Dim Mail as String & Mail = "Training", then onto the "EMail()" sub.
    The EMail() Public Sub sets up the email with a few IF statements to change some of the wording, depending on if Mail = Permit or Mail = Training. The problem is, the value of Mail isn't remembered from either first sub into the EMail sub.
    I'm hoping to have the email stuff in only one place so if something like "mail recipients" needs to be changed, it only needs to be done in one place instead of having a piece of code for each form. The email part was set up previously with the Permit form only, and worked great, so I know the text is correct without the IF statements. How can the value of Mail be remembered between subs?



    Also tried declaring Mail as Variant in each module, same results, a blank email.

    Thank you for your help. Everyone here thinks I know what I'm doing, but I owe it all to you!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Options:

    1. textbox on form holds the value, all modules can refer to the form as long as form remains open

    2. declare a global variable in module header

    3. pass the value by argument, declare the Email sub with an argument:
    Sub Email(strMailType As String)
    Use the strMailType variable in your conditional If code.
    Now pass the value to the strMailType variable by argument when calling the Email sub from the other subs:
    Email("Permit")
    Email("Training")

    4. TempVars - I've never used them so don't really know if will work for your situation so I will let you research
    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
    NISMOJim is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    273
    The first answer sounded like what I'm most familiar with, so I started with that.
    I just added textboxes (called "TCMail") to each form with a control source of ="Permit" on one & ="Training" on the other.
    I got rid of both modules (Public Sub MailPermit() & Public Sub MailTraining()) so both command buttons go right to Public Sub EMail().
    In the EMail sub I added Dim Mail as String & Mail = Me.TCMail.
    When I run the code I get a compile error: Invalid use of Me keyword. It seems I would have to use Mail = Forms!frmPermit.TCMail or Mail = Forms!frmTraining.TCMail, but that would probably mess things up too since only one of the forms would be open. Is there a way for the code to establish if one form is open then Mail = one textbox, or if the other form is open then Mail = that textbox?

    or...
    What about declaring a global variable? I don't know how to do that, and would I still need the two modules that I just deleted?

    #3 sounds too confusing for me at this late hour, and I will look into #4.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    For 1 I invisioned a combobox with choices of 'Permit' or 'Training'. User selects one. Code refers to combobox for the selected value. But since you have two forms (just got that) that is not valid.

    Yes, code can check if form is open. Is it possible for both to be open at same time? Or code can just refer to the currently active object, get the object name and act accordingly.

    I think 3 is the easiest choice. It will be a good technique to acquire. Once you understand, you will be amazed what you can accomplish through procedures (subs and functions) using arguments. You can pass the form name by argument. Does the mail procedure need to pull other info from the forms?

    I avoid global variables because they can be a headache when debugging code. Declare a variable in a general module header section and the variable is available to all procedures in all modules.

    Here is demo on TempVars https://www.accessforums.net/code-re...ars-36353.html

    Me is alias for form or report code is behind.
    Last edited by June7; 07-20-2013 at 12:57 PM.
    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.

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

Similar Threads

  1. Where Can I Get Info On Modules
    By aamer in forum Access
    Replies: 1
    Last Post: 08-24-2012, 11:33 PM
  2. Steer me towards Modules
    By libraccess in forum Access
    Replies: 2
    Last Post: 04-10-2012, 09:01 PM
  3. Modules using Crosstabs
    By OTSeraph in forum Access
    Replies: 1
    Last Post: 02-10-2012, 10:50 AM
  4. What is the point of different modules?
    By cowboy in forum Programming
    Replies: 3
    Last Post: 03-29-2010, 10:43 PM
  5. How to use Modules
    By wasim_sono in forum Access
    Replies: 0
    Last Post: 01-16-2007, 06:29 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