Results 1 to 11 of 11

Thread: Like Operator [Resolved]

  1. #1

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

    Like Operator [Resolved]

    Hi all!!
    I did a seach for the like operator and could find this question asked before so here goes.....

    I have a Function that takes a string (of an IP address) and uses the like operation to see if the format of the IP is correct,

    Code:
    Public Function IPSim(ByVal IPS As String) As Boolean
        Dim Isit As Boolean
        Isit = IPS Like "#.#.#.#" Or "#.#.#.##" Or "#.#.#.###" Or "#.#.##.#" Or "#.#.##.##" Or "#.#.##.###" Or _
        "#.#.###.#" Or "#.#.###.##" Or "#.#.###.###" Or "#.##.#.#" Or "#.##.#.##" Or "#.##.#.###" Or "#.##.##.#" Or _
        "#.##.##.##" Or "#.##.##.###" Or "#.##.###.#" Or "#.##.###.##" Or "#.##.###.###" Or "#.###.#.#" Or _
        "#.###.#.##" Or "#.###.#.###" Or "#.###.##.#" Or "#.###.##.##" Or "#.###.##.###" Or "#.###.###.#" Or _
        "#.###.###.##" Or "#.###.###.###" Or "##.#.#.#" Or "##.#.#.##" Or "##.#.#.###" Or "##.#.##.#" Or _
        "##.#.##.##" Or "##.#.##.###" Or "##.#.###.#" Or "##.#.###.##" Or "##.#.###.###" Or "##.##.#.#" Or _
        "##.##.#.##" Or "##.##.#.###" Or "##.##.##.#" Or "##.##.##.##" Or "##.##.##.###" Or "##.##.###.#" Or _
        "##.##.###.##" Or "##.##.###.###" Or "##.###.#.#" Or "##.###.#.##" Or "##.###.#.###" Or "##.###.##.#" Or _
        "##.###.##.##" Or "##.###.##.###" Or "##.###.###.#" Or "##.###.###.##" Or "##.###.###.###" Or "###.#.#.#" Or _
        "###.#.#.##" Or "###.#.#.###" Or "##.#.##.#" Or "##.#.##.##" Or "###.#.##.###" Or "###.#.###.#" Or _
        "###.#.###.##" Or "###.#.###.###" Or "###.##.#.#" Or "###.##.#.##" Or "###.##.#.###" Or "###.##.##.#" Or _
        "###.##.##.##" Or "###.##.##.###" Or "###.##.###.#" Or "###.##.###.##" Or "###.##.###.###" Or _
        "###.###.#.#" Or "###.###.#.##" Or "###.###.#.###" Or "###.###.##.#" Or "###.###.##.##" Or "###.###.##.###" Or _
        "###.###.###.#" Or "###.###.###.##" Or "###.###.###.###"
        If Isit = True Then...........
    Anyways, when I run this I get a type mismatch error 13. Now I have done something like this before and it did work, is there some glaring error that I'm overlooking?

    Thanks in advance!!
    Last edited by BooDa72; Apr 18th, 2005 at 05:08 PM.
    There’s nothing like ‘free elections’ coupled with ‘martial law’. It just screams democracy........ Matt Good

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Like Operator

    It won't work like that. Try something like this:

    VB Code:
    1. Isit = False
    2. Isit = IPS Like "#.#.#.#" Or _
    3.         IPS Like "#.#.#.##" Or _
    4.         IPS Like "#.#.#.###" Or _
    5.         IPS Like "###.###.###.###"

  3. #3
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Like Operator

    Hi. Welcome to the forum

    I don't know much about the Like function, but you need to change the way that you use it:
    VB Code:
    1. Public Function IPSim(ByVal IPS As String) As Boolean
    2.     Dim Isit As Boolean
    3.     Isit = (IPS Like "#.#.#.#") Or (IPS Like"#.#.#.##") Or (IPS Like "#.#.#.###" )...

    Also, have you looked into the possibility of using regular expressions?

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Like Operator

    In cases like this you'd probably be better off using Regular Expressions, take up much less room

    EDIT:

    Something like this should do the trick:

    Code:
    ^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9
    ])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[
    1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]
    {1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}
    [0-9]{1}|[0-9])$
    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Like Operator

    With the 'or' command you must specify the complete statement,

    VB Code:
    1. If [B]MyVariable = "Hello"[/B] or [B]myVariable = "By Bye"[/B] or [B]myVariable = "argh"[/B] then

    Its the same with your code above you must after each or specify what you are comparing.

    Hope that helps

    Pino

    Edit - Very late sorry, went to geta drink

  6. #6

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

    Re: Like Operator

    Quote Originally Posted by dglienna
    It won't work like that. Try something like this:

    VB Code:
    1. Isit = False
    2. Isit = IPS Like "#.#.#.#" Or _
    3.         IPS Like "#.#.#.##" Or _
    4.         IPS Like "#.#.#.###" Or _
    5.         IPS Like "###.###.###.###"
    Ohhhhh Right!!! I forgot about that!!!! Thanks dglienna, I went throught this before when I first started to use like, I have been away from programming for a while, now I'm getting back into it. It's like starting all over again!!
    Thanks to everyone for the help and the welcomes!!
    There’s nothing like ‘free elections’ coupled with ‘martial law’. It just screams democracy........ Matt Good

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Like Operator

    Quote Originally Posted by BooDa72
    Ohhhhh Right!!! I forgot about that!!!! Thanks dglienna, I went throught this before when I first started to use like, I have been away from programming for a while, now I'm getting back into it. It's like starting all over again!!
    Thanks to everyone for the help and the welcomes!!
    No problem, glad you have solved it.

    If you could edit the first post and add [resolved] to the subject and/or as the icon that would be great

    Thank-You

  8. #8
    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.

  9. #9

    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

  10. #10
    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"

  11. #11
    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