userform parameters

G

Guest

Hello - I have a lot of userforms that use global variables that are created
just for the purpose of populating the userforms. Is there a way to create a
userform that accepts parameters? How? And what would be the syntax to show
the userform ans pass the parameters to it?
 
G

Guest

You can easily accept parameters for the userform - but how do you want them
entered? In worksheet cells, or in a dialog box? This shows examples of
both, prompting the user for one parameter and taking the second from Cell
A1. The userform needs two textboxes, Textbox1 and Textbox2, for this
example:

Sub TestForm()
Dim Param1 As String

Param1 = InputBox("Enter Parameter 1:")
UserForm1.TextBox1 = Param1
UserForm1.TextBox2 = Range("A1").Value
UserForm1.Show

End Sub
 
N

NickHK

Mike,
You can add customer properties to the userform. e.g.
'Assuming you have textbox called txtTester on this user form.
Public Property Let InitText(argText As String)
txtTester.Text = argText
End Property

Or you can create a class that has a reference to a form
'In the class module
Dim MyForm As UserFormdataInput

Public Propert Let InitText(argText As String)
MyForm.txtTester.Text = argText
End Property

Depends how you wish to reuse the forms that you already have.

NickHK
 
B

Bob Phillips

A userform is just a class module, so it can have properties and methods
exposed to other modules, just like other classes. For example

Private mValue As String

Public Property Let myValue(pValue As String)
mValue = pValue
End Property

Private Sub UserForm_Activate()
TextBox1.Text = mValue
End Sub


and in a code module, use

Load UserForm1
UserForm1.myValue = "Hello"
UserForm1.Show

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top