Code Samples for Businesses, Schools & Developers

First Published 2 Oct 2022           Last Updated 9 Oct 2022


The code below provides a simple test to check whether an Access app is connected to the internet

Copy the code to a standard module

Option Compare Database
Option Explicit

'###################################
'API declaration
#If VBA7 Then 'A2010 or later (32/64-bit)
      Private Declare PtrSafe Function InternetGetConnectedState Lib "wininet.dll" _
            (ByRef dwFlags As LongPtr, ByVal dwReserved As Long) As Boolean
#Else 'A2007 or earlier
      Private Declare Function InternetGetConnectedState Lib "wininet.dll" _
            (ByRef dwFlags As Long, ByVal dwReserved As Long) As Boolean
#End If
'###################################

Public Function GetInternetConnectedState() As Boolean

On Error GoTo Err_Handler

      GetInternetConnectedState = InternetGetConnectedState(0&, 0&)
      Debug.Print GetInternetConnectedState

Exit_Handler:
      Exit Function

Err_Handler:
      MsgBox "Error " & Err.number & " in GetInternetConnectedState procedure : " & vbNewLine & Err.description, vbOKOnly + vbCritical
      Resume Exit_Handler

End Function



The GetInternetConnectedState function returns True if connected to the internet; otherwise False



I have used the above code successfully for about 15 years without any issues
However, I should mention that using the InternetGetConnectedState API function is no longer recommended.

See the official Microsoft documentation at https://learn.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-internetgetconnectedstate
Microsoft suggest the INetworkListManager::GetConnectivity method should be used instead.



Colin Riddington           Mendip Data Systems                 Last Updated 9 Oct 2022



Return to Code Samples Page




Return to Top