Results 1 to 7 of 7
  1. #1
    PJ Crittenden is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2014
    Posts
    22

    How do I run a module from a button?

    You folks helped me by creating a module to populate something in a table...it works great. Now I've created a button, but I can only get it to open the module, and I need it to run the module. How do I do that?

    Here's the code for the module:


    Option Explicit
    Sub PopulateHeader()
    Dim rs As DAO.Recordset, strHeader As String
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM Raw_Data WHERE Header Is Null ORDER BY ID;")
    While Not rs.EOF
    If rs!Field6 = "T0" And strHeader <> Left(rs!Field1, 19) Then
    strHeader = Left(rs!Field1, 19)
    Else
    rs.Edit
    rs!Header = strHeader
    rs.Update
    End If
    rs.MoveNext
    Wend
    End Sub

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by PJ Crittenden View Post
    ...it works great..
    How do you know it works great? How are you calling the procedure now?

    From within the same module, VBA looks like this
    Call PopulateHeader

  3. #3
    PJ Crittenden is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2014
    Posts
    22
    I open the module and click on the little green Run subform button.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by PJ Crittenden View Post
    I open the module and click on the little green Run subform button.
    You can automate the process by using VBA
    Call PopulateHeader

    However, if PopulateHeader is not within the same module (that your Call PopulateHeader line of code is in) it will not work. In other words, you need PopulateHeader to be a public function. There are other ways, but I am not going to get into that here.

    To create a Public Function, you need your code block to be in a Standard Module and you need the Function Declaration Line to be
    Public Function PopulateHeader()
    instead of
    Sub PopulateHeader()

    And the Signature Line to be
    End Function
    instead of
    End Sub

    Just remember that all of your code that is the Public Function needs to be in a Standard Module in order to be available from any other module.

  5. #5
    PJ Crittenden is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2014
    Posts
    22
    Got it to work. Thank you!

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,620
    If omit the Public keyword, Access will default to public.

    If you have a button on a form to run this code, instead of a call to code in standard (general) module, the code can be in the button Click event procedure in the module behind the form, as explained in your other thread.

    Unless you want to call the procedure from multiple locations, suggest you put it in the button Click event - just less complication.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by June7 View Post
    ...If omit the Public keyword, Access will default to public...
    I was not sure why the OP had a Sub Procedure and did not want to get too deep into Public Interface vs. Private interface.

    Everything can go into a click event, for sure... Just as easy to call the sub from a click event within the same Module.

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

Similar Threads

  1. Running a module from a button click
    By adacpt in forum Modules
    Replies: 4
    Last Post: 12-03-2012, 01:09 PM
  2. Running a module from a button in a form
    By sardamil in forum Modules
    Replies: 3
    Last Post: 05-01-2012, 10:59 PM
  3. class module vs regular module
    By Madmax in forum Modules
    Replies: 1
    Last Post: 05-01-2012, 03:44 PM
  4. Run a Module
    By mchadwick in forum Modules
    Replies: 7
    Last Post: 09-02-2011, 09:24 AM
  5. Replies: 4
    Last Post: 05-16-2011, 04:58 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