VBScript数据验证-数值1导致无限循环 [英] VBScript Data Validation - Numeric 1 Results in Infinite Loop

查看:55
本文介绍了VBScript数据验证-数值1导致无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我仍然是具有编程概念的uber-n00b,并且知道足够多的VBS会对自己造成伤害,因此我很可能毁/屠杀此程序所涉及的一些术语/概念/想法,我试图写.您,拥有完全权利要解雇我的极其优秀的程序员,已受到警告.

DISCLAIMER: I'm still an uber-n00b with programming concepts and know just enough VBS to hurt myself, so I'm likely to slur/slaughter some of the terms/concepts/ideas that are entailed with this program I'm trying to write. You, the vastly superior programmer who has every right to flame me, have been warned.

我一直在尝试编写VBScript来验证数据,特别是日期.由于我的用户对键盘的使用感觉很差,因此我想通过分隔日期的每个部分的输入来简化操作(仅月和日,我已静态设置了年份).

I've been trying to write a VBScript to validate data, specifically Dates. Since my users are kind of poor with keyboards, I figured I'd make it easy by separating the entry of each part of the date (Month & Day only, I've got the Year set statically).

以前,我只用1个"Do ... Loop"来验证数字时遇到问题,因为我试图验证它是否为数字输入,并同时检查它是否在指定范围内(1 -一年中的12个月为12).

Previously, I was having problems with validating the numbers with only 1 "Do...Loop", as I was trying to verify if it was Numeric Input and checking at the same time if it was within the specified range (1 - 12, for the 12 months of the year).

这是以前的代码大致的样子:

This is what the previous code roughly looked like:

Do
    ' If...Then Statements Here
Loop Until (dtFirstMonth > 0) _
    And (dtFirstMonth < 13) _
    And IsNumeric(dtFirstMonth) _
    And (dtFirstMonth <> "") _
    And (dtFirstMonth <> vbNull)

这通常会导致数据类型不匹配"错误,因此我不得不将Validation Critera拆分为两个单独的"Do ... Loop"语句,如下面的当前代码所示:

This often resulted in "Data Type Mismatch" errors, so I had to split the Validation Critera to two separate "Do...Loop" statements, as you can see in the current code I have below:

Sub srTest()
    Do
        dtFirstMonth = InputBox("Please Enter a Numeric Month for the Starting Range", _
            "Starting Range Month")

        If (dtFirstMonth = vbNull) _
            Or (dtFirstMonth = "") _
            Or Not IsNumeric(dtFirstMonth) Then
                MsgBox "Please Enter a Valid Numeric Month",, "Enter Month Number"
        ElseIf (dtFirstMonth <> vbNull) _
            And (dtFirstMonth <> "") _
            And IsNumeric(dtFirstMonth) Then
                Do
                    dtFirstMonth = Round(dtFirstMonth)
                    Wscript.Echo dtFirstMonth ' Infinite Loop Here (Basically, As Soon As We Get Into Loop with a Value of 1, We're Stuck)
                    dtFirstMonth = CInt(dtFirstMonth)
                        ' Must Convert User Input to Integer to 
                        '   Prevent Data Mismatch Errors In 
                        '   Following "If" Statement; Besides, 
                        '   It Passed the First Test to be a
                        '   Numeric Value in the First Place

                    If (dtFirstMonth < 1) Or (dtFirstMonth > 12) Then
                        MsgBox "Please Enter a Valid Numeric Month",, "Enter Month Number"
                        Exit Do
                            ' Drop Out of 2nd Level Loop to 
                            '   Enter Into 1st Level Loop
                    End If
                Loop Until (dtFirstMonth > 0) _
                    And (dtFirstMonth < 13) _
                    And IsNumeric(dtFirstMonth) _
                    And (dtFirstMonth <> "") _
                    And (dtFirstMonth <> vbNull)

                If (dtFirstMonth < 1) Or (dtFirstMonth > 12) Then
                    dtFirstMonth = ""
                End If
                    ' dtFirstMonth Was Converted to Integer Earlier
                    ' This is to Meet the Value that Didn't Pass 
                    '   the Nested Do & If Statement (Level 2 Do Loop)
                    ' Sets dtFirstMonth to "Empty String" to Continue 
                    '   Looping in the Level 1 "Do...Loop" Statement; 
                    '   If Omitted, Level 1 "Do...Loop" is Satisfied, 
                    '   Thus Ending the Subroutine (Since the Value 
                    '   of dtFirstMonth is Still a Numeric Value)
        End If
Loop Until IsNumeric(dtFirstMonth) _
    And (dtFirstMonth <> "") _
    And (dtFirstMonth <> vbNull)

    Wscript.Echo dtFirstMonth
End Sub

srTest

我必须设置第一个"Do ... Loop"来检查用户输入(dtFirstMonth)确实是一个数字值,而不是空值或空字符串.嵌套的"Do ... Loop"或第二个"Do ... Loop"语句是我具有相同的条件以及定义所需范围的额外条件(介于1到12之间的任何数字)的地方.

I had to set up the 1st "Do...Loop" to check that the User Input (dtFirstMonth) was a indeed a Numeric Value and not a Null Value nor an Empty String. The Nested "Do...Loop", or 2nd "Do...Loop", statement is where I have the same Criteria plus the extra Criteria defining the desired ranges (any number between 1 and 12).

这对于2-12号是完美的,但是当脚本解析1号时,我进入了无限循环.

This is working perfectly for number 2-12, but when the script parses the number 1, I enter into an Infinite Loop.

我已经通过将整个第二个"Do ... Loop"部分替换为"Wscript.Echo dtFirstMonth"来确保在第二个"Do ... Loop"中发生无限循环.通过这样做,我得到了预期的结果:单个Echo,而不是无限数量(从技术上讲,我得到2,因为在子例程的底部确实有另一个"Wscript.Echo dtFirstMonth"字符串用于调试) ,但无论哪种方式,它都不是无限循环).

I've checked to make sure that the Infinite Loop is occurring in the 2nd "Do...Loop" by replacing the entire 2nd "Do...Loop" section with "Wscript.Echo dtFirstMonth". By doing this, I get the expected results: a single Echo, not an infinite number of them (technically, I get 2, as I do have another "Wscript.Echo dtFirstMonth" string at the bottom of the Subroutine for the purpose of debugging, but either way, it's not an Infinite Loop).

我也将较低范围的标准更改为这样,但这并不能纠正错误:

I've also changed the criterion for the lower range to be like this, yet this doesn't remediate the error:

Do
    ' If...Then Statements Here
Loop Until (dtFirstMonth >= 1)

我也尝试过这种方法,但没有成功:

I've also tried this, with no resulting success:

Do
    ' If...Then Statements Here
Loop Until (dtFirstMonth >= CInt(1))

在所有现实中,实际上都不需要此段,因为无论如何我都将用户的输入转换为整数.

In all reality, there really is no need for this segment, since I converted the User's Input to an integer anyway.

因为这开始变得令人困惑,所以我决定在脚本将用户输入传递给"CInt"函数之前添加"Round"语句,希望它可以确保不会被捕获为0.值或十进制值;是的,这对我来说是非理性的想法,但是我仍然想探索所有途径(还有一个事实,我有一些用户使用胖手指综合症",而另一些用户则使用滥用程序"思想,所以我想d确保该脚本占了十进制条目).我在嵌套的"Do ... Loop"之前和之后添加了"Round"字符串,但仍然遇到无限循环问题.

Since this was starting to get confusing, I decided to add the "Round" statement before the script passed the User's Input to the "CInt" function, hoping that it would make sure that it wasn't getting caught as a 0 value or decimal value somehow; yes, this is irrational thought on my part, but I still wanted to explore all avenues (there's also the fact that I have some users with "Fat Finger Syndrome" and some others with "Abuse The Program" mentality, so I figured I'd make sure the script accounted for decimal entries). I added the "Round" string before and after the nested "Do...Loop" and I still had the Infinite Loop issue.

这是我所能做到的,现在我被困住了.

This is as far as I've been able to get on this and now I'm stuck.

我意识到在VBScript中可能有更好的方法来进行日期/时间验证,我当然愿意接受任何新建议,但是我很乐意为纯粹的教育而解决这个问题.

I realize that there are likely better ways to do Date/Time Validation in VBScript, and I'm certainly open to any new suggestions, but I'd love to solve this for the pure sake of edification.

推荐答案

简单输入数字的代码太多.只是尝试使其简短而简单.示例:

Way too much code for a simple input of a number. Just try to keep it short and simple. Example:

Do
    dtm = InputBox("Please Enter a Numeric Month for the Starting Range", _
                "Starting Range Month")
    Select Case True
        Case isNull(dtm), (not isNumeric(dtm)), dtm = "", dtm = empty, (dtm < 1 OR dtm > 12)
            ' too exhaustive, but just for the sake of the example.
             MsgBox "Please enter an amount between 1 and 12"
        Case else
            ' Hey, this seems to be a valid amount!
            Exit do
    End Select
Loop While True

'  Do something with dtm

只是向您展示了一些富有创意的Select Casing,它支持延迟退出,因此,如果值为Null,则在进行评估之前会先转义,否则评估可能会引发错误.

Just showed you some creative Select Casing, this supports lazy exit, so if a value is Null, it escapes before getting evaluated where evaluating could throw an error.

这篇关于VBScript数据验证-数值1导致无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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