如何检查用户在 Textbox 中输入的值是否为双精度值? [英] How do I check if a value a user is entering in Textbox is a numeric double?
本文介绍了如何检查用户在 Textbox 中输入的值是否为双精度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检查用户是否在文本框中输入了一个数值,是否接受小数位.非常感谢任何帮助.
I am trying to check if a user enters a number value in a textbox, decimal places accepted. Any help is highly appreciated.
Private Sub textbox1_AfterUpdate()
If IsNumeric(textbox1.Value) = False Then
Me!textbox1.Undo
MsgBox "only numbers are allowed"
Exit Sub
End If
Exit Sub
使用 BeforeUpdate 事件:
using BeforeUpdate event:
Private Sub textbox1_BeforeUpdate(Cancel As Integer)
If IsNumeric(textbox1.Value) = False Then
MsgBox "only numbers are allowed"
Me!textbox1.Undo
Cancel = True
Exit Sub
End If
Exit Sub
我当前的代码根本不执行.我也在 textbox1_BeforeUpdate 事件中尝试过.请查看代码.
My current code does not execute at all. I have also tried it in the textbox1_BeforeUpdate event. Please see code.
新代码:
Public Function IsValidKeyAscii(ByVal keyAscii As Integer, ByVal value As
String) As Boolean
IsValidKeyAscii = (keyAscii = vbKeyDot And InStr(1, value, Chr$(vbKeyDot)) =
0) Or (keyAscii >= vbKey0 And keyAscii <= vbKey9)
End Function
Private Sub textbox1_KeyDown(KeyCode As Integer, Shift As Integer)
If Not IsValidKeyAscii(KeyCode, textbox1.value) Then KeyCode = 0
End Sub
推荐答案
您根本不应该将 VBA 用于此任务.
You shouldn't be using VBA for this task at all.
只需将字段格式属性设置为General number.这是确保用户只能在字段中输入数字的内置方法.
Just set the field format property to General number. That's the built-in way to ensure users can only enter numbers in a field.
这篇关于如何检查用户在 Textbox 中输入的值是否为双精度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文