Results 1 to 11 of 11

Thread: Like Operator [Resolved]

Hybrid View

  1. #1
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Like Operator

    I would not use LIKE. Try this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     TestAndPrint "a.a.a.a"
    5.     TestAndPrint "123.123.123.289"
    6.     TestAndPrint "1.1.1.1"
    7.     TestAndPrint "123.234.111.1"
    8. End Sub
    9.  
    10. Private Sub TestAndPrint(sIP As String)
    11.     Debug.Print sIP & ": " & IsIP(sIP)
    12. End Sub
    13.  
    14. Private Function IsIP(sIP As String) As Boolean
    15.     Dim Octet() As String
    16.     Dim i As Integer
    17.    
    18.     Octet = Split(sIP, ".")
    19.     IsIP = False
    20.    
    21.     If UBound(Octet) = 3 Then
    22.         'We have 4 parameters
    23.         IsIP = True
    24.        
    25.         For i = 0 To 3
    26.             If IsNumeric(Octet(i)) Then
    27.                 If Octet(i) < 0 Or Octet(i) > 255 Then
    28.                     'Not a valid octet
    29.                     IsIP = False
    30.                     Exit For
    31.                 End If
    32.             Else
    33.                 'It's not numeric
    34.                 IsIP = False
    35.                 Exit For
    36.             End If
    37.         Next i
    38.     End If
    39.    
    40. End Function
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    5

    Re: Like Operator

    Hey McBrain, that's pretty slick
    Hey I remember you now, when I was heavy into programming you were part of these forums too, it's all coming back to me now!!
    There’s nothing like ‘free elections’ coupled with ‘martial law’. It just screams democracy........ Matt Good

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Like Operator

    There is a possible bug in the posted IsIp function, because of its use of IsNumeric.

    With these test ip's, the function will still return True.

    TestAndPrint "123.234.111.1D1"
    TestAndPrint "123.234.111.4E1"
    TestAndPrint "123.234.111.1,1"

  4. #4
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Like Operator

    Quote Originally Posted by brucevde
    There is a possible bug in the posted IsIp function, because of its use of IsNumeric.

    With these test ip's, the function will still return True.

    TestAndPrint "123.234.111.1D1"
    TestAndPrint "123.234.111.4E1"
    TestAndPrint "123.234.111.1,1"
    What the h...?? On what planet "1D1" is numeric?? I think it's because it's considering an hex value. Here's a correction for it.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     TestAndPrint "123.234.111.1D1"
    5.     TestAndPrint "123.234.111.4E1"
    6.     TestAndPrint "123.234.111.1,1"
    7.     TestAndPrint "123.234.111.111"
    8. End Sub
    9.  
    10. Private Sub TestAndPrint(sIP As String)
    11.     Debug.Print sIP & ": " & IsIP(sIP)
    12. End Sub
    13.  
    14. Private Function IsIP(sIP As String) As Boolean
    15.     Dim Octet() As String
    16.     Dim i As Integer
    17.    
    18.     Octet = Split(sIP, ".")
    19.     IsIP = False
    20.    
    21.     If UBound(Octet) = 3 Then
    22.         'We have 4 parameters
    23.         IsIP = True
    24.        
    25.         For i = 0 To 3
    26.             If IsAllNumeric(Octet(i)) Then
    27.                 If Octet(i) < 0 Or Octet(i) > 255 Then
    28.                     'Not a valid octet
    29.                     IsIP = False
    30.                     Exit For
    31.                 End If
    32.             Else
    33.                 'It's not numeric
    34.                 IsIP = False
    35.                 Exit For
    36.             End If
    37.         Next i
    38.     End If
    39.    
    40. End Function
    41.  
    42. Private Function IsAllNumeric(sNumber As String) As Boolean
    43.     Dim i As Integer
    44.    
    45.     IsAllNumeric = True
    46.     For i = 1 To Len(sNumber)
    47.         If Not IsNumeric(Mid$(sNumber, i, 1)) Then
    48.             IsAllNumeric = False
    49.             Exit For
    50.         End If
    51.     Next i
    52. End Function
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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