Call function from macro using RunCode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kledki
    New Member
    • Jul 2019
    • 20

    Call function from macro using RunCode

    I have a function tha I have written which I am trying to run from a macro using Runcode. The macro that I am trying to call the function from is the OnClick event of a command button. Basically, I want to print a different page of my reort dependent upon the value in the text control "Text28." But when I press the button, the code is function is not running and I can't figure out why. Any suggestions would be very much appreciated! Here is what I have for the function:
    Code:
    Public Function PrintOut()
        If Text28 = "aaa" Then
            DoCmd.PrintOut acPages, 2, 2, , 1
        ElseIf Text28 = "bbb" Then
            DoCmd.PrintOut acPages, 3, 3, , 1
        ElseIf Text28 = "ccc" Then
            DoCmd.PrintOut acPages, 4, 4, , 1
        ElseIf Text28 = "ddd" Then
            DoCmd.PrintOut acPages, 5, 5, , 1
    End If
    End Function
  • Nauticalgent
    New Member
    • Oct 2015
    • 102

    #2
    I noticed that the function you posted is public. Is it in a separate module than the form that has the command button?

    If so, you will need to pass an argument to the function, in this case the value of Text28. Depending on your answer, I have some suggestion to make your code more efficient.

    Comment

    • kledki
      New Member
      • Jul 2019
      • 20

      #3
      Yes it is in a separate module

      Comment

      • Nauticalgent
        New Member
        • Oct 2015
        • 102

        #4
        Try:
        Code:
        Public Function PrintOut(strTest As String)
            Select Case strTest
                Case "aaa"
                    DoCmd.PrintOut acPages, 2, 2, , 1
                Case "bbb"
                    DoCmd.PrintOut acPages, 3, 3, , 1
                Case "ccc"
                    DoCmd.PrintOut acPages, 4, 4, , 1
                Case "ddd"
                    DoCmd.PrintOut acPages, 5, 5, , 1
                Case Else
            End Select
        End Function
        Then on the OnClick() event of your button, use this code:
        Code:
        Call PrintOut(Me.Text28)

        Comment

        • kledki
          New Member
          • Jul 2019
          • 20

          #5
          Thank you so much for your help

          Comment

          • Nauticalgent
            New Member
            • Oct 2015
            • 102

            #6
            You're very welcome, happy to help.

            Comment

            Working...