VBA code to check internet connection in Windows OS 64 bit, VBA MS Access 2019 (1 Viewer)

nhtuan

New member
Local time
Today, 16:32
Joined
Dec 23, 2010
Messages
24
Sharing code saves time, each year I love more and more MS Access and its VBA, the environment that has more than 30 years of operations, highly stable and highly available, flexible, comprehensible and highly productive in this connected world.

In our days, internet connection solves numerous things, and enable our unlimited capacity.
I would like to share for someone who needs. I'm not professional programmer but have passion and curiosity to dig dipper.
The following code is tested in Windows 11 64 bit with MS Access 2019 and a Wireless connection, not yet tested with LAN cable, suppose it might work as well.

In brief, there are two steps.
Step 1. Create a standard module with following codes (copy and past), name it whatever you like (ex. MyModuleChkInternet).

Code:
' Code begins from this line
Option Compare Database


Option Explicit


 Public Flg As LongPtr
 Public Declare PtrSafe Function InternetState _
            Lib "Wininet.dll" (lpdwFlags As LongPtr, _
            ByVal dwReserved As Long) As Boolean

Private Const INTERNET_CONNECTION_MODEM As Long = &H1
Private Const INTERNET_CONNECTION_LAN As Long = &H2
Private Const INTERNET_CONNECTION_PROXY As Long = &H4
Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Private Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8
Private Const INTERNET_RAS_INSTALLED As Long = &H10
Private Const INTERNET_CONNECTION_CONFIGURED As Long = &H40
Function IsInternetOk() As Boolean
    Dim INTNET As Long


    INTNET = InternetState(Flg, 0&)


    If Flg >= INTERNET_CONNECTION_OFFLINE Then
        Debug.Print "INTERNET_CONNECTION_OFFLINE"
    End If

If CBool(INTNET) Then
        IsInternetOk = True
    Else
        IsInternetOk = False
    End If
End Function

' Save your module for later use

Step 2. Call module from a sub, below is call by a click action

Code:
        ' To call the function
        Private Sub MyButton_Click()
        If IsInternetOk Then
          MsgBox "Connected to the Internet."
          'do some thing else
        Else
           MsgBox "Not connected to the Internet."
           'do some thing
        End If
        End sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:32
Joined
May 7, 2009
Messages
19,169
error when run on x64 office:
error.png
 

oleronesoftwares

Passionate Learner
Local time
Today, 16:32
Joined
Sep 22, 2014
Messages
1,159
Sharing code saves time, each year I love more and more MS Access and its VBA, the environment that has more than 30 years of operations, highly stable and highly available, flexible, comprehensible and highly productive in this connected world.

In our days, internet connection solves numerous things, and enable our unlimited capacity.
I would like to share for someone who needs. I'm not professional programmer but have passion and curiosity to dig dipper.
The following code is tested in Windows 11 64 bit with MS Access 2019 and a Wireless connection, not yet tested with LAN cable, suppose it might work as well.

In brief, there are two steps.
Step 1. Create a standard module with following codes (copy and past), name it whatever you like (ex. MyModuleChkInternet).
error when run on x64 office:
hi @nhtuan error was triggered when the code was run by @arnelgp on x64 office, do look into it. Thank you
 

nhtuan

New member
Local time
Today, 16:32
Joined
Dec 23, 2010
Messages
24
@oleronesoftwares the revised code is here, please let me know how your result.
Step 1. Create a standard module with following codes (copy and past), name it whatever you like (ex. MyModuleChkInternet).
Code:
Option Compare Database

Option Explicit


 Public Flg As LongPtr
 Public Declare PtrSafe Function InternetGetConnectedState _
            Lib "Wininet.dll" (lpdwFlags As LongPtr, _
            ByVal dwReserved As Long) As Boolean

Private Const INTERNET_CONNECTION_MODEM As Long = &H1
Private Const INTERNET_CONNECTION_LAN As Long = &H2
Private Const INTERNET_CONNECTION_PROXY As Long = &H4
Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Private Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8
Private Const INTERNET_RAS_INSTALLED As Long = &H10
Private Const INTERNET_CONNECTION_CONFIGURED As Long = &H40
Function IsInternetOk() As Boolean
    Dim INTNET As Long


    INTNET = InternetGetConnectedState(Flg, 0&)


    If Flg >= INTERNET_CONNECTION_OFFLINE Then
        Debug.Print "INTERNET_CONNECTION_OFFLINE"
    End If

If CBool(INTNET) Then
        IsInternetOk = True
    Else
        IsInternetOk = False
    End If
End Function

' Save your module for later use

Step 2. Call module from a sub, below is call by a click action
Code:
Private Sub Command1_Click()
        If IsInternetOk Then
          MsgBox "Connected to the Internet."
          'do some thing else
        Else
           MsgBox "Not connected to the Internet."
           'do some thing
        End If
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:32
Joined
May 7, 2009
Messages
19,169
it does work now!:D
 

Jason Lee Hayes

Active member
Local time
Today, 23:32
Joined
Jul 25, 2020
Messages
174
Unfortunately you cannot rely on "Wininet.dll" to identify a connection

Connect to your WIFI; run the code you will get IsInternetOk = True..
Disconnect from WIFI, run the code and you will still get IsInternetOK = True..

Reference: https://docs.microsoft.com/en-gb/windows/win32/cimwin32prov/win32-networkadapter?redirectedfrom=MSDN

therefore:-

Set oObject = GetObject("WINMGMTS:\\.\ROOT\cimv2")
Set adapter = oObject.InstancesOf("Win32_NetworkAdapter")

You need to pull the following:-

string NetConnectionID;
uint16 NetConnectionStatus;

then

If item.NetConnectionID <> "null" And item.NetConnectionStatus = 2

This will work reliably...

If you need to identify whether LAN/WIFI etc then look at the Win32_NetworkAdapter class properties and qualifiers
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 23:32
Joined
Jan 14, 2017
Messages
18,186
@InView
Sorry to disagree but the code in post #5 does correctly detect whether or not there is an internet connection both when WiFi is on and when it is off.
In the latter case, the function gives False (assuming there is no Ethernet connection)

The problem you may be referring to is detecting whether Wifi is in use when a workstation has both Ethernet and Wifi connections
 

561414

Active member
Local time
Today, 17:32
Joined
May 28, 2021
Messages
280
I have found reliable to just ping google in order to check if there's internet connection. I mean, it would be a disaster worldwide if google went down.
 

isladogs

MVP / VIP
Local time
Today, 23:32
Joined
Jan 14, 2017
Messages
18,186
I have found reliable to just ping google in order to check if there's internet connection. I mean, it would be a disaster worldwide if google went down.
:)

FWIW, here is my own very simple code to check for an internet connection which I have used successfully for over a decade.
However, MS recommend using INetworkListManager::GetConnectivity instead (not tested)

Code:
Option Compare Database
Option Explicit

#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
       GetInternetConnectedState = InternetGetConnectedState(0&, 0&)
End Function
 
Last edited:

sonic8

AWF VIP
Local time
Tomorrow, 00:32
Joined
Oct 27, 2015
Messages
998
I have found reliable to just ping google in order to check if there's internet connection. I mean, it would be a disaster worldwide if google went down.
People using this approach with Facebook were in for an unpleasant surprise when Facebook went offline in last October.

In many cases it would be the best approach to just try to connect to the resource you want to connect to, instead of testing general internet connectivity.
 

561414

Active member
Local time
Today, 17:32
Joined
May 28, 2021
Messages
280
People using this approach with Facebook were in for an unpleasant surprise when Facebook went offline in last October.

In many cases it would be the best approach to just try to connect to the resource you want to connect to, instead of testing general internet connectivity.
Facebook goes offline every day, Google is more reliable (it has been to me, anyway) and I would not generalize, being able to ping Google but not --the resource you want-- would inform you of an issue with --the resource you want--. And I would totally recommend that approach considering the amount of times Windows has told me I have internet connectivity and it's not true.
 

Jason Lee Hayes

Active member
Local time
Today, 23:32
Joined
Jul 25, 2020
Messages
174
@InView
Sorry to disagree but the code in post #5 does correctly detect whether or not there is an internet connection both when WiFi is on and when it is off.
In the latter case, the function gives False (assuming there is no Ethernet connection)

The problem you may be referring to is detecting whether Wifi is in use when a workstation has both Ethernet and Wifi connections
It may work for you but does not for me. I did test on my usuall MacBook Pro using Bootcamp though and even though it should work ive come across a couple of issues including webcam capture that i never got to work either without using VCL as a work round. I will test on my PC later today. Ive no doubt if you say it works it will... Likely a bootcamp envoronment issue...
 

Users who are viewing this thread

Top Bottom