Results 1 to 10 of 10
  1. #1
    mcomp72 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    92

    Can't call a Sub in a Module from inside a Class Object

    I'm pretty new to Access (but been doing VBA in Excel for a little while now), and I have hit a snag. I may be trying to get Access VBA to do something it can't do, but not sure.



    I have a form with a button on it. If the user presses the button, I want it to execute a sub in a module. My sub for when the user clicks on the button (the event) is located in the Class Object for that specific form. Here's the code from that sub inside "Form_MyForm" which is in 'Microsoft Access Class Objects'.

    Private Sub Command20_Click()

    ... (some other code that doesn't refer to the Sub I want to call.)

    Application.Run Module1.CreatePDFFiles

    End Sub


    The CreatePDFFiles sub is inside Module1.

    When I click on the button and execute Sub Command20_Click, I get a compile error: Expected Function or variable. The part of the line below that is in bold is highlighted blue in my code window:
    Application.Run Module1.CreatePDFFiles

    I'm not sure what I'm doing wrong. Any ideas?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The syntax is normally just the function name:

    CreatePDFFiles

    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    mcomp72 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    92
    Quote Originally Posted by pbaldy View Post
    The syntax is normally just the function name:

    CreatePDFFiles

    When I remove the rest of the line, I get Compile Error: Syntax Error. In fact, when I deleted rest of the line, the line turned red before I even ran the sub again.

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    Is the sub set up as Private? Is so, remove the word "Private".

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Sounds like it's in a standard module. Does it take arguments? Can you attach the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    mcomp72 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    92
    davegri: the Sub ‘Command20_Click’ is set to Private; the one that I am trying to run from within ‘Command20_Click’ not. I tried removing the word Private from the one, but the error was still there.

    pbaldy: yes, the command takes arguments, but I tried putting a ‘ before the arguments to disable them for testing purposes, but still got the same error.

    Here is a link to download the db:
    https://www.dropbox.com/s/t5g5oho6l7...ing.accdb?dl=0

    Once you have the db open, open the Form "Employees on jobs". There should be one record there. Click the button in the header "Create PDF Files of Start Paperwork." That should open a new form in a new window. All of the check boxes should be checked, but if not, just check one of them. Then click "Create PDF Files". That should attempt to trigger the sub I want to run. The only thing in that sub currently is a MsgBox command -- this is just to test to make sure it's working. I'll write the actual code later.

    Thanks for taking a look!

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try:

    Code:
    CreatePDFFiles PrintDealMemoTopPage, PrintDealMemoScheduleA, PrintStartForm, PrintBoxRentalForm, PrintI9Front, PrintI9Back, PrintNoticePage1, PrintNoticePage2
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    mcomp72 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    92
    That worked!

    I'm curious, why do we not use the parentheses to pass the arguments to the sub like we normally would? In the past whenever I have used VBA in Excel and needed to pass arguments, I always used parentheses after the sub name.

  9. #9
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    IIRC, the parentheses are only required where a function or sub call uses the Call keyword, or if it is a function and it is returning a value to a variable. The message box function is a good example:
    Result = MsgBox ("message", options) required to pass the value of a choice to Result
    MsgBox "message" - not required and will? generate an error if used
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    This should help:

    https://msdn.microsoft.com/en-us/vba...ion-procedures

    In general, if you use Call or want to use a return value, you have to use parentheses, otherwise not.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Updating function inside a module
    By keiths in forum Modules
    Replies: 3
    Last Post: 04-05-2017, 08:10 AM
  2. call class function from object
    By Ruegen in forum Programming
    Replies: 2
    Last Post: 01-20-2015, 09:51 AM
  3. Initialization Module/Class
    By swalsh84 in forum Modules
    Replies: 1
    Last Post: 12-13-2014, 12:25 PM
  4. Replies: 2
    Last Post: 01-07-2013, 07:24 AM
  5. class module vs regular module
    By Madmax in forum Modules
    Replies: 1
    Last Post: 05-01-2012, 03:44 PM

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