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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textboxes, how do I limit entries 3

Status
Not open for further replies.

Trowser

IS-IT--Management
Dec 16, 2002
125
GB
ok I have a textbox for people to submit data
but I only want them to be able to type in numbers.
is there anyway to do this?
 
YES

use the testbox_KeyPress(...) sub...

Private Sub Text1_KeyPress(KeyAscii As Integer)
temp = Chr(KeyAscii)
If temp < &quot;0&quot; Or temp > &quot;9&quot; Then KeyAscii = 0
End Sub

have fun
-Josh S
 
brilliant thanks right now how do I limit them to only entering a number before 0 and 50.

I have used the Lostfocus on the textbox to test what is in it then return an error if it isn't between 0 and 50 but is there a better way?
 
Use the validate event...

Also, having code in the keypress event won't stop people from copying and pasting into your text box...

If I were you I would just use the validate event to make sure that: #1 it is a number and #2 that it is between 0 and 50.

 
A better way to limit users to enter only numbers would be to use Microsoft Masked Edit control instead of the text box.
Include this control in your project and pull up CUSTOM properties for this control.
Set the mask to ##(this would limit the users from entering only 2 digit numbers).

Cheers
Razvi.
 
thanks guys thats given me alot to think about :)
 
*** Skip 1-3 and go to 4 ;-)

1) Set Text1.MaxLength to 2

2) if you want it to change to 50 if you type 51-99:

Private Sub Text1_Change()
temp = Val(Text1)
If temp > 50 Then Text1 = &quot;50&quot;
End Sub

3) to allow the backspace button (chr(8)) to be pressed...

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub ' backspace key
temp = Chr(KeyAscii)
If temp < &quot;0&quot; Or temp > &quot;9&quot; Then KeyAscii = 0
Text1.MaxLength = 2
End Sub

4) this is the BEST code to use, and the only code you need... I finally figured it out ;-)

Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
  If KeyAscii = 8 Then Exit Sub ' backspace key
  temp = Chr(KeyAscii)
  If temp < &quot;0&quot; Or temp > &quot;9&quot; Then KeyAscii = 0
  
  temp = Text1
  If Text1.SelLength > 0 Then
    If Text1.SelText = temp Then
      temp = Chr(KeyAscii)
    Else
      temp = Left(temp, Text1.SelStart) + Chr(KeyAscii) + _
        Right(temp, Len(temp) - Text1.SelStart - Text1.SelLength)
    End If
  Else
    n = Text1.SelStart
    If n = 0 Then temp = Chr(KeyAscii) + temp
    If n > 0 And n = Len(Text1) Then temp = temp + Chr(KeyAscii)
    If n > 0 And n < Len(Text1) Then temp = Left(temp, n) + _
      Chr(KeyAscii) + Right(temp, Len(Text1) - n)
  End If
  temp = Val(temp)
  If temp > 50 Then KeyAscii = 0
End Sub

Thanks,
-Josh Stribling
 
use #2 above to eliminate the possibility of paste loop hole...

Private Sub Text1_Change()
temp = Val(Text1)
If temp > 50 Then Text1 = &quot;50&quot;
End Sub
 
' Place these code into Keypress event

Dim strValid As String
Dim i%

strValid = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;

KeyAscii = Asc(UCase(Chr(KeyAscii)))

If KeyAscii > 26 Then
If InStr(strValid, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
Beep
End If
End If
 
Errrm.... PotatoTang,

You appear to be allowing all alphanumeric characters through!

? and what is the integer variable for?
________________________________________________________________
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.'
 
i just know the basics of programing using VB 6.
can anybody give me a comprehensive tutorial of Visual Basic.
thnks
 
devdba,

It's best to start a new thread for an entirely different subject see FAQ referred below

A quick search in this forum will give loads of answers to your question. e.g.
thread222-317567
thread222-432795
thread222-415870
________________________________________________________________
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.'
 

Trowser:

see thread222-363768 and thread222-370383 [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

If you use the KeyDown/KeyPress/KeyUp event, you will need extra code in the Validate/Change event to handle when the user pastes a string into the text box.
The change event seems appropiate to handle most all situations.
If you already know that the values being put into the textbox are correct, such as setting the text in code, then you can use a form level boolean variable to exit the proceedure.

Here is something else to try:
Option Explicit
Private m_bNoChange As Boolean

Private Sub Text1_Change()
Static bGetOut As Boolean
Dim Char As String
Dim Pos As Integer
If bGetOut Or m_bNoChange Then Exit Sub

Pos = Text1.SelStart
If Pos = 0 Then Pos = 1
Char = Mid$(Text1.Text, Pos, 1)
bGetOut = True
If Char = vbNullString Then
Text1.Text = &quot;0&quot;
Text1.SelStart = 1
ElseIf Not IsNumeric(Char) And Asc(Char) <> 46 And Asc(Char) <> 44 Then
bGetOut = True
Text1.Text = Replace(Text1.Text, Char, vbNullString)
Text1.SelStart = Pos - 1
End If
If Not IsNumeric(Text1.Text) Then
Text1.Text = &quot;0&quot;
Text1.SelStart = 1
End If
bGetOut = False
End Sub

Private Sub Text1_Validate(Cancel As Boolean)
m_bNoChange = True
Text1.Text = = Format$(Text1.Text)
m_bNoChange = False
End Sub [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

hey johnwm,

u can assign your desired in the value &quot;strValid&quot;.

For example, if the textbox is just allowed to be entered &quot;ABCDEFG&quot;, then set &quot;strValid = &quot;ABCDEFG&quot;.
Then the text box is just allowing &quot;ABCDEFG&quot; to be inputted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top