Pad zeros in a field

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an IP address field in my table. There is already existing data.
What I would like to do is pad the existing data with zeroes so that for
example 10.1.104.3 would be 010.001.104.003. Is there a good way to do this
with Access or will I have to export the data and do something else? Also, I
want to add a mask to this field so that any new addresses are entered the
way that I want. What would be the best way to do this and will adding a
mask now affect the existing data? Thanks. Matt
 
Matt:
I am a long time programmer, so can help with your first problem. Write a
function which is called as needed (On_Open form on lose focus in IP Address
field) and in each put this line IPAddress = PadAddress(IPAddress), where
IPAddress if your field name.

Then write this function in a Module so it would be available for any Form
or report.

Public Function PadAddress(byval strIPAddress as String) as String
Dim nIPAddress(4) as Integer, strTemp as String, lngLocation as Long
Dim nCounter as Integer

lngLocation = InStr(strIPAddress, ".")
nIPAddress(0) = CInt(Left(strIPAddress, lngLocation))
strTemp = Right(strIpAddress, Len(strIPAddress)-lngLocation)
lngLocation = InStr(StrTemp, ".")
nIPAddress(1) = CInt(Left(strTemp, lngLocation))
strTemp = Right(strIpAddress, Len(strTemp)-lngLocation)
lngLocation = InStr(StrTemp, ".")
nIPAddress(2) = CInt(Left(strTemp, lngLocation))
strTemp = Right(strIpAddress, Len(strTemp)-lngLocation)
nIpAddress(3) = CInt(strTemp)
For nCounter = 0 to 3
strTemp = CStr(nIpAddress(nCounter))
if Len(strTemp) = 1 Then strTemp = "00" + strTemp
if Len(strTemp) = 2 Then strTemp = "0" + strTemp
if nCounter < 3 then strTemp = strTemp + "."
PadAddress = PadAddress + strTemp
Next
End Function

John H W
 
Matt:

Below is one possible approach to padding the IP address with zeros:

Public Function FormatIP() As String
Dim IP As String
Dim octets() As String

IP = "10.1.104.3"
octets = Split(IP, ".")

FormatIP = Format(octets(0), "000") & "." & Format(octets(1), "000") &
"." & Format(octets(2), "000") & "." & Format(octets(3), "000")

End Function

Adding a mask to the table will not affect the existing data.

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I have an IP address field in my table. There is already existing data.
What I would like to do is pad the existing data with zeroes so that for
example 10.1.104.3 would be 010.001.104.003. Is there a good way to do this
with Access or will I have to export the data and do something else? Also,
I
want to add a mask to this field so that any new addresses are entered the
way that I want. What would be the best way to do this and will adding a
mask now affect the existing data? Thanks. Matt
 
Back
Top