Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create InPut Mask For Text Box 1

Status
Not open for further replies.

bebbo

Programmer
Dec 5, 2000
621
0
0
GB
I don't appear to find an input mask property for text box. I want to make people enter numbers not strings how do I do that?
 
Select Project > Components
Then select Microsoft Masked Edit Control

Then you can alter the Mask property.
 
The easiest way is as Qamgine says. If you use the text1_change event and someone enters 1234567A it will erase
all of the numbers not just the A. There is a way to do it using the change event but it requires a little more code to get it to work correctly.
 
Check out faq222-2244 - it gives some help on getting good answers. See particularly paragraphs 8 & 9 as there are at least 2 faqs on this subject
faq222-45 and faq222-302 solve different aspects of the same problem. There are also numerous threads turned up by 'Search'

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Whilst we were on the subject I thought I'd have a play around with anything non-numerical :) Maybe not the best solution but you could use the following to remove any non-numeric characters:
Code:
Private Sub Text1_Validate(Cancel As Boolean)
Dim i

For i = 1 To Len(Text1.Text)
    If Not IsNumeric(Mid(Text1.Text, i, 1)) Then
        Text1.Text = Trim(Replace(Text1.Text, Mid(Text1.Text, i, 1), ""))
        If i > Len(Text1.Text) Then
            Exit For
        Else
            i = i - 1
        End If
    End If
Next

End Sub

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Little Bit of Modification on Ca8msm's code to stop the cursor moving back to the start of the box after entering a non-numeric character.

[tt]
Private Sub Text1_Change() 'Didn't work 4me in validate
Dim i
Dim CursorNow As Long

CursorNow = Text1.SelStart
For i = 1 To Len(Text1.Text)
If Not IsNumeric(Mid(Text1.Text, i, 1)) Then
Text1.Text = Trim(Replace(Text1.Text, Mid(Text1.Text, i, 1), ""))
Text1.SelStart = CursorNow
If i > Len(Text1.Text) Then
Exit For
Else
i = i - 1
End If
End If
Next i

End Sub
[/tt]

Good code ca8msm.


jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top