Results 1 to 13 of 13
  1. #1
    jsmain is offline Novice
    Windows 11 Office 365
    Join Date
    Mar 2024
    Posts
    5

    Question Table validation that excludes chars in multiple positions within length.

    I am trying to add validation to my table field to prevent entry of certain characters.
    I am entering Serial numbers and rev letter.

    Serial numbers are 6-10 chars in length, and may include B,C,D,F,G,H,J,K,L,M,N,P,R,T,V,W,X,Y,0,1,2,3,4,5,6, 7,8,9, Excluding A,E,I,O,Q,S,U,Z
    Revs may contain up to 2 chars, and exclude I,O,Q,S,X,Z, and 0-9

    I have been able to determine how to implement a validation for a single char, but multiple eludes me.
    What I've tried (Rev):

    Like "[!0-9,I,O,Q,S,X,Z]"
    Like "[!0-9,I,O,Q,S,X,Z][!0-9,I,O,Q,S,X,Z]"


    Like "[!*0-9*,*I*,*O*,*Q*,*S*,*X*,*Z*][!*0-9*,*I*,*O*,*Q*,*S*,*X*,*Z*]"

    Am I close? What am I missing???
    From what I gather, each group in sq brackets is set to a char position, but how do I make those after the first optional?

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Table field level validation is limited in many ways. That shouldn't matter since the best approach is to use forms and not edit/append in the actual tables. In fact, casual users shouldn't even see them. If you use forms and code, you can implement virtually any kind of multiple constraints.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    jsmain is offline Novice
    Windows 11 Office 365
    Join Date
    Mar 2024
    Posts
    5
    Ok, but that doesn't fix the problem, It still does the same after removing at the table level, and inserting at the form level. Are you suggesting at VBA code level (On Change event) of the form to filter the field content?

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I'm talking about a procedure (code) written to handle the constraints and the variations. Seems like there could be quite a few of them, but believe it or not, that's not really evident yet. What I did was throw out a vague suggestion because I don't know if you're open to the suggestion. Some will say that they want a non-code solution for one reason or another, so I don't just jump off the diving board and start writing stuff as a rule. A code solution greatly depends on what the inputs look like. What you've shown so far is the constraints. In order to make use of any one of several vba functions, we'd have to know what the inputs look like. It's important that your samples (if you choose to provide them) cover all the bases. We'd have to know where to expect numbers vs letters for all possibilities, and whether or not they are or must be upper case only. That would be a start. Any code you use that might be provided should work for as many variations that you know of. For those you don't, it would likely fail so it is important to cover as many variations of the inputs as possible.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    jsmain is offline Novice
    Windows 11 Office 365
    Join Date
    Mar 2024
    Posts
    5
    I thought I was clear on the constrains....

    Rev letters could be anything from A to YY (Length of 1 or 2 chars). I did not mention upper case, but they would be converted during entry regardless. Something I did not mention was it could also be a "-", so "-ABCDEFGHIJKLMNPRTUVWY" as acceptable in position 1, and the second, all except "-".

    Serial numbers are a mix of letters and numbers with the previous requirements, like 4P2HNM or 1C811F50D3 (Length of 6 to 10 chars) as examples. I've done nothing with "Macros", but have done some VBA in Excel and Access.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    constraints and the variations
    Samples that would encompass the variations is what I think is needed.
    I attended the funeral service of my best friend today, so if I seem a bit cranky it's because I'm trying to help here and there in an attempt to bury my grief. It's not really working...
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    You could use the form BeforeUpdate and use a function to check your validation requirements before accepting the serial number

  8. #8
    jsmain is offline Novice
    Windows 11 Office 365
    Join Date
    Mar 2024
    Posts
    5
    Quote Originally Posted by Micron View Post
    Samples that would encompass the variations is what I think is needed.
    I attended the funeral service of my best friend today, so if I seem a bit cranky it's because I'm trying to help here and there in an attempt to bury my grief. It's not really working...
    Sorry to hear, and for your loss!

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Thank you.
    After reviewing your first post I'm thinking you don't care where alpha or numeric characters are located, you just don't want what you posted as exclusions to be anywhere in the string. Sorry if I missed that at first.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    Join Date
    Apr 2017
    Posts
    1,680
    A probable solution:
    In BeforeUpdate event of control in form, cycle through the entry characterwise, and check for the ASCII code of every character being in allowed range(s). Something like
    Code:
    ...
    booStringIsOK = True
    For i = 1 To Len(Me.YourString)
         intCharCode = Code(Upper(Mid(Me.YourString, i, 1)))
         booStringIsOK = booStringIsOK And ((intCharCode Between 48 And 57)  Or (intCharCode Between 65 And 68) Or ...)
    Next i
    If booStringIsOK = False Then
         <some code for false entry handling>
    End If
    ...

  11. #11
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I would have thought these functions would to the the trick

    Code:
    Function ValidSerial(s As String) As Boolean
    Dim i As Integer
    Dim L As Integer
         
        L = Len(s)
        If L < 6 Or L > 10 Then
             Exit Function 'validserial is false because not in required len range
         Else
             ValidSerial = True
             i = 1
             Do While ValidSerial = True
                 ValidSerial = "BCDFGHJKLMNPRTVWXY0123456789" Like "*" & Mid(s, i, 1) & "*"
                 i = i + 1
                 If i > L Then Exit Do
             Loop
         End If
    End Function
    
    
    Function ValidRev(s As String) As Boolean
    Dim i As Integer
    Dim L As Integer
         
        L = Len(s)
        If L > 2 Then
             Exit Function 'validserial is false because not in required len range
         Else
             ValidRev = True
             i = 1
             Do While ValidRev = True
                 ValidRev = "-ABCDFGHJKLMNPRTVWY" Like "*" & Mid(s, i, 1) & "*"
                 i = i + 1
                 If i > L Then Exit Do
             Loop
         End If
    End Function
    or a more general function
    Code:
    Function Validated(s As String, minL As Integer, maxL As Integer, OKchars As String) As Boolean
    Dim i As Integer
    Dim L As Integer
         
        L = Len(s)
        If L < minL Or L > maxL Then
             Exit Function 'validserial is false because not in required len range
         Else
             Validated = True
             i = 1
             Do While Validated = True
                 Validated = OKchars Like "*" & Mid(s, i, 1) & "*"
                 i = i + 1
                 If i > L Then Exit Do
             Loop
         End If
    End Function

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Based on the initial post, I created a function to check proposed serial numbers against your criteria.
    Here is code for checking serial numbers and a test routine.You could use similar approach to deal with revisions.

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: checkSerial
    ' Purpose: Routine to check a string for length and excluded characters
    'Serial numbers are 6-10 chars in length, and may include
    'B,C,D,F,G,H,J,K,L,M,N,P,R,T,V,W,X,Y,0,1,2,3,4,5,6, 7,8,9,
    'Excluding A,E,I,O,Q,S,U,Z
    ' Procedure Kind: Function
    ' Procedure Access: Public
    ' Parameter serNo (String): String/Serial number to check
    ' Return Type: String
    ' Author: Jack
    ' Date: 13-Mar-24
    ' ----------------------------------------------------------------
    Function checkSerial(serNo As String) As String
              Dim i As Integer
              Dim ErrCondition As String
    10        ErrCondition = ""
              'check for length
    20        If (6 > Len(serNo) Or 10 < Len(serNo)) = True Then
    30            ErrCondition = ErrCondition & " -invalid length; "
                  ' Debug.Print serNo & "   has invalid length"     'for testing
    40        End If
    50        For i = 1 To Len(serNo)
                  'check for excluded chars
    60            If InStr("AEIOQSUZ", Mid(serNo, i, 1)) > 0 Then
    70                ErrCondition = ErrCondition & Mid(serNo, i, 1) & ", "
                      ' Debug.Print Mid(serNo, i, 1) & "  is illegal character"  'for testing
    80            Else
                      ' Debug.Print Mid(serNo, i, 1) & "  is OK" 'for testing
    90            End If
    100       Next i
             
    110       If Len(ErrCondition & "") = 0 Then
    120           checkSerial = UCase(serNo)
    130       ElseIf InStr(ErrCondition, ",") > 0 Then
    140           ErrCondition = ErrCondition & " -illegal character(s)"
    150           checkSerial = ErrCondition
    160       Else
    170           checkSerial = ErrCondition
    180       End If
    End Function
    Test routine

    Code:
    Sub TestCheckSerial()
              Dim i As Integer
              Dim serials(4) As String
    10        serials(0) = "Cyrr888"
    20        serials(1) = "Seri888"
    30        serials(2) = "2lng8889998YOU33"
    40        serials(3) = "iou"
    50        serials(4) = "123456789"
    60        For i = LBound(serials) To UBound(serials)
    70            Debug.Print serials(i) & "   " & checkSerial(serials(i))
    80        Next i
    End Sub
    Results of test

    Code:
    Cyrr888   CYRR888
    Seri888   S, e, i,  -illegal character(s)
    2lng8889998YOU33    -invalid length; O, U,  -illegal character(s)
    iou    -invalid length; i, o, u,  -illegal character(s)
    123456789   123456789

  13. #13
    jsmain is offline Novice
    Windows 11 Office 365
    Join Date
    Mar 2024
    Posts
    5
    Thank you all! When I get another free moment, I will give these suggestions a try.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 05-12-2018, 05:21 PM
  2. Dcount with multiple excludes
    By tagteam in forum Access
    Replies: 3
    Last Post: 10-23-2017, 02:45 PM
  3. Replies: 7
    Last Post: 01-26-2016, 01:33 PM
  4. Replies: 2
    Last Post: 06-16-2015, 03:52 AM
  5. Replies: 3
    Last Post: 06-21-2013, 09:06 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums