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

pasting in Numeric field 1

Status
Not open for further replies.

jass1220

Programmer
May 20, 2003
88
0
0
AE
am using this code to prevent from entering letters in the numeric fields

//////////////////////////////
Private Sub txtPhone_KeyPress(KeyAscii As Integer)

If KeyAscii <> 8 Then
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
MsgBox (&quot;Only Numbers allowed&quot;)
End If
End If

End Sub
///////////////////


because this code in the keypress event .. if i copied and pasted letters in this field it will accept it !!

so what shall i do ?
 
Why not use the IsNumeric function in Vb?

Dim MyVar as long, MyCheck as boolean
For example
MyVar = &quot;53&quot; ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.

but if MyVar = &quot;Help&quot;
MyCheck = IsNumeric(MyVar) 'Returns false


If mycheck = false then
msgbox(&quot;Please enter a numeric value&quot;
end if

You can pretty it up but it seems it would be easier to use this. I could be wrong.

Hope it helps.

Joanne
 
jas1220
You might check the value of the string you pasted in the TextBox Using the Change Event. If it's not numeric, notify the user with a MsgBox and return focus to the TextBox.
I prefer to valdate the fields when the user Updates the
form rather than trying to pick out an Event that won't
affect some other behavior as the user tabs through the
boxes.

jayare
 
i've done this .. it working .. thanks programers


////////////////////

Dim Check As Boolean

Check = IsNumeric(Clipboard.GetText())
If Check = False Then
If Me.ActiveControl.Name = &quot;txtDays&quot; Or Me.ActiveControl.Name = &quot;txtLimit&quot; Or Me.ActiveControl.Name = &quot;txtFees&quot; Or Me.ActiveControl.Name = &quot;txtHours&quot; Then
MsgBox (&quot;This is a numeric field, cannot paste letters in it&quot;)
Else
Me.ActiveControl.SelText = Clipboard.GetText()
End If
Else
Me.ActiveControl.SelText = Clipboard.GetText()
End If
End Sub

/////////////////
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top