Consulting

Results 1 to 5 of 5

Thread: Referencing a Userform in a module

  1. #1
    VBAX Newbie
    Joined
    Jan 2014
    Posts
    2
    Location

    Question Referencing a Userform in a module

    Hi Vbax users

    I'm creating a number of userforms and at the top of these forms I am adding a combo box and a spin button.

    The function of these controls does the same thing on each form so I'm trying to create a Public Sub in a module which will create these controls.
    But I am getting a Type-Mismatch error.

    These test I've done so far is as follow:

    Code in Module1:

    Public Sub SiteSelector(CurrentForm as Userform)
    Dim TestCombo as MSForms.ComboBox
    Set TestCombo = CurrentForm.Controls.Add("Forms.ComboBox.1", "TestCombo", True)
    End Sub
    Code in form(I applied it to a button click event):

    Private Sub CommandButton1_Click
    Module1.SiteSelector(Me)
    End Sub
    Thanks in advance
    Last edited by SamT; 01-07-2014 at 10:53 AM. Reason: Added Code Tags with Post Editor # Icon

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Try this.

    Module1
    Public Sub SiteSelector(CurrentForm As UserForm)
         Dim TestCombo As Object
        Set TestCombo = CurrentForm.Controls.Add("Forms.ComboBox.1", "TestCombo")
    End Sub
    UserForm
    Private Sub UserForm_Initialize()
    Module1.SiteSelector Me
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Newbie
    Joined
    Jan 2014
    Posts
    2
    Location
    No parenthesis around the Me.

    Good Stuff, works now, Cheers

  4. #4
    Perhaps not needed, but how are you going to let the userform respond to events of the programmatically added controls? They wont be available at design time so you cannot add them there. Instead, you would need a class module which contains a withevents declaration for the controls in question.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Slinfoot View Post
    No parenthesis around the Me.
    Nor should there be.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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