Hi To All,

How can the Network LAN Connection to be Enabled/Disabled
through Vb Code

Recommended Answers

All 4 Replies

This code actually "toggles" the connection, by retrieving it's current state, and then setting the opposite state..... but, in vb6 you'll need to add a reference to "Microsoft Shell Controls and Automation" before it will work for you, plus, you may need to change some variables to fit the name of your connection....

Dim objCP, objEnable, objDisable, colNetwork
Dim clsConn, clsLANConn, clsVerb
Dim strNetConn, strConn, strEnable, strDisable
Dim bEnabled, bDisabled

strNetConn = "Network Connections"
strConn = "Local Area Connection"

strEnable = "En&able"
strDisable = "Disa&ble"

Set objshell = CreateObject("Shell.Application")

If objshell Is Nothing Then
    MsgBox "Fooey"
    Exit Sub
End If


Set objCP = objshell.NameSpace(3) 'Control Panel

Set colNetwork = Nothing
For Each clsConn In objCP.Items
    If clsConn.Name = strNetConn Then
        Set colNetwork = clsConn.GetFolder
        Exit For
    End If
Next

If colNetwork Is Nothing Then
    MsgBox "Network folder not found"
    Exit Sub
End If

Set clsLANConn = Nothing
For Each clsConn In colNetwork.Items
    If InStr(LCase(clsConn.Name), LCase(strConn)) Then
        Set clsLANConn = clsConn
        Exit For
    End If
Next

If clsLANConn Is Nothing Then
    MsgBox "Network Connection not found"
    Exit Sub
End If

bEnabled = True
Set objEnable = Nothing
Set objDisable = Nothing

For Each clsVerb In clsLANConn.Verbs
    If clsVerb.Name = strEnable Then
        Set objEnable = clsVerb
        bEnabled = False
    End If
    If clsVerb.Name = strDisable Then
        Set objDisable = clsVerb
    End If
Next

If bEnabled Then
    objDisable.DoIt
Else
    objEnable.DoIt
End If

Thanks For your help Mr.Comatose.

how can know the lan cable has been connected or not in vb6

@rrajm, you need to open your OWN thread, we will then answer from there. This is a VERY old post(2006), nobody will give you help from here. Click HERE to open a new thread.:)

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.