Get all IP Addresses of your machine

        17 votes: *****      33,997 views      5 comments
by Wayne Phillips, 18 May 2005    (Access 97+)

Here's some VBA code that you can use to determine the IP Addresses used by your network cards in your machine.

To run the code, insert a new module and copy/paste the code from below.  Now open the immediate window (press Ctrl + G) and type GetIPAddresses{ENTER} into the immediate window and it will output the IP Addresses.  Replace the line Debug.Print in the code with whatever you want (e.g. store it in a table or global variable).

Note: you may have more than one IP Address assigned to a machine and the localhost loopback 127.0.0.1 is also returned.  An optional parameter can be given to the function GetIPAddresses to filter the loopback address.

For example...

IP Address Function Example

Here's the code to copy & paste into a VBA module...


Option Explicit

' VBA MODULE: Get all IP Addresses of your machine
' (c) 2005 Wayne Phillips (http://www.everythingaccess.com)
' Written 18/05/2005
'
' REQUIREMENTS: Windows 98 or above, Access 97 and above
'
' Please read the full tutorial here:
' http://www.everythingaccess.com/tutorials.asp?ID=Get-all-IP-Addresses-of-your-machine
'
' Please leave the copyright notices in place.
' Thank you.
'

Option Compare Database

'A couple of API functions we need in order to query the IP addresses in this machine
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetIpAddrTable Lib "Iphlpapi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long

'The structures returned by the API call GetIpAddrTable...
Type IPINFO
    dwAddr As Long         ' IP address
    dwIndex As Long         ' interface index
    dwMask As Long         ' subnet mask
    dwBCastAddr As Long     ' broadcast address
    dwReasmSize As Long    ' assembly size
    Reserved1 As Integer
    Reserved2 As Integer
End Type

Public Function ConvertIPAddressToString(longAddr As Long) As String
    
    Dim IPBytes(3) As Byte
    Dim lngCount As Long
    
    'Converts a long IP Address to a string formatted 255.255.255.255
    'Note: Could use inet_ntoa instead

    
    CopyMemory IPBytes(0), longAddr, 4 ' IP Address is stored in four bytes (255.255.255.255)
    
    'Convert the 4 byte values to a formatted string
    While lngCount < 4
    
        ConvertIPAddressToString = ConvertIPAddressToString + _
                                    CStr(IPBytes(lngCount)) + _
                                    IIf(lngCount < 3, ".", "")

        lngCount = lngCount + 1
        
    Wend
    
End Function

Public Sub GetIPAddresses(Optional blnFilterLocalhost As Boolean = False)

    Dim Ret As Long, Tel As Long
    Dim bytBuffer() As Byte
    Dim IPTableRow As IPINFO
    Dim lngCount As Long
    Dim lngBufferRequired As Long
    Dim lngStructSize As Long
    Dim lngNumIPAddresses As Long
    Dim strIPAddress As String

On Error GoTo ErrorHandler:
    
    Call GetIpAddrTable(ByVal 0&, lngBufferRequired, 1)

    If lngBufferRequired > 0 Then
    
        ReDim bytBuffer(0 To lngBufferRequired - 1) As Byte
    
        If GetIpAddrTable(bytBuffer(0), lngBufferRequired, 1) = 0 Then
    
            'We've successfully obtained the IP Address details...
    
            'How big is each structure row?...
            lngStructSize = LenB(IPTableRow)
    
            'First 4 bytes is a long indicating the number of entries in the table
            CopyMemory lngNumIPAddresses, bytBuffer(0), 4
    
            While lngCount < lngNumIPAddresses
        
                'bytBuffer contains the IPINFO structures (after initial 4 byte long)
                CopyMemory IPTableRow, _
                            bytBuffer(4 + (lngCount * lngStructSize)), _
                            lngStructSize
                
                strIPAddress = ConvertIPAddressToString(IPTableRow.dwAddr)
                
                If Not ((strIPAddress = "127.0.0.1") _
                        And blnFilterLocalhost) Then
                        
                    'Replace this with whatever you want to do with the IP Address...
                    Debug.Print strIPAddress
                
                End If
            
                lngCount = lngCount + 1
            
            Wend
        
        End If
        
    End If

Exit Sub

ErrorHandler:
    MsgBox "An error has occured in GetIPAddresses():" & vbCrLf & vbCrLf & _
            Err.Description & " (" & CStr(Err.Number) & ")"

End Sub

*Microsoft Access is a trademark of Microsoft Corporation in the United States and other countries*

IMPORTANT: This document may not be reproduced in part or whole without prior consent from the author.

http://www.everythingaccess.com


<< Back to tutorials index





Rate this article:   Your rating: Poor Your rating: Not so good Your rating: Average Your rating: Good Your rating: Excellent

Have your say - comment on this article.

What did you think of 'Get all IP Addresses of your machine'?


1.

jaffer says...

07 May 2008

 
its great bcoz my system is on restriction cant find out the Ip address, this is kool stuff i will use it to best of my use

2.

Saurav says...

08 May 2008

 
Works great. I am using this as one of the parameters for logging user information when a shared workbook is accessed on the network.

3.

A&A - In Any Place says...

25 June 2008

 
Great!

I´m using this as MS Excel option for login users.

ANDRÉ BERNARDES
Santos - SP - Brasil

4.

Anton says...

18 January 2010

 
Great. I use this to check if user is connected via VPN or not (IP adress changes)

5.

Naren says...

11 November 2013

 
man, this works great! and fast too.. I'm using it on Windows 7 wit Microsoft Access 2013, but I wonder if it will work under Windows 8..

Thanks!

Have your say...

Name
E-mail (e-mail address will be kept private)
Comments
Verify Code
Verification Code
Please note: It may take 30 seconds after clicking submit - please be patient to avoid duplicate submissions.   All comments must be approved before being displayed on this web page.  This process may take up to 24 hours.


Site tools