View Full Version : Test for connection

07-03-2007, 05:15 PM
for example:

this would be perfect ... if I could get it to work/translate it


Public Function IsConnectionAvailable() As Boolean
' Returns True if connection is available
' Replace www.yoursite.com (http://www.yoursite.com) with a site that
' is guaranteed to be online - perhaps your
' corporate site, or microsoft.com
Dim objUrl As New System.Uri("www.yoursite.com/ (http://www.yoursite.com/)")
' Setup WebRequest
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objResp As System.Net.WebResponse
' Attempt to get response and return True
objResp = objWebReq.GetResponse
objWebReq = Nothing
Return True
Catch ex As Exception
' Error, exit and return False
objWebReq = Nothing
Return False
End Try

hxxp://www.developerfusion.co.uk/show/3903/ (http://www.developerfusion.co.uk/show/3903/)
I have seen the code to test for AOL, RAS, Proxy
as per:

hxxp://www.vbaexpress.com/kb/getarticle.php?kb_id=313#instr (http://www.vbaexpress.com/kb/getarticle.php?kb_id=313#instr) &

hxxp://vbnet.mvps.org/code/network/internetgetconnectedstate.htm (http://vbnet.mvps.org/code/network/internetgetconnectedstate.htm)
but not for VPN, unless I am misunderstanding something om those pages.

my code looks currently looks like this...

Sub TestConn()
' Tests Connestion status (in Module 2)
' notifies user of live status / or hides dB options

Dim OnOffline As Integer

If IsConnected = True Then
MsgBox "Connection verified"
lblConnLabel.BackColor = vbGreen
lblConnLabel.Caption = "Online"
Frame2.Visible = True
CmdLocalSave.Visible = False
OnOffline = "1"
MsgBox "Connection Not Verifed: Local Save Options Enabled"
lblConnLabel.BackColor = vbRed
lblConnLabel.Caption = "Offline"
Frame2.Visible = False
CmdLocalSave.Visible = True
OnOffline = "0"
End If

End Sub

which hits the module:

Option Explicit

Public Declare Function InternetGetConnectedState _
Lib "wininet.dll" (lpdwFlags As Long, _
ByVal dwReserved As Long) As Boolean

Function IsConnected() As Boolean
Dim Stat As Long
IsConnected = (InternetGetConnectedState(Stat, 0&) <> 0)
End Function

but that only test to see if the machine has a live internet connection which gives me a false negative as that wont necessarily connect to the dB.

I have been trying to error trap a fail on the dB connection and that works, but, its a very slow test.

' MsgBox Err.Description
If Err.Number = -2147467259 Or Err.Number = -3704 Then
MsgBox "Its alive!!!!! not!"
MsgBox "Error # " & Err & " : " & Error(Err)
Resume Exit_connOpen
End If
End Sub

Any ideas gratefully recieved, tia.

Im using
word 2003