Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Best Practice - Forms, Calling external procedure (VBA/Word 2003)

    Dear Loungers,

    I have several very similar forms in one global template. Are there any guidelines or known problems with referring to code in another procedure from the form code? for example I have a validation routine and it does the same thing for each form so it seems sensible to only have the code once.

    Any advice, thanks................. liz

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

    Re: Best Practice - Forms, Calling external procedure (VBA/Word 2003)

    1) Place the common code in a standard module (created by Insert | Module), in the form of a public procedure or function.
    2) If you need to refer to controls on the userform, pass the controls as arguments to the procedure or function.
    3) If the calling code on the userform needs to act on the result of the common code, make the common code a function that returns a value.

    A very simple example: let's say that you want to ensure that the user enters something in a text box. The following function is placed in a standard module:

    Public Function Test(tb As MSForms.TextBox) As Boolean
    If tb.Text = "" Then
    MsgBox "Please enter some text.", vbExclamation
    tb.SetFocus
    Test = False
    Else
    Test = True
    End If
    End Function

    The text box control is passed as an argument to the function, its Text property is checked, and if necessary focus is set to the text box and a message is displayed. The function returns True if the text box passed the test, False if it failed.
    Note that the function doesn't have to know what the userform is called, nor what the text box is called.

    Now let's say you have a userform with a text box TextBox1 and a command button CommandButton1. When the user clicks the command button, the form is to be closed if the user entered something (this is just a useless example!). Here is the On Click code for the command button:

    Private Sub CommandButton1_Click()
    If Test(Me.TextBox1) = True Then
    Unload Me
    End If
    End Sub

    You see that the text box is passed to the Test function, and action is taken depending on the return value of Test.

  3. #3
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Best Practice - Forms, Calling external proced

    Hnas,

    Thank you. I have used this technique and also your aldvice previously about referring to external procedures and it works well. My concern was is there anything specila about Forms, I know there shouldn't be but I wanted to check. So it seems that because you didn't mention anything specifically problematic with forms and form code everything should work well.

    Thank you............. liz

Posting Permissions

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