Results 1 to 13 of 13

Thread: Or....

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    9

    Or....

    Hey,
    I've got to validate a value in access, which I am doing in VB.

    Basically I'm doing something Like this

    Code:
    If LeftP = "A" or "B" or "C" .....etc then
    Some values like 0 and I will not be accepted since they are not allowed ( Car registation Number ) .

    Anyway is there anyway to make Vb check more than one character in a If statement. Because I know how to do a simple

    If Leftp= "A" then ...

    Hope you understand.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    There are few ways to do that:
    VB Code:
    1. If LeftP = "A" Or LeftP = "B" Or ... Then
    2.  
    3. Else
    4.  
    5. End If
    6.  
    7. Select Case LeftP
    8.     Case "A", "B", "C", ...
    9.         '...
    10.     Case Else
    11.         '...
    12. End Select

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    9
    I guess I could do it via an array too, using a loop right... Then declare the numbers/ letters which could be accepted at the beginnning?

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    You may certainly use arrays or array of UDT... I don't, however, know what you are after.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    9
    I think I'll just declare at the beginning something like.

    Dim AcceptedValues 0 to 22 as String

    Then delcare the accepted values like

    AcceptedValues(0)= "A" and so on.

    Then for the checking line
    For I = 0 to 24
    If LeftP= AcceptedValues(I) then
    Msgbox= "Correct Value"
    Else
    Msgbox = "Entered incorrect Value"
    End If
    Next I

    I know that code isn't 100% right.. I cant get the loop to work

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    I think you want "pattern matching". The Like operator might be what you need.

    The following code indicates the registration number must
    have a length of 6
    first character can only be A, B, C
    2nd can only be F,N,R
    3rd X,Y,Z
    4th, 5th and 6th - must be digits and cannot be 0

    VB Code:
    1. Dim strRegistrationNumber
    2.  
    3. strRegistrationNumber = "ARY173"
    4.  
    5. If strRegistrationNumber Like "[A-C][FNR][X-Z][1-9][1-9][1-9]" Then
    6.    Debug.Print "Reg # is valid"
    7. Else
    8.    Debug.Print "Reg # is invalid"
    9. End If

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    9
    Thanks brucevde + RhinoBull.
    I think that should do the trick. I might have to edit the
    "[A-C][FNR][X-Z][1-9][1-9][1-9]" to what I need it to be ( I have to do it in the Pre 2001 UK format which is slighty different to todays format.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Before you go off on that array solution, take a good look at RhinoBull's second suggestion in his first post. Using the Select Case statement can put all the options into one statement. Whether this is actually faster than looping through the arrays, would have to be tested, since I don't know how VB would implement the Select, but I would expect it to be faster.

  9. #9
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    I would say that Select Case statements are alot faster than looping through and using If...Then statements.


    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    It really depends on size of your array - you wouldn't notice difference if it's a small one but theoretically Select Case should be faster due to indexing.

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    9
    I would use case but I don't really understand how it works.

  12. #12
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    It isn't much harder to understand than If...Then statements

    VB Code:
    1. Dim str As String
    2.  
    3. str = "Hello World!"
    4.  
    5. Select Case str
    6.     Case "Hello World!": 'If str = "Hello World!" Then
    7.         MsgBox "Hello World!"
    8.     Case "Hello World 2!": 'If str = "Hello World 2!" Then
    9.         MsgBox "Hello World 2!"
    10. End Select

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  13. #13

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    9
    Thanks. That makes it easier to understand.

    Basically now I have three blocks of text

    First is a block of 2 characters which is letter block ( No Numbers)
    I can validate that it is simply letters by using access validation. But what I need to do is make sure certain letters are not used.
    IE. O, l ..etc . Is there anyway I can scan the LeftP ( Left Part) for these characters and if they are contained in this part flag it up.

    Thanks once again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width