Results 1 to 4 of 4
  1. #1
    Whale is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    4

    Forgotten How To Add To All Modules

    I haven't used Access for a few years now and have just finished a small project for work.



    At the end (of course) I thought gee it would be a good idea to globally declare a string, assign the module name to the global string and then in error handling add this global string so I can go straight to the Module if there is an error.

    I recall many years ago I was able to do something similar using a code module that walked the VBE and added the global string to every procedure however I cant recall how or where I may have got this from. Anyone got a bit of code or a direction to point me in?

    Thanks

  2. #2
    Micron is online now Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    If I understand the need, it is easier than you make it out to be. There are a few levels of 'scope' for a variable within the code for a project. For your case, it is enough to Dim the variable a the top of one standard module in order to make it visible to the whole project so that you can use it from anywhere. However, this is seldom a great idea. Rather than repeat the reasons why, look at http://wiki.c2.com/?GlobalVariablesAreBad IMO, one of the most potentially dangerous reasons seems to be missing there: if the app errors, global variables can revert back to default values or data types. Depending on the need, a custom class object or function might be more advisable. You can expose the function globally but use the value it returns locally.

    If you want me to expound a bit on variable scope, let me know.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Whale is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    4
    Thanks...perhaps I haven't explained what I am trying to do well.

    What I would like to do is declare a variable such as

    g_strMN as String

    This is a global variable.

    Then in each module insert

    Private Sub cboFilterName_Change()
    g_strMN = 'cboFilterName_Change()'

    Then in error code I can add:

    Msgbox g_strMn & " " & Err.Number etc

    Thanks

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    You might want to investigate MZTools for VBA. It is a utility that can add an Error Handler to your procedure with 1 click.
    Always advisable to have an error handling routine.
    The MZTools for VBA utility has been updated and is no longer free. It has many useful features. I think the cost for 1 developer is about $75 US.

    There is also a product called vbWatchDog from EverythingAccess (Wayne Phillips) that may be of interest.

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

Similar Threads

  1. When to use standard modules vs. class modules
    By Access_Novice in forum Programming
    Replies: 7
    Last Post: 01-03-2016, 10:47 PM
  2. Passing a value between modules
    By NISMOJim in forum Programming
    Replies: 3
    Last Post: 07-19-2013, 12:34 PM
  3. Forgotten DB PW
    By Vellmen in forum Access
    Replies: 5
    Last Post: 04-01-2013, 10:06 AM
  4. Where Can I Get Info On Modules
    By aamer in forum Access
    Replies: 1
    Last Post: 08-24-2012, 11:33 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