Results 1 to 2 of 2
  1. #1
    cherold is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    82

    How do I get my public variables/functions working the way I'd like them to?


    I'm hoping someone can explain, or tell me where to find a good explanation, on getting things working between vba modules. That is, to make public variables that have a single instance across multiple modules and to make public functions that are visible from any module even if that module is connected to a subform. I'll explain what's going on with my code to make clear what my issues are.

    I have a module for a form, [Form_Song List subform]. This is used to enter song information and uses various event calls. The event calls then call other functions I've written, some of which I want to call from modules outside of [Form_Song List subform]. So I thought I'd put non-event-call functions into their own module, which I called [SongAddingRoutines].

    Today I realized there was an issue. in [Form_Song List subform] I declare a variable: "Public action_taken as Integer". This tracks the last action I've taken regarding adding songs. After an issue arose, some debugging indicated that if I put action_taken in a watchlist from [Form_Song List subform] and from [SongAddingRoutines], Access sees these two separate variables which can have different values at the same time.

    So I took all the code from [SongAddingRoutines] and put it back in [Form_Song List subform]. This fixed the issue I'd been having. However, when another module tried to call one of the functions that had been moved, it couldn't see it even though it's declared "Public Sub AttachRecordingtoDisc(Recording_id As Long, Optional LP_id As Long = 0)". So I wound up having to simple copy and paste two functions from [Form_Song List subform] directly into that other module.

    So, is there a way to declare a variable that will be the same over multiple modules? Is there any way to make a function in a subform-specific module visible to all modules?

    The ultimate goal is simply to have better organized, non-repeated code, so any advice on that is appreciated.

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    If you declare your variables in a general module along with your non event functions (User Defined Functions) I think all will be well.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Replies: 1
    Last Post: 10-16-2019, 03:45 PM
  2. Replies: 4
    Last Post: 01-25-2019, 09:03 PM
  3. Public Functions and Private Subs
    By d9pierce1 in forum Programming
    Replies: 15
    Last Post: 11-28-2018, 09:25 AM
  4. VBA Public variables
    By jmitchelldueck in forum Programming
    Replies: 5
    Last Post: 08-20-2015, 12:03 PM
  5. public variables
    By zul in forum Programming
    Replies: 3
    Last Post: 08-23-2011, 11:11 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