PDA

View Full Version : Text box focus



msretenovic
2004-11-12, 04:36 PM
Hi all,

I have been trying to figure this out all morning and I need a little push.

I have a text box on a form. I am checking to see if the textbox holds a valid real number. If it does not have a real number in it, I display a message telling the user that they must enter a number. Now, I would like to highlight the text in the text box, but I don't know how to do that, and I can't seem to find what I'm looking for in the help :| .

I have tried to use textBox1.setFocus, but after reading in the help, I realized that setFocus was meant for the window, not the text box.

I would appreciate any help, Thanks!!!

Ed Jobe
2004-11-12, 05:01 PM
Here's a sample of what I use:


Private Sub tbHeight_Change()
'Only Height values greater than 0 are allowed.
'zero height is only allowed in styles.
On Error Resume Next
Dim dbl As Double
dbl = tbHeight.Value
If dbl <= 0 Then
MsgBox "Height must be a numerical value greater than zero.", vbExclamation + vbOKOnly
tbHeight.Value = ThisDrawing.GetVariable("TEXTSIZE")
tbHeight.SelStart = 0
tbHeight.SelLength = Len(tbHeight.Text)
End If
End Sub

msretenovic
2004-11-12, 07:44 PM
Ed,

Thanks for the code, but it is still not highlighting :confused: . It is allowing me to go to another text box while leaving the invalid data in the text box I am validating. Grrrrrr....... I'm a little frustrated that I'm just not getting it right now. :banghead:

Here is a snippet of what I have:



Private Sub tbWindowllx_AfterUpdate()
On Error Resume Next
If Not isNumber(tbWindowllx.Value) Then
MsgBox "Value must be a number!", vbExclamation + vbOKOnly
tbWindowllx.SelStart = 0
tbWindowllx.SelLength = Len(tbWindowllx.Text)
End If
End Sub


Here is the isNumber function that I am using:



Private Function isNumber(val As String)
Dim num As Double
Dim isNum As Boolean
On Error Resume Next
num = CDbl(val)
If Err <> 0 Then
isNum = False
Else
isNum = True
End If
isNumber = isNum
End Function

Coolmo
2004-11-12, 07:51 PM
Here's what I use:



Private Sub TextBox1_Change()
If Val(TextBox1.Text) = 0 And TextBox1.Text <> "0" And TextBox1.Text <> "0." And TextBox1.Text <> "0.0" And TextBox1.Text <> "0.00" Then
TextBox1.Text = "0"
End If
End Sub

This basically doesn't allow the user to enter anything other than a real number but it will allow them to enter a real number starting with "0" and running through "0.00". Otherwise the "Val(TextBox1.Text) = 0" would trigger the IF statement. Anything else entered into the box will trigger this IF statement and automatically set the value to zero (0). It works great and you don't have to worry about telling the person to enter something correct. Of course I choose a default of zero (0) but you could put what makes better sense to you as a default.

Hope this helps

msretenovic
2004-11-12, 08:01 PM
Thanks, but doesn't really help me too much. I tried it, and when I entered 9y0, it didn't do anything. I need this to highlight the text so that the user can fix it. With the code that I show, I could have it set to zero. But, I want it to allow the user to change it if they put a mistake in the box.

But, keep the suggestions coming, I will try whatever I need 'til I get this thing figured out.

msretenovic
2004-11-12, 08:59 PM
Ok, so I can't seem to get what I want. Soooooo, I decided on a work around until someone can give me a REALLY BIG kick in the right direction. I have gone with changing the background color of the cell to yellow. Then, if the user clicks the ok button before correcting the error, I can redirect them (I think). Still a work in progress, but I appreciate the suggestions.

Ed Jobe
2004-11-13, 12:31 AM
I think the difference it the event you're using. Notice I used the Change event. This gets the data before it's updated, and when you have a chance to do something about it. AfterUpdate is too late.

msretenovic
2004-11-15, 02:26 PM
Ed,

I understand what you were saying now . Thank you, that does what I want.

:Puffy:

Have one or two on me :beer: :beer: