creating a function in a module then call from a form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Steve B
    New Member
    • Mar 2008
    • 4

    creating a function in a module then call from a form

    Hello,

    I am trying to create a math function in a module and then call the function from a VB form. For some reason I get errors each time I try.

    on the VB Form

    A = 1
    B = 2

    how do I call the functin below?

    Module
    Function Add (A,B)
    Add = A + B
    End Function
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by Steve B
    Hello,

    I am trying to create a math function in a module and then call the function from a VB form. For some reason I get errors each time I try.

    on the VB Form

    A = 1
    B = 2

    how do I call the functin below?

    Module
    Function Add (A,B)
    Add = A + B
    End Function
    the functions are public by default, any way, specify it, just to make sure, you should also change the functions name, since Add is a method.
    may be this will do:

    [CODE=vb]Public Function MyAdd(A, B)
    MyAdd = A + B
    End Function[/CODE]

    Also check the event's sub where you're calling it from.
    HTH

    Comment

    • Steve B
      New Member
      • Mar 2008
      • 4

      #3
      Originally posted by kadghar
      the functions are public by default, any way, specify it, just to make sure, you should also change the functions name, since Add is a method.
      may be this will do:

      [CODE=vb]Public Function MyAdd(A, B)
      MyAdd = A + B
      End Function[/CODE]

      Also check the event's sub where you're calling it from.
      HTH
      Thank you for the help. How do I call MyAdd in the VB5 form?

      Comment

      • Steve B
        New Member
        • Mar 2008
        • 4

        #4
        Originally posted by Steve B
        Thank you for the help. How do I call MyAdd in the VB5 form?
        Here is the code from the Sub on the main form,

        Private Sub Command2_Click( )

        Dim A, B, x
        Dim MyAdd
        A = 2
        B = 2

        Label11.Caption = Format(MyAdd, "##0.0")

        It will not return a value to label 11.

        Comment

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          Originally posted by Steve B
          Here is the code from the Sub on the main form,
          [CODE=vb]Private Sub Command2_Click( )

          Dim A, B, x
          'Dim MyAdd <-- no need to do this
          A = 2
          B = 2

          Label11.Caption = Format(MyAdd(A, B), "##0.0") 'Call the function properly
          end sub[/CODE]

          HTH

          Comment

          • Steve B
            New Member
            • Mar 2008
            • 4

            #6
            Originally posted by kadghar
            [CODE=vb]Private Sub Command2_Click( )

            Dim A, B, x
            'Dim MyAdd <-- no need to do this
            A = 2
            B = 2

            Label11.Caption = Format(MyAdd(A, B), "##0.0") 'Call the function properly
            end sub[/CODE]

            HTH
            To: HTH, thank you provided the information that I needed.

            Comment

            • kadghar
              Recognized Expert Top Contributor
              • Apr 2007
              • 1302

              #7
              Originally posted by Steve B
              To: HTH, thank you provided the information that I needed.
              HTH = hope that helps.

              call me Kad,

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by kadghar
                HTH = hope that helps.
                Hahaha... last year I was calling someone "Tia" for a while, not realising it was short for thanks in advance.

                Comment

                • kadghar
                  Recognized Expert Top Contributor
                  • Apr 2007
                  • 1302

                  #9
                  Originally posted by Killer42
                  Hahaha... last year I was calling someone "Tia" for a while, not realising it was short for thanks in advance.
                  Just for you not to feel bad, 'Tía' means 'aunt' in Spanish. And in Spain (not in Mexico), they call friends Tío (he) or Tía (she), it's like saying buddy.

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    In this case, the poster had simply put TIA at the end meaning Thanks In Advance, and I misinterpreted it. But thanks for trying... :)

                    Comment

                    Working...