get UserForm1 value

M

Michael W

Hi,

I have a module and userform1 in the doc. My module has six main functions
that will be run once user click a custom toolbar that's connected for each
function. The userform purpose is simply to let user input the value and
then call a function that will process the value further.

My questions is simple, how would I get to run the form and get the value
for my function(Sub ConvertMTP)module?

Thank you

My userform code:
Sub CommandButton1_Click()
Unload Me
End
End Sub

Sub CommandButton2_Click()
If TextBox1.Value = "" Or TextBox2.Value = "" Then
MsgBox ("You must input the Application Name and its Functional Area")
TextBox1.SetFocus
Else
Application = TextBox1.Value
Functional = TextBox2.Value
ChildFA = TextBox3.Value
Unload Me
NewMacros.ConvertMTP
End If

End Sub

Sub CommandButton3_Click()
TextBox1.Value = Null
TextBox2.Value = Null
TextBox3.Value = Null
End Sub


Sub Form_load()
TextBox1.SetFocus
userform1.Show
End Sub

My module function:
Sub ConvertMTP()
'
' ConvertMTP Macro
' Macro created 8/21/2006 by Business Objects
'

Dim Test As FormField

Load userform1
userform1.Show

' I try to get userform1 textbox1 value, but fail
'MsgBox (Test.TextBox1.Value)

'Create text file
Const ForWriting = 2
Dim objDialog, intReturn, objFSO, objFile, Wscript
Set objDialog = CreateObject("SAFRCFileDlg.FileSave")
objDialog.FileName = "TCS.MTP"
objDialog.FileType = "QACenter MTP File Type (*.MTP)"
intReturn = objDialog.OpenFileSaveDlg

If intReturn Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(objDialog.FileName, ForWriting,
True)
Else
Wscript.Quit
End If
'
objFile.WriteLine ("[MTP]")
objFile.Write ("Name=")
'
objFile.Close
 
J

Jean-Guy Marcil

Michael W was telling us:
Michael W nous racontait que :
Hi,

I have a module and userform1 in the doc. My module has six main
functions that will be run once user click a custom toolbar that's
connected for each function. The userform purpose is simply to let
user input the value and then call a function that will process the
value further.
My questions is simple, how would I get to run the form and get the
value for my function(Sub ConvertMTP)module?

If I understand correctly, you have six procedures (that you call functions)
that all call the same userform in order to get some values from the user.
Each procedure then processes this information in a different way.

First, try to have all the code dealing with the data manipulation outside
the userform, i.e, in the calling Sub or Function.
Also, try to use explicit name for the userform and its controls (After a
while, you may forget what CommandButton1 does or what value TextBox1 is
supposed to hold.
Also, the use of "End" is not necessary
Finally, it is bad practice to use reserved words or object/method/property
names as names for you own object/variables. In your code, you use
"Application" which is part of all the Office Object Libraries.

Here is a first example of what you cold do. It uses arrays to get the info
from the userform to the calling Sub.

Code in the main module:
'_______________________________________
Sub FirstFunction()

Dim strValues() As String

strValues = GetValues

If strValues(0) = "Cancelled" Then
MsgBox "Function 1 cancelled by user."
Else
MsgBox strValues(0) & " - Function 1"
MsgBox strValues(1) & " - Function 1"
MsgBox strValues(2) & " - Function 1"
End If

End Sub
'_______________________________________
'_______________________________________
Sub SecondFunction()

Dim strValues() As String

strValues = GetValues

If strValues(0) = "Cancelled" Then
MsgBox "Function 2 cancelled by user."
Else
MsgBox strValues(0) & " - Function 2"
MsgBox strValues(1) & " - Function 2"
MsgBox strValues(2) & " - Function 2"
End If

End Sub
'_______________________________________
'_______________________________________
Function GetValues() As String()

Dim myForm As UserForm1
Dim strLocalArray(2) As String

Set myForm = New UserForm1

myForm.Show

With myForm
If .boolCancel = True Then
MsgBox "Cancelled by user."
strLocalArray(0) = "Cancelled"
Else
strLocalArray(0) = .TextBox1.Value
strLocalArray(1) = .TextBox2.Value
strLocalArray(2) = .TextBox3.Value
End If
End With

Unload myForm

GetValues = strLocalArray

End Function
'_______________________________________

And the code behind the userform (which, for this example, only has three
textboxes and two buttons):
'_______________________________________
Public boolCancel As Boolean
'_______________________________________
Private Sub CommandButton1_Click()

Me.hide
boolCancel = True

End Sub
'_______________________________________
'_______________________________________
Private Sub CommandButton2_Click()

If TextBox1.Value = "" Or TextBox2.Value = "" Then
MsgBox "You must input the Application Name and its Functional Area"
TextBox1.SetFocus
Else
Me.hide
End If

End Sub
'_______________________________________
'_______________________________________
Private Sub UserForm_Initialize()

boolCancel = False

End Sub
'_______________________________________

If you do not like working with arrays (and since you only have three values
to handle), you could work with data type.
This variation of the code works as well. The Userform code is the same.

'_______________________________________
Type UserInfoType
App As String
Functional As String
ChildFa As String
End Type
'_______________________________________
'_______________________________________
Sub FirstFunction()

Dim strValues As UserInfoType

strValues = GetValues

If strValues.App = "Cancelled" Then
MsgBox "Function 1 cancelled by user."
Else
MsgBox strValues.App & " - Function 1"
MsgBox strValues.Functional & " - Function 1"
MsgBox strValues.ChildFa & " - Function 1"
End If

End Sub
'_______________________________________
'_______________________________________
Sub SecondFunction()

Dim strValues As UserInfoType

strValues = GetValues

If strValues.App = "Cancelled" Then
MsgBox "Function 2 cancelled by user."
Else
MsgBox strValues.App & " - Function 2"
MsgBox strValues.Functional & " - Function 2"
MsgBox strValues.ChildFa & " - Function 2"
End If

End Sub
'_______________________________________
'_______________________________________
Function GetValues() As UserInfoType

Dim myForm As UserForm1
Dim localUIT As UserInfoType

Set myForm = New UserForm1

myForm.Show

With myForm
If .boolCancel = True Then
localUIT.App = "Cancelled"
Else
localUIT.App = .TextBox1.Value
localUIT.Functional = .TextBox2.Value
localUIT.ChildFa = .TextBox3.Value
End If
End With

Unload myForm

GetValues = localUIT

End Function
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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