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).
Step 2. Call module from a sub, below is call by a click action
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