(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