Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 63
Default Userform SetFocus with Exit and Cancel

Dear All,

I have a userform with a couple of textboxes and an OK and Cancel button at
the bottom of the form. After consulting this news group, I found the Exit
event (I was using the AfterUpdate event before, but wanted the setfocus to
be in a certain textbox.) For validation purposes, I have set up the Exit
event as you can see in the following:

Private Sub SymbolBox_Exit(ByVal cancel As MSForms.ReturnBoolean)
'
Dim RngFound As Range, SymbolRange As Range 'SellRange as Range
Dim SellSymbol As String ' LstRow As Integer
If SymbolBox.Value = "ABC" Then
If Not IsNumeric(SymbolBox.Value) Then
SymbolBox.Value = UCase(SymbolBox.Value)
SellBox.SetFocus
Else
MsgBox "Need to type in a valid symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
End If
Else
MsgBox "Need to enter a symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
'Exit Sub
End If

End Sub

The problem is if the user does not enter any data, but wants to exit the
form and clicks on the Cancel button, nothing happens. The userform does not
close. How can I get the userform to close? Thanks in advance.

Neal
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Userform SetFocus with Exit and Cancel

I think I'd drop the msgboxes and use a label on the userform.

And if you change the cancelbutton.takefocusonclick to false, they can click the
cancel button.



Option Explicit

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub SymbolBox_Exit(ByVal cancel As MSForms.ReturnBoolean)
'
Dim RngFound As Range, SymbolRange As Range 'SellRange as Range
Dim SellSymbol As String ' LstRow As Integer

me.label1.caption = ""
If SymbolBox.Value = "ABC" Then
If Not IsNumeric(SymbolBox.Value) Then
SymbolBox.Value = UCase(SymbolBox.Value)
sellbox.SetFocus
Else
Me.Label1.Caption = "Need to type in a valid symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
End If
Else
Me.Label1.Caption = "Need to enter a symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
'Exit Sub
End If

End Sub

Private Sub UserForm_initialize()
Me.Label1.Caption = ""
Me.CommandButton1.TakeFocusOnClick = False
End Sub

I'm not sure what you were doing in your test code, though.


Neal wrote:

Dear All,

I have a userform with a couple of textboxes and an OK and Cancel button at
the bottom of the form. After consulting this news group, I found the Exit
event (I was using the AfterUpdate event before, but wanted the setfocus to
be in a certain textbox.) For validation purposes, I have set up the Exit
event as you can see in the following:

Private Sub SymbolBox_Exit(ByVal cancel As MSForms.ReturnBoolean)
'
Dim RngFound As Range, SymbolRange As Range 'SellRange as Range
Dim SellSymbol As String ' LstRow As Integer
If SymbolBox.Value = "ABC" Then
If Not IsNumeric(SymbolBox.Value) Then
SymbolBox.Value = UCase(SymbolBox.Value)
SellBox.SetFocus
Else
MsgBox "Need to type in a valid symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
End If
Else
MsgBox "Need to enter a symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
'Exit Sub
End If

End Sub

The problem is if the user does not enter any data, but wants to exit the
form and clicks on the Cancel button, nothing happens. The userform does not
close. How can I get the userform to close? Thanks in advance.

Neal


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 63
Default Userform SetFocus with Exit and Cancel

Dave,

Thanks for the advice. I will try it. What I was trying to do in my code
was test to see if the user entered the right three digit code. If it was
not correct or nothing was entered or it was numeric, then I wanted the
messagebox to popup and say that the user needed to enter a three digit
alphanumeric code. When the user hit OK on the msgbox, the setfocus would
return to the SymbolBox. I will try the labels. Do the labels look like the
message boxes? Thanks.

Neal

"Dave Peterson" wrote:

I think I'd drop the msgboxes and use a label on the userform.

And if you change the cancelbutton.takefocusonclick to false, they can click the
cancel button.



Option Explicit

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub SymbolBox_Exit(ByVal cancel As MSForms.ReturnBoolean)
'
Dim RngFound As Range, SymbolRange As Range 'SellRange as Range
Dim SellSymbol As String ' LstRow As Integer

me.label1.caption = ""
If SymbolBox.Value = "ABC" Then
If Not IsNumeric(SymbolBox.Value) Then
SymbolBox.Value = UCase(SymbolBox.Value)
sellbox.SetFocus
Else
Me.Label1.Caption = "Need to type in a valid symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
End If
Else
Me.Label1.Caption = "Need to enter a symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
'Exit Sub
End If

End Sub

Private Sub UserForm_initialize()
Me.Label1.Caption = ""
Me.CommandButton1.TakeFocusOnClick = False
End Sub

I'm not sure what you were doing in your test code, though.


Neal wrote:

Dear All,

I have a userform with a couple of textboxes and an OK and Cancel button at
the bottom of the form. After consulting this news group, I found the Exit
event (I was using the AfterUpdate event before, but wanted the setfocus to
be in a certain textbox.) For validation purposes, I have set up the Exit
event as you can see in the following:

Private Sub SymbolBox_Exit(ByVal cancel As MSForms.ReturnBoolean)
'
Dim RngFound As Range, SymbolRange As Range 'SellRange as Range
Dim SellSymbol As String ' LstRow As Integer
If SymbolBox.Value = "ABC" Then
If Not IsNumeric(SymbolBox.Value) Then
SymbolBox.Value = UCase(SymbolBox.Value)
SellBox.SetFocus
Else
MsgBox "Need to type in a valid symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
End If
Else
MsgBox "Need to enter a symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
'Exit Sub
End If

End Sub

The problem is if the user does not enter any data, but wants to exit the
form and clicks on the Cancel button, nothing happens. The userform does not
close. How can I get the userform to close? Thanks in advance.

Neal


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Userform SetFocus with Exit and Cancel

No. The labels look like textboxes with a grey background(?).

Ahhh. You'll see it when you try it.

Neal wrote:

Dave,

Thanks for the advice. I will try it. What I was trying to do in my code
was test to see if the user entered the right three digit code. If it was
not correct or nothing was entered or it was numeric, then I wanted the
messagebox to popup and say that the user needed to enter a three digit
alphanumeric code. When the user hit OK on the msgbox, the setfocus would
return to the SymbolBox. I will try the labels. Do the labels look like the
message boxes? Thanks.

Neal

"Dave Peterson" wrote:

I think I'd drop the msgboxes and use a label on the userform.

And if you change the cancelbutton.takefocusonclick to false, they can click the
cancel button.



Option Explicit

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub SymbolBox_Exit(ByVal cancel As MSForms.ReturnBoolean)
'
Dim RngFound As Range, SymbolRange As Range 'SellRange as Range
Dim SellSymbol As String ' LstRow As Integer

me.label1.caption = ""
If SymbolBox.Value = "ABC" Then
If Not IsNumeric(SymbolBox.Value) Then
SymbolBox.Value = UCase(SymbolBox.Value)
sellbox.SetFocus
Else
Me.Label1.Caption = "Need to type in a valid symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
End If
Else
Me.Label1.Caption = "Need to enter a symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
'Exit Sub
End If

End Sub

Private Sub UserForm_initialize()
Me.Label1.Caption = ""
Me.CommandButton1.TakeFocusOnClick = False
End Sub

I'm not sure what you were doing in your test code, though.


Neal wrote:

Dear All,

I have a userform with a couple of textboxes and an OK and Cancel button at
the bottom of the form. After consulting this news group, I found the Exit
event (I was using the AfterUpdate event before, but wanted the setfocus to
be in a certain textbox.) For validation purposes, I have set up the Exit
event as you can see in the following:

Private Sub SymbolBox_Exit(ByVal cancel As MSForms.ReturnBoolean)
'
Dim RngFound As Range, SymbolRange As Range 'SellRange as Range
Dim SellSymbol As String ' LstRow As Integer
If SymbolBox.Value = "ABC" Then
If Not IsNumeric(SymbolBox.Value) Then
SymbolBox.Value = UCase(SymbolBox.Value)
SellBox.SetFocus
Else
MsgBox "Need to type in a valid symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
End If
Else
MsgBox "Need to enter a symbol"
cancel = True
SymbolBox.Text = ""
SymbolBox.SetFocus
'Exit Sub
End If

End Sub

The problem is if the user does not enter any data, but wants to exit the
form and clicks on the Cancel button, nothing happens. The userform does not
close. How can I get the userform to close? Thanks in advance.

Neal


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get userform Cancel button to invoke Exit Sub in calling macro? Craig Remillard New Users to Excel 1 November 19th 09 03:55 AM
Cancel/Exit Sub Howard Excel Discussion (Misc queries) 3 December 16th 08 09:53 PM
InputBox / VBA question ok = print, cancel = exit todd78 Excel Worksheet Functions 4 August 9th 07 04:42 PM
Cancel an application exit from VBA? helmekki[_98_] Excel Programming 3 October 28th 05 03:31 AM
Cancel Exit on duplicate Garry Jones Excel Programming 2 November 3rd 03 11:21 PM


All times are GMT +1. The time now is 02:55 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"