Excel VBA - Cursor disappears when validating content of a TextBox

L

lonewolfbr

I ask you friend's help for this problem:

I designed a form with several TextBoxes, some of which I need to check
to prevent input of invalid data, with some criteria I previously
defined (for instance, input must be an integer, between 1 and 999,
inclusive).

Then, with the BeforeUpdate event, I display a MsgBox to alert user,
and I want the focus to return to the TextBox with the invalid entry,
in order to be changed to a valid one.

My code worked fine except for a strange thing: cursor disappears when
user clicks "Ok", and seems to be nowhere, as pressing Tab, Shift-Tab,
Esc or Enter doesn't any effect.

If user wants to start all up again, I must to make invisible the frame
in which that TextBox is, but then VB returns an runtime error
'-2147418113 (8000ffff)'. It seems that MSForms does something in those
situations that takes the focus off any part of my application. I need
somebody who understands it to explain it to me, please.

Thank you all folks very much! My code is below.


Private Sub TextBox_BeforeUpdate(ByVal Cancel As
MSForms.ReturnBoolean)
If IsNumeric(TextBox.Value) Then
If CInt(TextBox.Value) < 1 Or CInt(TextBox.Value) > 999 Then
If MsgBox("Entry must be between 1 and 999. Click (OK) to
enter new dara, or (Cancel) to clean form up and start again",
vbOKCancel + vbExclamation, "Attention!") = vbCancel Then
Cancel = False
Frame1.Visible=False
Else
TextBox= ""
TextBox.SetFocus
Cancel = True
Exit Sub
End If
End If
Else
BotCaixa = MsgBox("Entry must be an INTEGER, between 1 and
999.", vbOKOnly + vbExclamation, "Attention!")
TextBox = ""
TextBox.SetFocus
Cancel = True
End If
End Sub
 

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