G
Greg Maxey
The standard VBA InputBox does not provide a means of determining if
the user clicked on cancel or just left the input field blank.
I put together a UserForm to perform the role of an InputBox that
allows me to determine which user action occurred.
I basically built a form with a label, a textbox, an OK commandbutton
and a Cancel command but.
I assigned "Cancel" = True for the Cancel commandbutton. In my code I
set the value of the textbox to "**Canceled by user**" if the user
clicked the cancel button, clicked Esc, or clicked the close form
"X."
While perhaps crude, it worked like I expected. Next I thought I
might set the value of the .Tag property to "**Canceled by user**" if
one of the actions above occurred and set .Tag to "TextBox1.Text" if
the OK command button was pressed.
This worked also except for when the user closed the form with the "X"
button.
On that action, the VB Editor returned an:
Run Time Error '2147418105(8011007)'
Automation Error. The callee (server [not server application]) is not
available and dissappared. All connections are invalid. The call may
have been executed.
After stepping through my code I can "almost" understand why that
occurs. However, I don't understand why it occurs when I use .Tag
vice .TextBox1.Text
Here is my code with both variations. The current arrangement
uses .TextBox1.Text. It works as expected. If you change the
arrangement and use .Tag, the error occurs on the line:
CallInputBox = myFrm.Tag
Main Macro:
Sub ScratchMacro()
Dim myStr As String
myStr = CallInputBox("User Name", "What is your name?")
If myStr = "**Canceled by user**" Then
Exit Sub
Else
MsgBox myStr
End If
End Sub
Function opens the UserForm:
Function CallInputBox(ByRef pTitle As String, ByRef pPrompt As String,
_
Optional ByRef pDefualt As String) As String
Dim myFrm As myInputBox
Set myFrm = New myInputBox
myFrm.Caption = pTitle
myFrm.Label1 = pPrompt
myFrm.TextBox1 = pDefualt
myFrm.Show
CallInputBox = myFrm.TextBox1.Text
'CallInputBox = myFrm.Tag
Unload myFrm
Set myFrm = Nothing
End Function
UserForm code:
Private Sub CommandButton1_Click()
If Me.TextBox1.Text = vbNullString Then
MsgBox "Please enter your name in the text field provided"
Me.TextBox1.SetFocus
Else
Me.Tag = Me.TextBox1.Text
Me.Hide
End If
End Sub
Private Sub CommandButton2_Click()
Me.TextBox1.Text = "**Canceled by user**"
'Me.Tag = "**Canceled by user**"
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = 0 Then
Me.TextBox1.Text = "**Canceled by user**"
'Me.Tag = "**Canceled by user**"
End If
End Sub
If I step through the code and close the Userform with the "x" I can
see that the UserForm_QuerryClose Event (and Terminate Event) fires
before the Function CallInputBox gets its value.
So why does myFrm.TextBox1.Text work when myFrm.Tag throws the error?
Also interested in other ideas to streamline or improve on this
UserForm idea.
Thanks.
the user clicked on cancel or just left the input field blank.
I put together a UserForm to perform the role of an InputBox that
allows me to determine which user action occurred.
I basically built a form with a label, a textbox, an OK commandbutton
and a Cancel command but.
I assigned "Cancel" = True for the Cancel commandbutton. In my code I
set the value of the textbox to "**Canceled by user**" if the user
clicked the cancel button, clicked Esc, or clicked the close form
"X."
While perhaps crude, it worked like I expected. Next I thought I
might set the value of the .Tag property to "**Canceled by user**" if
one of the actions above occurred and set .Tag to "TextBox1.Text" if
the OK command button was pressed.
This worked also except for when the user closed the form with the "X"
button.
On that action, the VB Editor returned an:
Run Time Error '2147418105(8011007)'
Automation Error. The callee (server [not server application]) is not
available and dissappared. All connections are invalid. The call may
have been executed.
After stepping through my code I can "almost" understand why that
occurs. However, I don't understand why it occurs when I use .Tag
vice .TextBox1.Text
Here is my code with both variations. The current arrangement
uses .TextBox1.Text. It works as expected. If you change the
arrangement and use .Tag, the error occurs on the line:
CallInputBox = myFrm.Tag
Main Macro:
Sub ScratchMacro()
Dim myStr As String
myStr = CallInputBox("User Name", "What is your name?")
If myStr = "**Canceled by user**" Then
Exit Sub
Else
MsgBox myStr
End If
End Sub
Function opens the UserForm:
Function CallInputBox(ByRef pTitle As String, ByRef pPrompt As String,
_
Optional ByRef pDefualt As String) As String
Dim myFrm As myInputBox
Set myFrm = New myInputBox
myFrm.Caption = pTitle
myFrm.Label1 = pPrompt
myFrm.TextBox1 = pDefualt
myFrm.Show
CallInputBox = myFrm.TextBox1.Text
'CallInputBox = myFrm.Tag
Unload myFrm
Set myFrm = Nothing
End Function
UserForm code:
Private Sub CommandButton1_Click()
If Me.TextBox1.Text = vbNullString Then
MsgBox "Please enter your name in the text field provided"
Me.TextBox1.SetFocus
Else
Me.Tag = Me.TextBox1.Text
Me.Hide
End If
End Sub
Private Sub CommandButton2_Click()
Me.TextBox1.Text = "**Canceled by user**"
'Me.Tag = "**Canceled by user**"
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = 0 Then
Me.TextBox1.Text = "**Canceled by user**"
'Me.Tag = "**Canceled by user**"
End If
End Sub
If I step through the code and close the Userform with the "x" I can
see that the UserForm_QuerryClose Event (and Terminate Event) fires
before the Function CallInputBox gets its value.
So why does myFrm.TextBox1.Text work when myFrm.Tag throws the error?
Also interested in other ideas to streamline or improve on this
UserForm idea.
Thanks.