Manipulate IP address

C

corky_guy

Hi -
I want to manipulate an IP address such that whatever is entered into
textbox1 is examined and then the last octet is increased by one.

for example: user inputs 1.2.3.4
code will output 1.2.3.5

I tried to do it, but I'm not sure how to examine the contents of the
textbox1 to know where to make the mathematical addition.


Can anyone provide the code for something like that?
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Hi -
I want to manipulate an IP address such that whatever is entered into
textbox1 is examined and then the last octet is increased by one.

for example: user inputs 1.2.3.4
code will output 1.2.3.5

I tried to do it, but I'm not sure how to examine the contents of the
textbox1 to know where to make the mathematical addition.


Can anyone provide the code for something like that?

First of all. if you rely on user input to actually type 4 series of digits
separated by 3 periods, you are asking for trouble because some users will
make mistakes.
Also, IP addresses cannot be made of numbers higher than 255.

If you insist on having user input in one single textbox, good luck because
without error trapping, the code will fail eventually, and the error
trapping will need to be quite complicated.

A much easier option is to use 4 separate textboxes, with periods on the
userform displayed with label controls.

This way, you need three basic error trapping:
1) Make sure that each textbox actually contains a number.
2) Make sure that each character in each textbox is actually a digit.
3) Check that the number they form is not higher than 255.

Here is some userform code that would achieve al that. Note that if the last
series is equal to 255, then the code does not increment it as numbers in IP
addresses cannot be higher than 255.
This code is based on a userform that has 4 textboxes named "txtIP_1" to
"txtIP_4" and a button control named "cmdOK".

'_______________________________________
Option Explicit
'_______________________________________
Private Sub cmdOK_Click()

Dim strFinal As String
Dim strIP_4 As String

With Me
If CLng(.txtIP_4) < 255 Then
strIP_4 = CStr(CLng(.txtIP_4) + 1)
Else
strIP_4 = .txtIP_4
End If
strFinal = .txtIP_1 & "." & .txtIP_2 & "." & .txtIP_3 & "." & strIP_4
MsgBox strFinal
End With

End Sub
'_______________________________________

'_______________________________________
Private Sub txtIP_1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Not CheckContent(Me.txtIP_1) Then Cancel = True

End Sub
'_______________________________________

'_______________________________________
Private Sub txtIP_2_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Not CheckContent(Me.txtIP_2) Then Cancel = True

End Sub
'_______________________________________

'_______________________________________
Private Sub txtIP_3_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Not CheckContent(Me.txtIP_3) Then Cancel = True

End Sub
'_______________________________________

'_______________________________________
Private Sub txtIP_4_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Not CheckContent(Me.txtIP_4) Then Cancel = True

End Sub
'_______________________________________

'_______________________________________
Function CheckContent(ctrlCheck As Control) As Boolean

Dim i As Long
Dim j As Long

CheckContent = True

With ctrlCheck
i = Len(.Text)
If i = 0 Then
CheckContent = False
MsgBox "You must enter a number in this control."
Exit Function
Else
j = 1
For j = 1 To i
If Not Mid(.Text, j, 1) Like "[0-9]" Then
CheckContent = False
MsgBox "You must use only digits from 0 to 9."
Exit For
End If
Next
If CheckContent Then
If CLng(.Text) > 255 Or CLng(.Text) = 0 Then
CheckContent = False
MsgBox "You must enter a number between 1 and 255."
End If
End If
If Not CheckContent Then
With ctrlCheck
.SelStart = 0
.SelLength = Len(.Text)
End With
End If
End If
End With

End Function
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

corky_guy

Hi Jean-Guy and thank you for your reply.

Can I skip the error checking and just use one "dumb" text box? Most
of the users are going to simply copy/paste from another form, so
mistakes (crossing my fingers) should be rare. Since the users are
going to paste, having one input box will be easier than four.

Thanks again
 
J

Jay Freedman

Hi -
I want to manipulate an IP address such that whatever is entered into
textbox1 is examined and then the last octet is increased by one.

for example: user inputs 1.2.3.4
code will output 1.2.3.5

I tried to do it, but I'm not sure how to examine the contents of the
textbox1 to know where to make the mathematical addition.


Can anyone provide the code for something like that?

Dim ip As String
Dim iparray As Variant
Dim i As Integer

ip = Textbox1.Text
iparray = Split(ip, ".")
If UBound(iparray) <> 3 Then
GoTo badVal
End If
For i = 0 To 3
If Not IsNumeric(iparray(i)) Then GoTo badVal
If (iparray(i) < 0) Or (255 < iparray(i)) _
Then GoTo badVal
Next

iparray(3) = CStr(CInt(iparray(3)) + 1)
If iparray(3) > 255 Then
MsgBox ip & " can't be incremented"
Exit Sub
End If

ip = Join(iparray, ".")
Textbox1.Text = ip

Exit Sub

badVal:
MsgBox ip & " is not valid"

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Hi Jean-Guy and thank you for your reply.

Can I skip the error checking and just use one "dumb" text box? Most
of the users are going to simply copy/paste from another form, so
mistakes (crossing my fingers) should be rare. Since the users are
going to paste, having one input box will be easier than four.

Personally, I would have error checking, even with only one textbox.

See Jay's message.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

corky_guy

Thank you Jay -- that's working. I just need to take the IP out of
the box and make it a variable (so I can use it elsewhere in my
code). Can you do that? Right now, the IP is only incrementing
inside of the textbox.

thank you again!
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Thank you Jay -- that's working. I just need to take the IP out of
the box and make it a variable (so I can use it elsewhere in my
code). Can you do that? Right now, the IP is only incrementing
inside of the textbox.

thank you again!

Jay has already provided the variable:

Textbox1.Text = ip

Here, as was declared at the top of the Sub (Dim ip As String), ip is a
String variable.


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

corky_guy

ip = Join(iparray, ".")
TextBox1.Text = ip

Exit Sub
With ActiveDocument
.Variables("input") = .TextBox1.Text
.Variables("result") = ip
End With


Hi, yeah, I see that the variable is there, but I can't get the
variable to paste back into the document in the docvariable "result".
The paste only takes place inside of the textbox1. I tried removing
the TextBox1.Text = IP and replacing it with the variable (see above),
but it's still not working.

To review: user inputs IP addy in textbox1, hits a button, and the
result is the IP addy + 1 and pasted back into the document into
variable "result".
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
ip = Join(iparray, ".")
TextBox1.Text = ip

Exit Sub
With ActiveDocument
.Variables("input") = .TextBox1.Text
.Variables("result") = ip
End With


Hi, yeah, I see that the variable is there, but I can't get the
variable to paste back into the document in the docvariable "result".
The paste only takes place inside of the textbox1. I tried removing
the TextBox1.Text = IP and replacing it with the variable (see above),
but it's still not working.

To review: user inputs IP addy in textbox1, hits a button, and the
result is the IP addy + 1 and pasted back into the document into
variable "result".

This means that the content of the textbox need not to be changed, right?

In the code you posted above, you will not get anything into the document
variables if you put that code after an Exit Sub statement.

So, here is the whole code, including the document variables code and
without actually changing the user input in the textbox:

'_______________________________________
Dim ip As String
Dim iparray As Variant
Dim i As Integer

ip = TextBox1.Text
iparray = Split(ip, ".")
If UBound(iparray) <> 3 Then
GoTo badVal
End If
For i = 0 To 3
If Not IsNumeric(iparray(i)) Then GoTo badVal
If (iparray(i) < 0) Or (255 < iparray(i)) _
Then GoTo badVal
Next

iparray(3) = CStr(CInt(iparray(3)) + 1)
If iparray(3) > 255 Then
MsgBox ip & " can't be incremented"
End If

ip = Join(iparray, ".")

With ActiveDocument
.Variables("input") = .TextBox1.Text
.Variables("result") = ip
End With

Exit Sub

badVal:
MsgBox ip & " is not valid"
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top