In MS Access I am trying to sort IPs by their 3rd and 4th octet, and then have them sorted out as [IpAddress] and [HostName] in their respective column. The third octet will be linked to a server rack. Eg 1,22, or 121 and the 4th octet will be linked to its location in the rack. Eg s1, s22, or s121. So the IP will be sorted from a list and a query to build the HostName off of an IP. How could/should I go about doing this. I am not very experienced with MS access. I have tried useing an iif statement, but I am unable to get the desired results. I would like for the HostName to follow this nomenclature: 121s1. Thanks for all the help.
SELECT IIf(query1.Ipaddress Like '*.*.3.#', "1003s'#'", 22) AS HostName
FROM Query1;
This is the line that I am trying to use. However, it only will produce an output with the quotes around 1003a'#'. If I do not have the "" around it it will return "Syntax error (missing operator) in query expression IIf(query1.Ipaddress Like ' . .3.#', 1003a'#', 22). With the quotes it puts exactly what is in the quotes (1003a'#'). 22 is just a place holder so I can an answer back in access. Maybe this will help with what I am asking for.
As @WEI_DBA suggests in a comment to the question, consider changing the way you store the IP addresses. Instead of a single string
IpAddress
----------
10.0.121.1
10.0.12.34
you could store them as four separate integers
IpOctet1 IpOctet2 IpOctet3 IpOctet4
-------- -------- -------- --------
10 0 121 1
10 0 12 34
Then you could simply derive the HostName as
IpOctet3 & "s" & IpOctet4
and if you wanted the old representation for display purposes you could just use
IpOctet1 & "." & IpOctet2 & "." & IpOctet3 & "." & IpOctet4
After adding the 4 new Integer
columns to your table you could populate them using VBA code like this:
Option Compare Database
Option Explicit
Sub SplitIpAddresses()
Dim cdb As DAO.Database
Set cdb = CurrentDb
Dim rst As DAO.Recordset
Set rst = cdb.OpenRecordset("SELECT * FROM Table1 WHERE IpAddress IS NOT NULL", dbOpenDynaset)
Do While Not rst.EOF
Dim octets As Variant
octets = Split(rst!IpAddress, ".")
rst.Edit
rst!IpOctet1 = octets(0)
rst!IpOctet2 = octets(1)
rst!IpOctet3 = octets(2)
rst!IpOctet4 = octets(3)
rst.Update
rst.MoveNext
Loop
rst.Close
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.