PDA

View Full Version : [SOLVED] Referencing a Userform in a module



Slinfoot
01-07-2014, 10:26 AM
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

SamT
01-07-2014, 11:04 AM
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

Slinfoot
01-07-2014, 11:15 AM
No parenthesis around the Me.

Good Stuff, works now, Cheers

Jan Karel Pieterse
01-08-2014, 01:52 AM
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.

Bob Phillips
01-08-2014, 04:31 AM
No parenthesis around the Me.

Nor should there be.