Link to home
Start Free TrialLog in
Avatar of Paul McCabe
Paul McCabe

asked on

MS Access: Internet connectivity check using VBA

Hello Experts,

I am trying to compile some simple VBA code to check internet connectivity of the PC before the execution of a subroutine that sends emails via remote SMTP server. I have found that Access freezes if it tries to send emails when not connected to the internet, hence the desire for a connectivity pre-check. The idea is that if there is no internet connection then Access does not attempt to send the emails. For testing purposes I am trying to develop the connectivity check code behind a command button click.
I have found a few articles that deal with checking internet connectivity in VBA, but nothing that I can get to work. I am trying to adapt the following code (from: https://bytes.com/topic/access/answers/944317-test-internet-connection-within-access), which -if I can only get it to work- would seem to be the simplest solution:

Private Sub NetworkConnection_Click()
Public Declare Function InternetGetConnectedState Lib "wininet.dll" ()
If InternetGetConnectedState(0&, 0&) Then
MsgBox "Connected to internet"
Else
MsgBox "Not Connected to internet"
End If
End Sub

When this is run, I get an error saying that "only comments can be written after End Sub, End Function or End Property". If anyone could help out on this I would be most grateful ! I am using MS Access 2013 / Windows 7 professional 64-bit.
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also ... add this at the beginning of your module if you select to use my solution.

Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef dwflags As Long, ByVal dwReserved As Long) As Long

ET
Avatar of Paul McCabe
Paul McCabe

ASKER

Thank you all for your answers. I am not using a module, so in this case, where should the following statement be placed ?

Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef dwflags As Long, ByVal dwReserved As Long) As LongOption

I tried placing it under the "Compare Database" line at the top of the code, but unfortunately that didn't do the trick.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it ! Works perfectly. Thanks a lot guys.
Thanks for the points ... Glad I could help.

ET