devxlogo

Roll-Your-Own Decimal Entry Filter

Roll-Your-Own Decimal Entry Filter

Here’s an easy method for making sure your users enter only numeric data, and only one decimal point. First, place two Public procedures in a standard module. You can use Private procedures in a form if you’re only using it there, but you’ll lose easy portability for future projects.

The first procedure makes sure the decimal point is only entered once. The second procedure filters out all non-numeric characters except the decimal point:

 Public Sub DecCheck(Target As String, ByRef KeyStroke As _	Integer)	If InStr(Target, ".") And KeyStroke = 46 Then		KeyStroke = 0	End IfEnd SubPublic Sub NumCheck(ByRef KeyStroke As Integer)	If (KeyStroke < 48 Or KeyStroke > 57) And (KeyStroke _		<> 46 And KeyStroke <> 8) Then		KeyStroke = 0	End IfEnd Sub

Then invoke the code from your TextBox’s KeyPress event:

 Private Sub txtUnitPrice_KeyPress(KeyAscii As Integer)	DecCheck txtUnitPrice, KeyAscii	NumCheck KeyAsciiEnd Sub

One caveat: This code doesn’t prevent text characters from being pasted in via the clipboard.

See also  Monetize TikTok For Your Business
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist