Help!

Determining class of IP address

 
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> User Forms RSS
Next:  How get more than two decimal places in values re..  
Author Message
call_me_sol
External


Since: Jun 02, 2007
Posts: 3



PostPosted: Fri Jul 06, 2007 11:02 pm    Post subject: Determining class of IP address
Archived from groups: microsoft>public>word>vba>beginners, others (more info?)

Well, today's project is learning how to read the classful boundry of
an IP address.

Looking at the first octet of an IP address, I need to know the class
of that address:

Class A = 1-126
Class B = 128 - 191
Class C = 192 - 223

Looking at the first octet to determine the class:
64.192.29.2 = Class A = 64.0.0.0
172.16.32.4 = Clsss B = 172.16.0.0
208.16.14.1 = Class C = 208.16.14.0

I need a userform such that if user inputs any IP address into
textbox1 the form will return the Class of the written address as a
docvariable.

Can anyone show me how to do this?

Thank you!!
Back to top
Jay Freedman
External


Since: Mar 17, 2004
Posts: 3038



PostPosted: Sat Jul 07, 2007 2:53 am    Post subject: Re: Determining class of IP address [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Fri, 06 Jul 2007 19:02:57 -0700, wrote:

>Well, today's project is learning how to read the classful boundry of
>an IP address.
>
>Looking at the first octet of an IP address, I need to know the class
>of that address:
>
>Class A = 1-126
>Class B = 128 - 191
>Class C = 192 - 223
>
>Looking at the first octet to determine the class:
>64.192.29.2 = Class A = 64.0.0.0
>172.16.32.4 = Clsss B = 172.16.0.0
>208.16.14.1 = Class C = 208.16.14.0
>
>I need a userform such that if user inputs any IP address into
>textbox1 the form will return the Class of the written address as a
>docvariable.
>
>Can anyone show me how to do this?
>
>Thank you!!

Make a userform with a textbox named txtIP, a command button named
cmdClass, and a label named lblClass (plus any other controls you
like) and give the button this code:

Private Sub cmdClass_Click()
Dim IPaddr As Variant

If Len(txtIP.Text) > 0 Then
IPaddr = Split(txtIP.Text, ".")
If UBound(IPaddr) <> 3 Then
lblClass = "Not a valid IP address"
Exit Sub
End If

Select Case (CLng(IPaddr(0)))
Case 1 To 126
lblClass = "Class A"
Case 128 To 191
lblClass = "Class B"
Case 192 To 223
lblClass = "Class C"
Case Else
lblClass = "Not classified"
End Select
End If
End Sub

The Split function makes an array in the Variant, consisting of the
nodes of the address separated by periods (the periods aren't stored
in the Variant). The Select Case then figures out which range the
first node belongs to, and assigns the corresponding value to the
label.

The check for the upper bound of the variant makes sure there are four
nodes (numbering starts at 0). You could also throw in checks to make
sure all the nodes are numeric and that none of them have values
greater than 255; failing either test would also make the IP address
invalid.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Back to top
Russ
External


Since: Oct 02, 2005
Posts: 19



PostPosted: Sat Jul 07, 2007 2:55 am    Post subject: Re: Determining class of IP address [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Call_me_sol,
Greg Maxey showed you last month how get your data and work with select case
with the macro below.
So what are you having a problem with now?
You can use the left() function to return the first three characters.

Private Sub CommandButton1_Click()
Dim myArray
Dim lngCount As Long
Dim i As Long
Dim pString As String
Dim pString2
myArray = Split(Me.TextBox1.Text, Chr(13))
lngCount = UBound(myArray) + 1
Select Case lngCount
Case Is < 10
pString2 = "ip prefix maximum 100"
Case Is > 11
Select Case lngCount
Case Is < 100
pString2 = "ip prefix list maximum 1000"
Case Is > 100
pString2 = "ip prefix list maximum 10000"
Case Else
End Select
Case Else
pString = "Whatever"
End Select
For i = 0 To UBound(myArray)
pString = myArray(i)
ActiveDocument.Range.InsertAfter "ip prefix list AS2345 permit ip "
& pString & vbCr
Next i
ActiveDocument.Range.InsertAfter pString2
Me.Hide
End Sub





> Well, today's project is learning how to read the classful boundry of
> an IP address.
>
> Looking at the first octet of an IP address, I need to know the class
> of that address:
>
> Class A = 1-126
> Class B = 128 - 191
> Class C = 192 - 223
>
> Looking at the first octet to determine the class:
> 64.192.29.2 = Class A = 64.0.0.0
> 172.16.32.4 = Clsss B = 172.16.0.0
> 208.16.14.1 = Class C = 208.16.14.0
>
> I need a userform such that if user inputs any IP address into
> textbox1 the form will return the Class of the written address as a
> docvariable.
>
> Can anyone show me how to do this?
>
> Thank you!!
>

--
Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID
Back to top
call_me_sol
External


Since: Jun 02, 2007
Posts: 3



PostPosted: Sat Jul 07, 2007 10:45 am    Post subject: Re: Determining class of IP address [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi -

Thanks for the reply. The part I'm specifically having a problem with
is taking the value that's input into the textbox and returning it in
its classful boundary.

So, if the user enters 64.192.29.2, I want the form to return 64.0.0.0
(because it's part of a class A block).
If the user enters 208.16.14.1, I want the form to return 208.16.14.0
(because it's parrt of a class C block).

I guess I don't know how to take inputted value, dissect it, and then
return it formatted as described above.

Thanks!!
Back to top
Jay Freedman
External


Since: Mar 17, 2004
Posts: 3038



PostPosted: Sat Jul 07, 2007 6:40 pm    Post subject: Re: Determining class of IP address [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sat, 07 Jul 2007 06:45:48 -0700, wrote:

>Hi -
>
>Thanks for the reply. The part I'm specifically having a problem with
>is taking the value that's input into the textbox and returning it in
>its classful boundary.
>
>So, if the user enters 64.192.29.2, I want the form to return 64.0.0.0
>(because it's part of a class A block).
>If the user enters 208.16.14.1, I want the form to return 208.16.14.0
>(because it's parrt of a class C block).
>
>I guess I don't know how to take inputted value, dissect it, and then
>return it formatted as described above.
>
>Thanks!!

The Split function has already separated the input into its separate
nodes. All you need now is to put the appropriate pieces (depending on
class) back together, followed by the necessary ".0" nodes:

Private Sub cmdClass_Click()
Dim IPaddr As Variant

If Len(txtIP.Text) > 0 Then
IPaddr = Split(txtIP.Text, ".")
If UBound(IPaddr) <> 3 Then
lblClass = "Not a valid IP address"
Exit Sub
End If

Select Case (CLng(IPaddr(0)))
Case 1 To 126
lblClass = "Class A: " & _
IPaddr(0) & ".0.0.0"
Case 128 To 191
lblClass = "Class B: " & _
IPaddr(0) & "." & IPaddr(1) & ".0.0"
Case 192 To 223
lblClass = "Class C: " & _
IPaddr(0) & "." & IPaddr(1) & "." & IPaddr(2) & ".0"
Case Else
lblClass = "Not classified"
End Select
End If
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> User Forms All times are: Eastern Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum