For a very long time, a best practice has been to always use Access over a LAN connection and to never use it over a WAN or wireless connection because of the possibility of corruption. You can read more on the subject by reviewing the article entitled Access Back-End Location – WAN, Online Server, OneDrive, DropBox, ….
Now, sadly, it is often not enough to just advise end-users to always use a LAN connection. They forget and simply won’t think about it (not out of malice). As such, you can find numerous discussions asking “how can you block your database from opening if the user is using a wireless connection?” or something to that effect.
Well, I too had been looking to enforce this type of thing in one of my databases where wireless connection had already broken the database a couple times already. Below is what I came up with.
'---------------------------------------------------------------------------------------
' Procedure : IsConnectedbyWireless
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Determine whether or not the PC is currectly connected using a wireless
' connection
' True -> has an active wireless connection
' False -> does not have an active wireless connection
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' None
'
' Usage:
' ~~~~~~
' ? IsConnectedbyWireless
'
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2017-01-29 Initial Release
' 2 2017-07-11 Added the use of the GetWirelessNames()
' 3 2018-09-20 Updated Copyright
'---------------------------------------------------------------------------------------
Public Function IsConnectedbyWireless() As Boolean
'Ref: https://msdn.microsoft.com/en-us/library/aa394216%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
Dim oWMI As Object
Dim oWirelessAdapters As Object
Dim WirelessNames As Collection
Dim i As Long
On Error GoTo Error_Handler
Set WirelessNames = GetWirelessNames()
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
For i = 1 To WirelessNames.Count
Set oWirelessAdapters = oWMI.ExecQuery("SELECT *" & vbCrLf & _
" FROM Win32_NetworkAdapter" & vbCrLf & _
" WHERE NetconnectionID = '" & WirelessNames(i) & "'" & _
" AND (NetConnectionStatus=1 OR NetConnectionStatus=2)" & _
" AND PhysicalAdapter='True'")
If oWirelessAdapters.Count <> 0 Then IsConnectedbyWireless = True
Next i
Error_Handler_Exit:
On Error Resume Next
Set WirelessNames = Nothing
Set oWirelessAdapters = Nothing
Set oWMI = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: IsConnectedbyWireless" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
'---------------------------------------------------------------------------------------
' Procedure : GetWirelessNames
' Original Author : Mohammed Alyafae, 9/12/2011
' Source : https://gallery.technet.microsoft.com/scriptcenter/Disable-wireless-f3bcf66f#content
' Modified by : Daniel Pineault, CARDA Consultants Inc.
' The original version only returned the first wireless adapter, this
' script returns a script of all the wireless adapters.
' Website : http://www.cardaconsultants.com
' Purpose : Build a collection of wireless network connections
' *** Is used by IsConnectedbyWireless
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' None
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2017-07-11 Initial Release
' 2 2018-09-20 Updated Copyright
'---------------------------------------------------------------------------------------
Function GetWirelessNames() As Collection
Dim strKeyPath As String
Dim strComputer As String
Dim objReg As Object
Dim arrSubKeys
Dim SubKey
Dim strValueName As String
Dim dwValue
Dim strValue As String
Const HKLM = &H80000002
Dim WirelessNames As Collection
Set WirelessNames = New Collection
strKeyPath = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objReg.Enumkey HKLM, strKeyPath, arrSubKeys
For Each SubKey In arrSubKeys
strValueName = "MediaSubType"
objReg.GetDWORDValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, dwValue
If dwValue = 2 Then
strValueName = "Name"
objReg.GetStringValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, strValue
WirelessNames.Add strValue
' Exit For
End If
Next
Set GetWirelessNames = WirelessNames
Set WirelessNames = Nothing
Set objReg = Nothing
End Function
Then I simply added code similar to
If IsConnectedbyWireless = True Then
Application.Quit
End If
to check at the startup of the database and close it immediately if an active wireless connection was detected.
It was a great challenge running jet/ace backend in a wireless LAN but with remote desktop/remote app technology , I did push access to 50 users in OLTP without any problem. On WAN Access is still usable using VPN remote desktop like gBridge or hamachi. My only hope in the near future is a true Webified Access.
One can always dream, but I highly doubt that is in the cards. We’ve been begging to webified Access to keep up with the competition for years (probably decades now). Out of it all, MS gave use Web Databases and killed them, then gave us Web Apps and killed them, with great prodigious, as well.
I think to properly webified Access would require a complete rewrite of the application and I don’t think MS will ever make such an investment. Heck, we can’t even get bugs fixed and let us not forget that Access is black sheep of the Office family continuously ignored, under developed, under publicized, …
Ah …, if only!
This is an interesting and useful piece of code, but it does not test whether the wifi connection is actually being used to access the data.
I am sitting here at my computer which uses wifi to connect to the web etc, but the Access dbs I work on are all located on my C drive. The IsConnectedbyWireless function returns true.
So for this to be useful for me, I would need to add another test to see whether I was using the db at the time, or perhaps if the location of the datafile was local.
Thanks John, I will ponder this issue and see what can be done to improve the code.
Thank you very much for the code.
Win32_NetworkAdapter class is deprecated and Microsoft refer to MSFT_NetAdapter class.
It is also stated (https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapter) that Win32_NetworkAdapter “only supplies IPV4 data”.
I’m not sure if that is relevant for testing if a wireless connection exists (is it ?)
And as for deprecation – deprecated functions are kept alive for backward compatibility, so it doesn’t seem a problem.
What is your take on that ?