Link to home
Start Free TrialLog in
Avatar of HKFuey
HKFueyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Access VBA to get IP4 address

I have a function that gets IP4 and IP6 addresses, does anyone know how to just get IP4 address? (e.g. 192.9.1.1)
Function GetIPAddress()
    Const strComputer As String = "."   ' Computer name. Dot means local computer
    Dim objWMIService, IPConfigSet, IPConfig, IPAddress, i
    Dim strIPAddress As String

    ' Connect to the WMI service
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    ' Get all TCP/IP-enabled network adapters
    Set IPConfigSet = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

    ' Get all IP addresses associated with these adapters
    For Each IPConfig In IPConfigSet
        IPAddress = IPConfig.IPAddress
        If Not IsNull(IPAddress) Then
            strIPAddress = strIPAddress & Join(IPAddress, ", ")
        End If
    Next

    GetIPAddress = strIPAddress
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HKFuey

ASKER

Fist post works perfectly, the other post are really useful too. Thanks very much
You are welcome!

/gustav
Whenever possible, I try to let the query filter the data as much as possible.  In this case, I'll check for a not null condition.

Next, I use the regexp object to parse the IPAddress.
Function GetIPAddress()
    Const strComputer As String = "."   ' Computer name. Dot means local computer
    Dim objWMIService, IPConfigSet, IPConfig, IPAddress, i
    Dim strIPAddress As String
    Dim oRE As Object
    Dim oMatches As Object

    Set oRE = CreateObject("vbscript.regexp")
    oRE.Pattern = "(\d+.\d+.\d+.\d+)"

    ' Connect to the WMI service
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    ' Get all TCP/IP-enabled network adapters
    Set IPConfigSet = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE and IPAddress Is Not Null")

    ' Get all IP addresses associated with these adapters
    For Each IPConfig In IPConfigSet
        IPAddress = IPConfig.IPAddress
        If oRE.test(IPAddress) Then
            Set oMatches = oRE.Execute(IPAddress)
            strIPAddress = strIPAddress & ", " & oMatches(0)
        end if
    Next

    GetIPAddress = Mid(strIPAddress,3)
End Function

Open in new window

IPAddress is not a field in the query, so your code doesn't run (here).

/gustav
I see.  IPAddress is a string array field type.  Since it is not an atomic data type, the normal rules for querying (WQL) do not apply.  Thanks, Gustav.

In addition to the above parsing methods, does anyone know if the (0) item in the IPAddress array will always point to the IPV4 address or if it might be elsewhere in the array.  If it will always be in the zero position, then we can add
IPConfig.IPAddress(0)

Open in new window

as a parsing method.
I wouldn't expect that position to be random.

/gustav
I wouldn't expect that position to be random.
If there is only an IPV6 address, should we see an empty zero position string?
Good question. You better check that out.

/gustav
You better check that out.
Unfortunately, I only have IPV4 in my environment and no way to mock up an IPV6-only environment to test.
Same here.

/gustav