Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling Modules (2003)

    Hi folks,

    So far my VBA coding is restricted behind forms using event procedures, plus the manual running of one or two modules Hans has helped me to cook! Can anyone tell me how to run a module automaticall as part of an event procedure going behind a click button on a form? I have tried the DoCmd Open Module, but doesn't seem to do what I want, which is to invisibly run the module at a certain point in the code behind the click button the form.

    Hope this makes sense - if not let me know what else you need to know! Grateful for your help.

    Inaflux

  2. #2
    Silver Lounger GARYPSWANSON's Avatar
    Join Date
    Aug 2001
    Location
    Frederick, Maryland, USA
    Posts
    1,788
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Calling Modules (2003)

    You would just call the module as you would a function. For example, you create a module that creates a message box. You save the module with the name GenerateMessageBox. In design mode you would have Sub CreateANewMessageBox () with the code to create the message box and then End Sub.

    Now, to launch the code from say a command button, on the On Click event just put in CreateANewMessageBox to execute the module.

    HTH
    Regards,

    Gary
    (It's been a while!)

  3. #3
    Star Lounger
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Modules (2003)

    Sounds easy enough! Will give it a go. thanks. Ina

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Calling Modules (2003)

    To expand a bit on Gary's reply:

    A module is a container for VBA code. It can contain declarations of variables, and the definition of functions and procedures (subs). One module can contain several procedures etc. So generally speaking, you don't "run" a module - you run a procedure or function.
    You can run a procedure simply by using its name, as Gary indicates, or by using the keyword Call followed by the name of the procedure. So say you want to run the procedure MyProc (which is defined in a module) from the On Click event procedure of CommandButton1, you can use

    Private Sub CommandButton1_Click()
    MyProc
    End Sub

    or

    Private Sub CommandButton1_Click()
    Call MyProc
    End Sub

    Both have the same effect.

  5. #5
    Silver Lounger GARYPSWANSON's Avatar
    Join Date
    Aug 2001
    Location
    Frederick, Maryland, USA
    Posts
    1,788
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Calling Modules (2003)

    ... your reply is much more elegant... <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>
    Regards,

    Gary
    (It's been a while!)

  6. #6
    Star Lounger
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Modules (2003)

    Hi Folks, am obviously doing something wrong - am trying Call MyModule within my code as described, and de###### returns "Expected variable or procedure not Module" - what am I doing wrong??

    Thanks foryour help.

    Ina

  7. #7
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Calling Modules (2003)

    You don't call the Module.
    Instead you call a procedure (ie. sub or function) that is within the Module.
    Regards
    John



  8. #8
    Silver Lounger GARYPSWANSON's Avatar
    Join Date
    Aug 2001
    Location
    Frederick, Maryland, USA
    Posts
    1,788
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Calling Modules (2003)

    When you save the module, you save it with a name. i.e., you saved a Module and Named it NewModule. Within the module you create a procedure. Lets say you name the procedure NewProcedure. You would call the procedure that is located in the module by referencing the procedure name in code. So, if you called the procedure from an on click event of a button, you would just type NewProcedure. (The name of the procedure located within the module - not the name of the module)
    Regards,

    Gary
    (It's been a while!)

  9. #9
    Star Lounger
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Modules (2003)

    Thanks both - amazingly easy, don't know why I managed to make such a meal of it.

    Cheers.

    Ina

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •