The following code only allows digits and a period (and only one period) with only one decimal after that period.
Be aware that this code only consider a period as a decimal sign so it's not localized for countries that use other signs (like commas in Scandinavia).Best regardsVB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) Dim nPos As Long Select Case KeyAscii Case vbKeyBack 'We allow this so we do nothing Case vbKey0 To vbKey9, Asc(".") nPos = InStr(Text1, ".") If nPos Then If Text1.SelStart > nPos Or KeyAscii = Asc(".") Then KeyAscii = 0 ElseIf Text1.SelStart = nPos And Len(Text1.Text) > nPos Then KeyAscii = 0 End If End If Case Else 'we dont allow any other characters KeyAscii = 0 End Select End Sub




Reply With Quote