Check whether network connection is active

Larry Haydn

Board Regular
Joined
Jul 18, 2019
Messages
207
Office Version
  1. 365
Platform
  1. Windows
How do I check whether a network connection is active?

I could check whether a mapped network drive exists, using this:

VBA Code:
Dim HaveDrive As Boolean
HaveDrive = False
HaveDrive = CreateObject("Scripting.FileSystemObject").DriveExists("P")
If HaveDrive Then ....

The problem is, if the network crashed, the mapped drive still "exist", even though
visually it has a big red X on the drive letter.

-------------------------------------------------------------
Why do I need this?
I am using this check to determine whether the program should attempt a connection with SQL Server.
This means that if the network is active, I will establish a connection to SQL Server and save data there.
On the other hand, if the network is inactive, then save the data locally.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Have a look here to see if this helps
It has an IsReady property for a drive using vba fso


Use is shown in the System.IO class for .NET example
 
Upvote 0
This is an old question and my solution is not elegant, but I put it here nonetheless.

VBA Code:

Sub test_connection()

Dim x As String
Dim Server_IP_adress As String

Server_IP_adress = "8.8.8.8"
x = Trim(ShellRun("ping " & Server_IP_adress))

If InStr(x, "Reply from " & Server_IP_adress & ":") > 0 Then
Debug.Print "Server_IP_adress: " & Server_IP_adress & " respond to ping"
Debug.Print InStr(x, "Reply from " & Server_IP_adress & ":")
Else
Debug.Print "No Network"
End If
End Sub
VBA Code:
 
Upvote 0
Have a look here to see if this helps
It has an IsReady property for a drive using vba fso


Use is shown in the System.IO class for .NET example
Thank you daverunt!
It works!
Even though it has a 1~1.5sec delay, this is still much better that the 5~8 sec delay when trying to connect to SQL Server when the network is down.
 
Upvote 0
This is an old question and my solution is not elegant, but I put it here nonetheless.

VBA Code:

Sub test_connection()

Dim x As String
Dim Server_IP_adress As String

Server_IP_adress = "8.8.8.8"
x = Trim(ShellRun("ping " & Server_IP_adress))

If InStr(x, "Reply from " & Server_IP_adress & ":") > 0 Then
Debug.Print "Server_IP_adress: " & Server_IP_adress & " respond to ping"
Debug.Print InStr(x, "Reply from " & Server_IP_adress & ":")
Else
Debug.Print "No Network"
End If
End Sub
VBA Code:
Thank you for the reply!
I used the IsReady method as suggested by daverunt.
 
Upvote 0
Glad it works for you. I could only test it with local drives and there's no delay but a network drive delay isn't unexpected.
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,194
Members
448,951
Latest member
jennlynn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top