PDA

View Full Version : [SOLVED] Const for userform name?



gibbo1715
08-28-2005, 03:48 AM
is it possible to set a userform name as a constant so i can make my code as reusable as possible?

I ll give an example in the only way i know how to try and explain what im after, hope this makes sense



Dim formename As String
formname = this userform <----------- need to be able to call this from all other procedures within the userform

for example


Private Sub btnFirst_Click()
DoFill Formname '(i.e.UserForm1) that will then run the code in the standard module
End Sub



'Code in Standard Module


Sub DoFill(UF)
On Error GoTo Err
UF.TextBox1.value = ActiveCell.value
UF.TextBox2.value = ActiveCell.Offset(0, 1).value
UF.TextBox3.value = ActiveCell.Offset(0, 2).value
Exit Sub
Err:
Exit Sub


Any Ideas?

Thanks

Gibbo

TonyJollans
08-28-2005, 04:16 AM
Hi Gibbo,

Individual instances of Userforms do not actually have names.

What you can do is to set a reference to the Userform Object.


Dim myForm As UserForm1
Set myForm = New UserForm1
:
:
DoFill myForm
:
:


Sub DoFill(UF As UserForm1)
UF.Textbox1 etc., etc.
:
:

gibbo1715
08-28-2005, 04:25 AM
thanks for the reply but i dont think that does what im after,

My problem is I have identical code in several userforms, where i just need to change one line per procedure to reference that userform (i.e. DoFill userform1, DoFill userform2, DoFill userform3) and so on to call the procedure in the standard module.

Was just looking for a piece of code to avoid me having to make that minor change each time by using a variable or constant but sounds like thats not possible

mdmackillop
08-28-2005, 05:13 AM
In Form module


Private Sub CommandButton1_Click()
DoFill Me
End Sub


In Standard module


Sub DoFill(UF As UserForm)
On Error GoTo Err
UF.TextBox1.Value = ActiveCell.Value
UF.TextBox2.Value = ActiveCell.Offset(0, 1).Value
UF.TextBox3.Value = ActiveCell.Offset(0, 2).Value
Exit Sub
Err:
Exit Sub
End Sub

gibbo1715
08-28-2005, 05:39 AM
how silly do i feel, didnt think the me command would work

Learn something new every day

cheers

gibbo

mdmackillop
08-28-2005, 05:41 AM
Always learning! :beerchug: