I want to find the MAC address of network adopter, I have a working code too but my problem is it only works if IP addres is enabled otherwise it won't get the address is there any solution to find MAC address even if IP Enabled is false if its possible update me with sample code

Here is my code to find MAC

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
Dim macme As String
Dim NewMac As String
macme = Itm.MACAddress
NewMac = Replace(macme, ":", "")
Text1.Text = NewMac
    Exit For
Next

Dani AI

Generated

Short answer: query the Win32_NetworkAdapter WMI class (not Win32_NetworkAdapterConfiguration) and test for non-null MAC values and adapter flags (physical, connected, adapter type) before calling Replace or using the value. Win32_NetworkAdapterConfiguration.IPEnabled only represents adapters with TCP/IP bound; that is why your original IPEnabled filter missed adapters when IP was disabled. (learn.microsoft.com)

Example (VB/VBScript-style WMI approach — avoids Replace on a Null and prefers a physical/connected Ethernet adapter first):

Dim wmi, col, nic, foundMac
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set col = wmi.ExecQuery("SELECT MACAddress, PhysicalAdapter, NetConnectionStatus, AdapterTypeID FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL")

foundMac = ""
For Each nic In col
  If Not IsNull(nic.PhysicalAdapter) And nic.PhysicalAdapter = True Then
    If Not IsNull(nic.NetConnectionStatus) And nic.NetConnectionStatus = 2 Then
      If Not IsNull(nic.AdapterTypeID) And nic.AdapterTypeID = 0 Then
        foundMac = nic.MACAddress
        Exit For
      ElseIf foundMac = "" Then
        foundMac = nic.MACAddress
      End If
    End If
  End If
Next

If foundMac = "" Then
  For Each nic In col
    If Not IsNull(nic.MACAddress) Then
      foundMac = nic.MACAddress
      Exit For
    End If
  Next
End If

If foundMac <> "" Then
  foundMac = Replace(foundMac, ":", "")
  Text1.Text = foundMac
End If

Notes and troubleshooting: the runtime error seen by after removing the WHERE is commonly caused by calling Replace on a Null MACAddress — always check IsNull (or Len/IsEmpty) first. Use PhysicalAdapter, NetConnectionStatus (2 = Connected) and AdapterTypeID (0 = Ethernet) to prefer LAN over wireless or virtual adapters; those property meanings are documented on the Win32_NetworkAdapter page. (learn.microsoft.com)

If WMI is unreliable in your environment (drivers, VMs, hidden/virtual adapters), consider the IP Helper API (GetAdaptersAddresses / GetAdaptersInfo) for a native enumeration that returns hardware addresses directly. (learn.microsoft.com)

As suggested, removing the WHERE is a quick test, but add the Null checks shown above. If needs the exact error text/line, include it when replying so the exact cause can be diagnosed.

Recommended Answers

All 3 Replies

Can't you just remove the WHERE condition?

I tried to remove it WHERE statement but getting errors but
Yes i can if find anyother working solution, i copied this code from internet as I am new to it
any other solution to grab the mac address of LAN card only if LAN is not available then check for wireless
the only thing I required is to grab single/one MAC address that works and keep constant if IP given or not given.

What error messages are you getting ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.