TextBox Change problem

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
I am having the following problem with a textbox in my userform. The moment that any number is entered, the program starts. This is a problem if the user needs to input a 2 digit number - since the program kicks in as soon as the first digit is entered. :(

Thanks in advance for any help. Here is the code that I am using.

Gene, "The Mortgage Man", Klein


Private Sub UserGCFTextBox_Change()
Dim num1 As Integer, num2 As Integer, correctgcd As Integer
num1 = Val(TextBoxnumber1.Value)
num2 = Val(TextBoxnumber2.Value)
correctgcd = GCD(num1, num2)
If Val(UserGCFTextBox.Value) = correctgcd Then
RightGCFTextBox.Visible = True
RightGCFTextBox.Value = correctgcd
RightGCFTextBox.BackColor = vbGreen
Else
RightGCFTextBox.Visible = True
RightGCFTextBox.Value = correctgcd
RightGCFTextBox.BackColor = vbRed
End If
End Sub
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,277
Gene

What are youy actually trying to do?

Why not use another event, say Exit?
 

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
What are youy actually trying to do?

Why not use another event, say Exit?
_________________
I don't know what you mean by Exit? The user (my son) puts in an answer for the GCF of two numbers. If it is right, a box comes up green, otherwise red. The problem is if the answer is more than one digit. The change macro kicks in the moment a digit is entered in the box.

Gene, "The Mortgage Man", Klein
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,277
Well you are currently using the Change event, for something I'm assuming is data validation.

So instead of that use the textbox's Exit event to do that validation.

Of the top of my head I think the default event for a textbox is the change event. Therefore when you double click one in design view you end up with something like this.
Code:
Private Sub TextBox1_Change()

End Sub
Now the textbox has various other events, like Exit.

To access them use the right dropdown.
 

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
How would the textbox know that an "Exit" has occured? Or in this case, how would the textbox know that the user has finished typing? Would s/he have to hit enter or something?

Gene, "The Mortgage Man", Klein
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,277
Gene

The Exit event would be triggered by moving to any of the other controls on the form.

And in the exit event you can do your validation and if there is a problem stop focus going to the other control and if required display some message saying what the problem is.

Do you have any other controls?
 

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
The Exit event would be triggered by moving to any of the other controls on the form.

And in the exit event you can do your validation and if there is a problem stop focus going to the other control and if required display some message saying what the problem is.

Do you have any other controls?
_________________
What happened to my stuff?
I guess this means that the user can't just type in an answer (unless the answer will always be one digit - which it wont) and expect something to happen. I will need (I assume) to add a "check my answer" button for this to work.

Gene, "The Mortgage Man", Klein
 

Fat Cat

Active Member
Joined
Nov 5, 2004
Messages
336
You can avoid the need to click on a confirm button by just adding another textbox to your UserForm (this will provide another tab stop for the focus to switch to when you hit the enter key and trigger the Exit event).

You can hide the 2nd TextBox by making your Userform much taller at design time and placing the TextBox near the bottom of the form. Then resize the UserForm to something smaller so that the 2nd TextBox is not visible anymore.

When you run Form, you won't see the 2nd TextBox, but when you hit the Enter key after entering a 1, 2 or more digit number, the Exit event will occur and you can fire off whatever code you want to run.

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    MsgBox (TextBox1.Value)

End Sub
 

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
I've went with the confirm button, but I'll keep it in mind for the next time.

Gene, "The Mortgage Man", Klein
 

Forum statistics

Threads
1,077,780
Messages
5,336,257
Members
399,073
Latest member
fairoos

Some videos you may like

This Week's Hot Topics

Top