Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Determining class of IP address

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
call_me_sol@yahoo.com - 07 Jul 2007 03:02 GMT
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!!
Jay Freedman - 07 Jul 2007 03:53 GMT
>Well, today's project is learning how to read the classful boundry of
>an IP address.
[quoted text clipped - 18 lines]
>
>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.
call_me_sol@yahoo.com - 07 Jul 2007 14:45 GMT
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!!
Jay Freedman - 07 Jul 2007 19:40 GMT
>Hi -
>
[quoted text clipped - 11 lines]
>
>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.
Russ - 07 Jul 2007 03:55 GMT
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.
[quoted text clipped - 18 lines]
>
> Thank you!!

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.