Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify the LAN IP Address? 2

Status
Not open for further replies.

TomCarnahan

Programmer
Dec 7, 2002
123
US
Is there a way to use VBA (+API's ???) to detect the LAN IP Address?

I have a portable Access application that gets transferred back and forth between 2 LANs. Some settings are different between the two. If I could ID the LAN IP address during start up, I could compensate for the differences.

Note: This application is for internal use on our company's multiple LANs.
 
here works for me
Code:
Public Declare Function GetAdaptersInfo Lib "IPHlpApi" (IpAdapterInfo As Any, pOutBufLen As Long) As Long

Public Const ERROR_BUFFER_OVERFLOW = 111

Public Type IP_ADDR_STRING
   tLng_NextIPAddr            As Long
   tStr_ThisIpAddress         As String * 16
   tStr_ThisIpMask            As String * 16
   tStr_ThisContext           As Long
End Type

Public Type IP_ADAPTER_INFO
   tLng_NextApapter           As Long
   tLng_ComboIndex            As Long
   tStr_AdapterName           As String * 260
   tStr_Description           As String * 132
   tLng_AddressLength         As Long
   tByt_PhysicalAddress(7)    As Byte
   tLng_Index                 As Long
   tLng_Type                  As Long
   tLng_DhcpEnabled           As Long
   tLng_CurrentIpAddress      As Long
   tIPA_IpAddressList         As IP_ADDR_STRING
   tIPA_GatewayList           As IP_ADDR_STRING
   tIPA_DhcpServer            As IP_ADDR_STRING
   tBol_HaveWins              As Boolean
   tIPA_PrimaryWinsServer     As IP_ADDR_STRING
   tIPA_SecondaryWinsServer   As IP_ADDR_STRING
   tLng_LeaseObtained         As Long
   tLng_LeaseExpires          As Long
End Type

Public Function GetNetworkInfo() As String

    Dim lLng_RetVal           As Long
    Dim lLng_AdapterInfoSize  As Long
    Dim lTyp_AdapterInfo      As IP_ADAPTER_INFO
    Dim temp As String
   lLng_AdapterInfoSize = 0
   lLng_RetVal = GetAdaptersInfo(ByVal 0&, lLng_AdapterInfoSize)
   If (lLng_RetVal <> ERROR_BUFFER_OVERFLOW) Then
      MsgBox "Unable to get 'GetAdaptersInfo' Size with Error:  " & lLng_RetVal
   Else
      lLng_RetVal = GetAdaptersInfo(lTyp_AdapterInfo, lLng_AdapterInfoSize)
      If (lLng_RetVal <> 0) Then
         MsgBox "GetAdaptersInfo Failed with Error:  " & lLng_RetVal
      Else
      
         temp = lTyp_AdapterInfo.tIPA_IpAddressList.tStr_ThisIpAddress
         GetNetworkInfo = TrimNull(temp)
         
      End If
   End If
   
End Function

Function TrimNull(str As String) As String
On Error GoTo TN_Err
    Dim I As Integer
    Dim index As Integer
    Dim char As String
    Dim char2 As String
    char = Right(str, 1)
    I = char
    index = 1
    char2 = Mid(str, Len(str) - index, 1)
    While char = char2
        index = index + 1
        char2 = Mid(str, Len(str) - index, 1)
    Wend
    
    TrimNull = Left(str, Len(str) - index)
    Exit Function
TN_Err:
    If Err = 13 Then
        Resume Next
    Else
        TrimNull = str
    End If
End Function
got this from another thread then modified it to take off the nulls at the end of the IP.

Brought to you By Nedaineum
 
You use something like this:
Public Function getMyIP()
Dim myWMI As Object, myobj As Object, itm
Set myWMI = GetObject("winmgmts:\\.\root\cimv2")
Set myobj = myWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each itm In myobj
getMyIP = itm.IPAddress(0)
Exit Function
Next
End Function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top