G
Greg Maxey
Steve Hudson was schooling me in UserForms earlier today.
He advised user form code should pretty well only exist in order to provide
data validation trough calling public procedures. I tried employing one of
his "modified" suggestions as follows.
I have a simple userform oFrm1 with a 1) command button and 2) a frame with
one textbox inside.
The purpose of this exercise is to learn to pass information from a calling
macro to a UserForm and report validated information back to the calling
macro. I am using an input box with a default value to set the
Frame.Caption in the UserForm. This part works. I am using the textbox in
the Userform to provide raw data to a Function. The Function is supposed to
validate the data. Then the validated textbox data is used to set the value
of a calling macro msgbox. Everything works if the data is valid. The
problem I can't figure out is correcting invalid data. On the first pass,
invalid data is detected and I am directed back to the Userform. However,
when I then enter valid data the routing complete but the data is lost. I
think my problem is that after I enter valid data, the code returns to the
function at the End With statement following .Show. For some reason this
scrambles the data and I just can't figure out a way around it.
Any help to get me back on track is appreciated.
Here is my calling macro:
Option Explicit
Public oPassedData As String
Public frmObject As myObject
Public myFrm As oFrm1
Sub PassData1()
Set myFrm = New oFrm1
'Pass Caption to UserForm.Frame1
myFrm.Frame1.Caption = InputBox("Enter Label1 Caption: ", _
"Pass Caption", "Enter a shoe size e.g., 10E")
myFrm.Show
MsgBox "Formatted date returned from the UserForm: " _
& myFrm.TextBox1.Text, , "Data Returned"
Set myFrm = Nothing
End Sub
Here is the UserForm Code:
Private Sub CommandButton1_Click()
Me.hide 'Dismiss the form and return processing to the caller
'Call a Function to validate data
TextBox1.Text = ShoeSizeValidator(TextBox1.Text)
End Sub
Here is the validating Function:
Function ShoeSizeValidator(oShoeSize As String) As String
If IsNumeric(oShoeSize) Then
ShoeSizeValidator = oShoeSize
Else
With myFrm
.Frame1.Caption = "Invalid entry. Please enter a valid size."
.TextBox1.Text = oShoeSize
With .TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
.Show
End With
End If
End Function
He advised user form code should pretty well only exist in order to provide
data validation trough calling public procedures. I tried employing one of
his "modified" suggestions as follows.
I have a simple userform oFrm1 with a 1) command button and 2) a frame with
one textbox inside.
The purpose of this exercise is to learn to pass information from a calling
macro to a UserForm and report validated information back to the calling
macro. I am using an input box with a default value to set the
Frame.Caption in the UserForm. This part works. I am using the textbox in
the Userform to provide raw data to a Function. The Function is supposed to
validate the data. Then the validated textbox data is used to set the value
of a calling macro msgbox. Everything works if the data is valid. The
problem I can't figure out is correcting invalid data. On the first pass,
invalid data is detected and I am directed back to the Userform. However,
when I then enter valid data the routing complete but the data is lost. I
think my problem is that after I enter valid data, the code returns to the
function at the End With statement following .Show. For some reason this
scrambles the data and I just can't figure out a way around it.
Any help to get me back on track is appreciated.
Here is my calling macro:
Option Explicit
Public oPassedData As String
Public frmObject As myObject
Public myFrm As oFrm1
Sub PassData1()
Set myFrm = New oFrm1
'Pass Caption to UserForm.Frame1
myFrm.Frame1.Caption = InputBox("Enter Label1 Caption: ", _
"Pass Caption", "Enter a shoe size e.g., 10E")
myFrm.Show
MsgBox "Formatted date returned from the UserForm: " _
& myFrm.TextBox1.Text, , "Data Returned"
Set myFrm = Nothing
End Sub
Here is the UserForm Code:
Private Sub CommandButton1_Click()
Me.hide 'Dismiss the form and return processing to the caller
'Call a Function to validate data
TextBox1.Text = ShoeSizeValidator(TextBox1.Text)
End Sub
Here is the validating Function:
Function ShoeSizeValidator(oShoeSize As String) As String
If IsNumeric(oShoeSize) Then
ShoeSizeValidator = oShoeSize
Else
With myFrm
.Frame1.Caption = "Invalid entry. Please enter a valid size."
.TextBox1.Text = oShoeSize
With .TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
.Show
End With
End If
End Function