Good day.

I just want to ask on how to make a textbox accept positive & negative numbers but will not accept string that was entered..

Ill try to use

if not isnumberic(text1.text) then
 msgbox"error'
else
end if

But if negative numbers is entered it will not read the negative sign..


Pls help.

Thank you for giving time.

God bless us.

Recommended Answers

All 9 Replies

You need to handle the ascii value of the keys on keypress event of the control.

You need to handle the ascii value of the keys on keypress event of the control.

thank you..I'll try it tonight.

God bless

do post back in case of any issues.

There is always the masked edit control....

Good Luck

Good day.

I just want to ask on how to make a textbox accept positive & negative numbers but will not accept string that was entered..

Ill try to use

if not isnumberic(text1.text) then
 msgbox"error'
else
end if

But if negative numbers is entered it will not read the negative sign..


Pls help.

Thank you for giving time.

God bless us.

first lets start with the textbox accepting numbers
here is the code

Private Sub txtnumbers_KeyUp(KeyCode As Integer, Shift As Integer)
  If txtnumbers.Text = "" Then
  Exit Sub
  Else
    If Not IsNumeric(txtnumbers.Text) Then
      txtnumbers.Text = Left(txtnumbers.Text, Len(txtnumbers.Text) - 1)
      SendKeys "{end}"
    Else
    End If
  End If
End Sub

Hi

Private Sub text1_KeyUp(KeyCode As Integer, Shift As Integer)
      If text1.Text = "" Then
      Exit Sub
      Else
      If Not IsNumeric(text1.Text) Then
      text1.Text = Left(text1.Text, Len(text1.Text) - 1)
      SendKeys "{end}"
      Else
      End If
      End If
      End Sub

try this
I got this code somewhere and it proved to be helpful
Happy Programing

ankush.mukherje,

You got it from the post directly preceeding yours!!!!

Once again, the masked edit box will work if you set up its mask properly, but if you don't want to use it, then use this...

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyBack Then Exit Sub 'allow user to correct mistakes
If KeyAscii >= 48 And KeyAscii <= 57 Then 'numbers 0 to 9
ElseIf KeyAscii = 45 Or KeyAscii = 43 Then '+ and - keys
Else
  KeyAscii = 0
End If
End Sub

But as you can see this verification does not allow for decimal points or commas, and it also allows for entry like this... "-123+345", which is why the masked edit control is so nice...

Good Luck

vb5prgrmr,
I am sorry to say but I did not see my preceding code..
I just saw the question and posted my own post

vb5prgrmr,
I am sorry to say but I did not see my preceding code..
I just saw the question and posted my own post

thank you guys. it really help me.

this function also worked but it only accept numbers without decimal places.

Function DontAllowString(Ascii As Integer) As Integer
If (Ascii < 48 Or Ascii > 57) And Ascii <> 8 Then
    DontAllowString = 0
Else
    DontAllowString = Ascii
End If

Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = DontAllowString(KeyAscii)
End Sub


End Function

God bless us all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.