用户表单输入验证 [英] Userform entry validation

查看:28
本文介绍了用户表单输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含特定文本框的用户表单,我只想应用数字规则(没有字符串条目).我在为此创建足够的错误处理程序时遇到了困难.本质上,因为这些文本框将用于执行数学函数,具有字符串值会导致 subs 崩溃,我无法找出在字符串条目处停止的正确语法.

I have a userform with specific TextBoxes that I want to apply numeric rules only (no string entries). I am having difficulties with creating an adequate error handler for this. Essentially, because these textboxes are going to be used to perform a mathematical function, having string values causes the subs to crash and I cannot figure out the correct syntax to halt at string entries.

我当前的代码是:

Private Sub TextBox12_Change()
    Sumdatup
End Sub

Private Sub TextBox16_Change()
Sumdatup
End Sub

Private Sub TextBox21_Change()
Sumdatup
End Sub

Private Sub Sumdatup()
Dim Total As Double
Total = 0
If Len(TextBox12.Value) > 0 Then Total = Total + CDbl(TextBox12.Value)
If Len(TextBox16.Value) > 0 Then Total = Total + CDbl(TextBox16.Value)
If Len(TextBox21.Value) > 0 Then Total = Total + CDbl(TextBox21.Value)
' Add more for the rest of your text boxes
TextBox26.Value = Total
End Sub

我曾尝试合并另一个 sub 来捕获字符串值,但我继续得到错误,该错误可以追溯到 Sumdatup 程序中的第一个 If 子句.

I had tried incorporating another sub to trap the string values from going through, but I continue to get an error tracing back to the first If clause in the Sumdatup program.

这是我尝试过的导致错误的方法:

This is what I have tried that gives me errors:

Private Sub TextBox12_Change()
NumbersOnly
Sumdatup
End Sub

Private Sub TextBox16_Change()
NumbersOnly
Sumdatup
End Sub

Private Sub TextBox21_Change()
NumbersOnly
Sumdatup
End Sub

Private Sub NumbersOnly()

If TypeName(Me.ActiveControl) = "TextBox" Then
With Me.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Then
MsgBox "Only numeric values allowed."
.Value = vbNullString

End If
End With
End If
End Sub

Private Sub Sumdatup()
Dim Total As Double
Total = 0
If Len(TextBox12.Value) > 0 Then Total = Total + CDbl(TextBox12.Value)
If Len(TextBox16.Value) > 0 Then Total = Total + CDbl(TextBox16.Value)
If Len(TextBox21.Value) > 0 Then Total = Total + CDbl(TextBox21.Value)
' Add more for the rest of your text boxes
TextBox26.Value = Total
End Sub

代码似乎从不检查 NumbersOnly 子程序,而是直接转到 Sumdatup 代码,在我尝试输入字符串值时出错...

The code never seems to check the NumbersOnly sub and goes straight to the Sumdatup code where it errors out when I try entering string values...

对我如何以不同的方式进行这件事有什么想法吗?

Any thoughts on how I might go at this a different way?

推荐答案

如果你真的需要边跑边做,你可以这样做:

If you really need to do it on the run, you can do it like this:

Private Sub TextBox12_Change()
  If chkNum Then Sumdatup
  If IsNumeric(TextBox12.Value) Then TextBox12.BackColor = 16777215 Else TextBox12.BackColor = 255
End Sub

Private Sub TextBox16_Change()
  If chkNum Then Sumdatup
  If IsNumeric(TextBox16.Value) Then TextBox16.BackColor = 16777215 Else TextBox16.BackColor = 255
End Sub

Private Sub TextBox21_Change()
  If chkNum Then Sumdatup
  If IsNumeric(TextBox21.Value) Then TextBox21.BackColor = 16777215 Else TextBox21.BackColor = 255
End Sub

Private Function chkNum() As Boolean
  If IsNumeric(TextBox12.Value) And IsNumeric(TextBox16.Value) And IsNumeric(TextBox21.Value) Then
    chkNum = (Len(TextBox12.Value) * Len(TextBox16.Value) * Len(TextBox21.Value)) > 0
  End If
End Function

Private Sub Sumdatup()
  TextBox26.Value = TextBox12.Value + TextBox16.Value + TextBox21.Value
End Sub

这篇关于用户表单输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆