Return to original text box in case of an error

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,341
I have a userform with 9 controls, of which 6 are textboxes. Some errors can be detected only on leaving the textbox. If an illegal entry is detected on leaving a textbox, my _AfterUpdate code will erase the entry and give a warning message. Typically users will have tabbed to the next textbox, but they may click any other control. I would like thecursor to be forced back to the empty textbox so that they may correct the error. Effectively, whatever control they then select, they will be returned to the textbox where the error was detected. If this is not possible, that information will stop me wasting any more time.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi, This is a very small example to get you the concept:
VBA Code:
Dim myTextBox As Object
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  Set myTextBox = ActiveControl
End Sub
Private Sub TextBox1_Change()
  If "some_error" Then
    myTextBox.SetFocus
  End If
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  Set myTextBox = ActiveControl
End Sub
Private Sub TextBox2_Change()
  If "some_error" Then
    myTextBox.SetFocus
  End If
End Sub
 
Upvote 0
Thanks for that Flashbond. A problem I see is that "If some error" is in the TextBox1_Change code. I have various tests for illegal entries in the TextBox, e.g. letters entered instead of numbers. The problem occurs when someone tabs off the TextBox. It is only when that that happens, that I am able to test the final value that is proposed and determine whether or not it is 'legal'. Somehow I need to cancel the change to a new textbox and return the focus to the previous TextBox. As an example, if values must be greater than 10, "2" is not an error as it may be the start of "20". If the TextBox is left at that stage, "2" is an error. I wish to be able to return the user to that text box with a message explaining the error. The only problem I have is cancelling the selection change.
 
Upvote 0
It was an interesting exercise for me :) This is the cleanest solution I could come up with:
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  Cancel = makeChecks
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  Cancel = makeChecks
End Sub
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  Cancel = makeChecks
End Sub
Function makeChecks() As Boolean
  Select Case ActiveControl.Name
  Case "TextBox1"
  If CDbl(ActiveControl.Text) > 10 Then
    MsgBox "TextBox1 must be smaller than 10"
    makeChecks = True
  End If
  Case "TextBox2"
  If CDbl(ActiveControl.Text) < 10 Then
    MsgBox "TextBox2 must be greater than 10"
    makeChecks = True
  End If
  Case "TextBox3"
  If CDbl(ActiveControl.Text) <> 10 Then
    MsgBox "TextBox2 must be equal than 10"
    makeChecks = True
  End If
  End Select
End Function
 
Last edited by a moderator:
Upvote 0
Solution
My most grateful thanks Flashbond. It works perfectly and I am greatly in debt to you. I'd been working for two days with no success to get what you've given me.
Not that it matters in the least, but your last message should be "equal to 10":)
Thanks again for all the work you've put into this. It is much appreciated.
 
Upvote 0

Forum statistics

Threads
1,215,107
Messages
6,123,126
Members
449,097
Latest member
mlckr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top