Module Compiling Issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KatcDolly
    New Member
    • Sep 2016
    • 15

    Module Compiling Issues

    I have code that needs to be used in multiple events. Please see link:



    I am having trouble with a Compile Error when I try to run it. Here is what I did, I placed it in a Module saved the Module as My_enableDisabl e. Then created the Private sub using the code. I called it in each event AfterUpdate and OnCurrent.
    When I do this an error comes up.

    what the Error says:

    Compile Error!
    Expected variable or procedure not module.

    I am not certain how to do this. Can you give me some advice and share some good links to go learn more on How to better understand using Modules?

    Thank you!
  • PhilOfWalton
    Recognized Expert Top Contributor
    • Mar 2016
    • 1430

    #2
    Usually caused by the name of a function or sub in ANY module having the same name as a module.

    I believe you have a Sub called EnableDisable. Might be worth changing the module name.

    Phil

    Comment

    • KatcDolly
      New Member
      • Sep 2016
      • 15

      #3
      I recognized I inadvertently named them the same and changed the Module name to My_enableDisabl e. When I tried it again, I still got an error. It said,

      Compile Error
      Sub or Function not defined

      What am I missing?

      Thanks

      Comment

      • PhilOfWalton
        Recognized Expert Top Contributor
        • Mar 2016
        • 1430

        #4
        Have you got
        Code:
        Option Compare Database
        Option Explicit
        at the head of EVERY Module in your database?

        Phil

        Comment

        • KatcDolly
          New Member
          • Sep 2016
          • 15

          #5
          Phil

          I do.

          I have the Module set as a private sub thinking it only needs to work in this database and it is a sub not a function.

          I tried calling it 2 different ways in the form and got the same error:
          The first way:

          call enableDisable

          The second way:

          enableDisable


          Katc Dolly

          Comment

          • PhilOfWalton
            Recognized Expert Top Contributor
            • Mar 2016
            • 1430

            #6
            Is the Sub EnableDisable in the same module that you are calling it from (Sorry about the preposition at the end of the sentence).

            If not, try removing the word Private

            Phil

            Comment

            • KatcDolly
              New Member
              • Sep 2016
              • 15

              #7
              Phil,
              I removed the Private. Then a new compile error appeared highlighting the Me. I replaced the Me with Forms!FormName! in each Me. After that, It worked!! :)

              I understand why Me needed to be replaced, but what did the Private do?

              Do you have a recommendation to read and learn more about Modules?

              Thank you for your help

              Comment

              • PhilOfWalton
                Recognized Expert Top Contributor
                • Mar 2016
                • 1430

                #8
                Not easy to explain, bu I'll have a go. I hope some of the other contributers will add to the explanation.

                All functions in Form & Report modules are by definition Private, because they are only accessible from that form or report.

                All Functions in normal Modules are Public unless preceded by the word "Private".

                So a Function or Sub in a Form or Report can call a (Public) Function or Sub in a normal module. That (Public) function can call a Private Function or Sub, but only if it is in the same module.

                So if you have a number of normal modules, they can have Private Functions and Subs with the same names that do different things, but if they are Public Functions or Subs, they need different names, otherwise Access wouldn't know which one to use.

                Hope that helps a bit.

                As for reading matter, I have no idea. If I get stuck I usually find what I want using Google or "Bytes"

                Phil

                Comment

                • jforbes
                  Recognized Expert Top Contributor
                  • Aug 2014
                  • 1107

                  #9
                  Phil nailed it, but this is here and might be helpful: Variable Scope in VBA for MS Access

                  Comment

                  • KatcDolly
                    New Member
                    • Sep 2016
                    • 15

                    #10
                    Phil and jforbes,

                    Thank you! T

                    The explanation and added reading are very educational and helpful. I will be back with more questions as I process this information. Thanks for your wisdom and support.

                    Comment

                    Working...