验证以逗号分隔的值列表是否遵循特定序列

时间:2014-02-05 14:49:36

标签: vba ms-access ms-access-2007 access-vba

我创建了一个循环来检查输入的值是否与排序匹配,具体取决于传递的扩充。因此,例如,排序约束必须是

"SU", "M", "TU", "W", "TH", "F", "SA" 

因此,如果用户输入以下输入

"SU,M,TU,SA"这是正确的

但是如果用户输入

"SU,TH,M"这是不正确的,因为M应该在TH

之前

编码已经实现并且工作正常但是我没有发现这是编码它的最佳方式,任何人都可以帮我更有效地编码吗?

  Function validExDays(exDays As String)
    Dim found As Boolean
    found = False

    If Len(exDays) >= 1 And Not IsNull(exDays) Then

    Dim NumOfCommas As Integer
    NumOfCommas = InstrCount(exDays, ",")

    Dim days(0 To 7) As String
    days(0) = ","
    days(1) = "SU"
    days(2) = "M"
    days(3) = "TU"
    days(4) = "W"
    days(5) = "TH"
    days(6) = "F"
    days(7) = "SA"
    Dim i, j, k, l, m, o, p, q As Integer
    i = 1
    j = 1
    k = 1
    l = 1
    m = 1
    o = 1
    p = 1
    q = 1


        Do While i <= 7
            If NumOfCommas = 0 Then
                'One day input check
                If i = 1 Then
                     Do While j <= 7
                        If UCase(exDays) = days(j) Then
                            found = True
                            Exit Do
                        End If
                        j = j + 1
                     Loop
                End If
            End If

            'Two day input check
            j = 1
            If NumOfCommas = 1 Then
                If found = False And i = 2 Then
                    Do While j <= 7
                        Do While k <= 7
                           If UCase(exDays) = days(j) + days(0) + days(k) Then
                               found = True
                               Exit Do
                           End If
                           k = k + 1
                        Loop

                        If found = False Then
                           j = j + 1
                           k = j
                        Else
                            Exit Do
                        End If
                    Loop
                End If
            End If

            'Three day input check

因此输入的字符串值可以是“SU,M,F”或“SU,F”或任何其他组合,但包含的任何项目必须按正确顺序排列。

2 个答案:

答案 0 :(得分:1)

以下代码更紧凑。它使用Split()函数来分解组件,并使用Dictionary对象来保存每个有效组件的索引值

Option Compare Database
Option Explicit

Public Function IsValidExDays(exDays As String) As Boolean
    Dim rtn As Boolean
    Dim valueArray() As String, valueItem As Variant
    Dim maxValue As Integer
    Dim dict As Object  ' Scripting.Dictionary

    rtn = True

    Set dict = CreateObject("Scripting.Dictionary")
    dict.Add "SU", 1
    dict.Add "M", 2
    dict.Add "TU", 3
    dict.Add "W", 4
    dict.Add "TH", 5
    dict.Add "F", 6
    dict.Add "SA", 7

    maxValue = 0
    valueArray = Split(exDays, ",")
    For Each valueItem In valueArray
        If dict.Exists(valueItem) Then
            If dict(valueItem) > maxValue Then
                maxValue = dict(valueItem)
            Else
                rtn = False
                Exit For
            End If
        Else
            rtn = False
            Exit For
        End If
    Next

    Set dict = Nothing
    IsValidExDays = rtn
End Function

答案 1 :(得分:0)

由于您处于Access状态,为什么不使用复选框创建一个输入表单,该复选框将始终按您期望的顺序返回参数?