Setting a variable for the userform itself?

W

Webtechie

Hello,

I am finding that I have the same code for different forms. The way our
shop needs the forms, we have one form that has a some data. Then another
form with the same data and more controls and more data.

I want to be able to write code that will work on either form.

sub ProcessResults(byRef userChoice as integer)

Dim myFrm1 as userform1
Dim myFrm2 as userform2
Dim myFrm

'Set the form variables
'******************
if userChoice = 1 then
set myFrm 1 = NEW userform1
set myFrm = myFrm1
else
set myFrm2 = NEW userform2
set myFrm = myFrm2
end if

'Now reference the form fields
'**************************
myFrm.txtFirstName.text = "David"

.... and so on


I can't seem to be able to set myFrm to whichever form is needed based on
userChoice and the if Statement.

I'm using Excel 2007.

Does anyone know how to set a variable for userform such as I've written
above?

Thanks.

Tony
 
P

Patrick Molloy

Option Explicit

Sub text()
xxx 2
End Sub

Sub xxx(userChoice As Long)
Dim myFrm As Object
'Set the form variables
'******************
If userChoice = 1 Then
Set myFrm = UserForm1
Else
Set myFrm = UserForm2
End If
myFrm.Show
End Sub
 
W

Webtechie

Thanks Patrick. Worked great.

Patrick Molloy said:
Option Explicit

Sub text()
xxx 2
End Sub

Sub xxx(userChoice As Long)
Dim myFrm As Object
'Set the form variables
'******************
If userChoice = 1 Then
Set myFrm = UserForm1
Else
Set myFrm = UserForm2
End If
myFrm.Show
End Sub
 
W

Webtechie

I had it correct, but initially thought it wasn't working because the
intellisense didn't work.

But I just referenced the controls and values were returned anyway.
 

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