Text Box : Controlling what the user inputs

M

Matthew Balch

Dear All,

I have a user form that has several text boxs that require input. How do I
format these boxes / control what the user inputs into them.

ie on the change sub of the text box I need a control that says

If textbox1.numberformat = "dd-mm-yyyy" Then Next
Else
MsgBox("Please enter date as dd-mm-yyyy")


Furthermore I would also like to format a text box so that percentages can
be entered. At present I have had to highlight to the user that they need to
be entered as a decimal. Is there any way around this?


Thanks in advance

And thanks for all your previous help. Starting to get the basics of this
and put together some 'good' templates.

Kind Regards
Matthew Balch
 
B

Bob Phillips

You cannot set formatr to textboxes as you can with cells (unfortunately),
and there is no built-in masking either, so you have to either build it
yourself, or check after the event. For instance,

With Me.TextBox1
If Not IsDate(.Text) Then
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
MsgBox ("Please enter date as dd-mm-yyyy")
End If
End With


For percentages, assuming not greater than 100, you can try

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With Me.TextBox1
Debug.Print .Text, KeyAscii
Select Case KeyAscii
Case 48 To 57 'numbers
If (Len(.Text) = 2 And (.Text <> "10" Or KeyAscii <> 48)) Or
_
Len(.Text) = 3 Then
Beep
KeyAscii = 0
End If
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select
End With
End Sub


--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)
 
M

Matthew Balch

Thanks Bob :)

Bob Phillips said:
You cannot set formatr to textboxes as you can with cells (unfortunately),
and there is no built-in masking either, so you have to either build it
yourself, or check after the event. For instance,

With Me.TextBox1
If Not IsDate(.Text) Then
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
MsgBox ("Please enter date as dd-mm-yyyy")
End If
End With


For percentages, assuming not greater than 100, you can try

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With Me.TextBox1
Debug.Print .Text, KeyAscii
Select Case KeyAscii
Case 48 To 57 'numbers
If (Len(.Text) = 2 And (.Text <> "10" Or KeyAscii <> 48)) Or
_
Len(.Text) = 3 Then
Beep
KeyAscii = 0
End If
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select
End With
End Sub


--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)
 

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