|
-
Apr 18th, 2005, 02:20 PM
#1
Thread Starter
New Member
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
-
Apr 18th, 2005, 02:26 PM
#2
Re: Like Operator
It won't work like that. Try something like this:
VB Code:
Isit = False
Isit = IPS Like "#.#.#.#" Or _
IPS Like "#.#.#.##" Or _
IPS Like "#.#.#.###" Or _
IPS Like "###.###.###.###"
-
Apr 18th, 2005, 02:27 PM
#3
Frenzied Member
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:
Public Function IPSim(ByVal IPS As String) As Boolean
Dim Isit As Boolean
Isit = (IPS Like "#.#.#.#") Or (IPS Like"#.#.#.##") Or (IPS Like "#.#.#.###" )...
Also, have you looked into the possibility of using regular expressions?
-
Apr 18th, 2005, 02:29 PM
#4
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
-
Apr 18th, 2005, 02:30 PM
#5
Re: Like Operator
With the 'or' command you must specify the complete statement,
VB Code:
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
-
Apr 18th, 2005, 02:31 PM
#6
Thread Starter
New Member
Re: Like Operator
 Originally Posted by dglienna
It won't work like that. Try something like this:
VB Code:
Isit = False
Isit = IPS Like "#.#.#.#" Or _
IPS Like "#.#.#.##" Or _
IPS Like "#.#.#.###" Or _
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
-
Apr 18th, 2005, 02:33 PM
#7
Re: Like Operator
 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
-
Apr 18th, 2005, 02:36 PM
#8
Need-a-life Member
Re: Like Operator
I would not use LIKE. Try this:
VB Code:
Option Explicit
Private Sub Form_Load()
TestAndPrint "a.a.a.a"
TestAndPrint "123.123.123.289"
TestAndPrint "1.1.1.1"
TestAndPrint "123.234.111.1"
End Sub
Private Sub TestAndPrint(sIP As String)
Debug.Print sIP & ": " & IsIP(sIP)
End Sub
Private Function IsIP(sIP As String) As Boolean
Dim Octet() As String
Dim i As Integer
Octet = Split(sIP, ".")
IsIP = False
If UBound(Octet) = 3 Then
'We have 4 parameters
IsIP = True
For i = 0 To 3
If IsNumeric(Octet(i)) Then
If Octet(i) < 0 Or Octet(i) > 255 Then
'Not a valid octet
IsIP = False
Exit For
End If
Else
'It's not numeric
IsIP = False
Exit For
End If
Next i
End If
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.
-
Apr 18th, 2005, 02:57 PM
#9
Thread Starter
New Member
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
-
Apr 18th, 2005, 03:32 PM
#10
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"
-
Apr 18th, 2005, 03:47 PM
#11
Need-a-life Member
Re: Like Operator
 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:
Option Explicit
Private Sub Form_Load()
TestAndPrint "123.234.111.1D1"
TestAndPrint "123.234.111.4E1"
TestAndPrint "123.234.111.1,1"
TestAndPrint "123.234.111.111"
End Sub
Private Sub TestAndPrint(sIP As String)
Debug.Print sIP & ": " & IsIP(sIP)
End Sub
Private Function IsIP(sIP As String) As Boolean
Dim Octet() As String
Dim i As Integer
Octet = Split(sIP, ".")
IsIP = False
If UBound(Octet) = 3 Then
'We have 4 parameters
IsIP = True
For i = 0 To 3
If IsAllNumeric(Octet(i)) Then
If Octet(i) < 0 Or Octet(i) > 255 Then
'Not a valid octet
IsIP = False
Exit For
End If
Else
'It's not numeric
IsIP = False
Exit For
End If
Next i
End If
End Function
Private Function IsAllNumeric(sNumber As String) As Boolean
Dim i As Integer
IsAllNumeric = True
For i = 1 To Len(sNumber)
If Not IsNumeric(Mid$(sNumber, i, 1)) Then
IsAllNumeric = False
Exit For
End If
Next i
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|