By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

Testing Internet connection before performing database connections

P: n/a
Hello,

Is there a way to test that an internet connection exists and that you are
able to see the webserver before performing any connections? The reason
being, when I try to connect to an SQL database it takes sooo long to time
out if the connection isn't available. If a user is on a dial up
connection, I'd like to be able to determine that quickly, then ask the user
if they would like to connect for current information otherwise rely on the
local (and possibly outdated) resources.

Any help would be appreciated.

Thanks!
Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Jozef wrote:
Hello,

Is there a way to test that an internet connection exists and that you are
able to see the webserver before performing any connections? The reason
being, when I try to connect to an SQL database it takes sooo long to time
out if the connection isn't available. If a user is on a dial up
connection, I'd like to be able to determine that quickly, then ask the user
if they would like to connect for current information otherwise rely on the
local (and possibly outdated) resources.

Any help would be appreciated.

Thanks!

I don't know if the function at
http://www.mvps.org/access/api/api0067.htm will help. I ran this IP
(www.pcpitstop.com) and it returned the following

? fGetHostName("64.29.201.21")
port-64-1952021-da120383www002.devices.datareturn.com

Determining the network connection (is the user using dial-up or
broadband)...I suppose there's an API somewhere.
Nov 13 '05 #2

P: n/a
This is ancient code. It may or may not do not do what you need to be
done; you could test it and see.

Private Declare Function InternetOpen Lib "wininet.dll" Alias
"InternetOpenA" _
(ByVal strAgent As String, ByVal lngAccessType As Long, _
ByVal strProxyName As String, ByVal strProxyBypass As String, _
ByVal lngFlags As Long) As Long

Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer

Private Const INTERNET_OPEN_TYPE_DIRECT As Long = 1

Public Function CanConnect() As Boolean
Dim mHInternet As Long
mHInternet = InternetOpen("Microsoft Access",
INTERNET_OPEN_TYPE_DIRECT, _
vbNullString, vbNullString, 0)
If mHInternet = 0 Then
MsgBox "Could not open Internet Connection", vbCritical,
"Whatever Caption you Want"
Else
CanConnect = True
InternetCloseHandle mHInternet
End If
End Function

Sub test()
MsgBox CanConnect
End Sub

Nov 13 '05 #3

P: n/a
Salad <oi*@vinegar.com> wrote in
news:NU*****************@newsread3.news.pas.earthl ink.net:
Jozef wrote:
Hello,

Is there a way to test that an internet connection exists and
that you are able to see the webserver before performing any
connections? The reason being, when I try to connect to an SQL
database it takes sooo long to time out if the connection isn't
available. If a user is on a dial up connection, I'd like to be
able to determine that quickly, then ask the user if they would
like to connect for current information otherwise rely on the
local (and possibly outdated) resources.

Any help would be appreciated.

Thanks!

I don't know if the function at
http://www.mvps.org/access/api/api0067.htm will help. I ran this
IP (www.pcpitstop.com) and it returned the following

? fGetHostName("64.29.201.21")
port-64-1952021-da120383www002.devices.datareturn.com

Determining the network connection (is the user using dial-up or
broadband)...I suppose there's an API somewhere.


While that is interesting and useful code, it won't tell you if
you're connected to the Internet. All it tells you is if there is
DNS information to answer the question, which could be cached
locally, or could be hard-wired in a HOSTS file.

Try this:

Go to the System32 directory, browse to \Drivers\Etc and open HOSTS.
Add an entry like this:

192.168.1.123 BogusMachine

Then try your code for both the IP address and the machine name. It
will resolve even if you're not connected to the Internet.

Now, find a machine on the Internet and enter it's IP address and
name in HOSTS. Disconnect from the Internet and run the code. You'll
see that it will return results.

So, this code is not a good proxy for answering the question "am I
connected to the Internet?"

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #4

P: n/a
"ly******@yahoo.ca" <ly******@yahoo.ca> wrote in
news:11**********************@g43g2000cwa.googlegr oups.com:
This is ancient code. It may or may not do not do what you need to
be done; you could test it and see.

Private Declare Function InternetOpen Lib "wininet.dll" Alias
"InternetOpenA" _
(ByVal strAgent As String, ByVal lngAccessType As Long, _
ByVal strProxyName As String, ByVal strProxyBypass As
String, _ ByVal lngFlags As Long) As Long

Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer

Private Const INTERNET_OPEN_TYPE_DIRECT As Long = 1

Public Function CanConnect() As Boolean
Dim mHInternet As Long
mHInternet = InternetOpen("Microsoft Access",
INTERNET_OPEN_TYPE_DIRECT, _
vbNullString, vbNullString, 0)
If mHInternet = 0 Then
MsgBox "Could not open Internet Connection", vbCritical,
"Whatever Caption you Want"
Else
CanConnect = True
InternetCloseHandle mHInternet
End If
End Function

Sub test()
MsgBox CanConnect
End Sub


I'm not sure I understand what this code does. Does it test that you
have an Internet connection, or only that you've got a TCP/IP
network connection?

I also wonder what's actually doing the testing, as a firewall
(hardware or software) could prevent the test from succeeding even
when the Internet were accessible via explicitly allowed methods.

In the end, I don't think any of the two suggestions is a proper way
to approach the problem, which is not really "am I connected to the
Internet?" but "can I connect to a particular machine?"

That can really only be done by initiating some kind of connection
to the remote machine or by pinging it to see if it's available. I
don't know what the best method for doing that from Access would be,
but it seems to me that you want to do a test that answers the
actual question that is the problem you're trying to address.

And, heaven forfend, Lyle, but you of all people shouldn't be
posting old rusty code that's several years old, don't you think?
You keep telling all of us that about our own tried-and-true methods
for coding in Access.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #5

P: n/a
ly******@yahoo.ca wrote:
This is ancient code. It may or may not do not do what you need to be
done; you could test it and see.

Private Declare Function InternetOpen Lib "wininet.dll" Alias
"InternetOpenA" _
(ByVal strAgent As String, ByVal lngAccessType As Long, _
ByVal strProxyName As String, ByVal strProxyBypass As String, _
ByVal lngFlags As Long) As Long

Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer

Private Const INTERNET_OPEN_TYPE_DIRECT As Long = 1

Public Function CanConnect() As Boolean
Dim mHInternet As Long
mHInternet = InternetOpen("Microsoft Access",
INTERNET_OPEN_TYPE_DIRECT, _
vbNullString, vbNullString, 0)
If mHInternet = 0 Then
MsgBox "Could not open Internet Connection", vbCritical,
"Whatever Caption you Want"
Else
CanConnect = True
InternetCloseHandle mHInternet
End If
End Function

Sub test()
MsgBox CanConnect
End Sub


An addition to Lyle's comments.

The use of the INTERNET_OPEN_TYPE_DIRECT option will work with an "unrestricted" Internet
connection. Using the INTERNET_OPEN_TYPE_PRECONFIG is probably a better choice since it
will try to determine proxy info from the registry.

There may also be installations where an authenticating proxy is used and the use of the
INTERNET_OPEN_TYPE_PROXY is required with values set by the InternetSetOption() call for
these constants:

Public Const INTERNET_OPTION_PROXY_USERNAME = 43
Public Const INTERNET_OPTION_PROXY_PASSWORD = 44

Google's your friend.

--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #6

P: n/a

You'll need to _ask_ the user if it's ok. You can ask them if they
never want to see the message again, but ask first. If nothing else,
it's the nice thing to do.

Then you can ping the server. On some machines this will automatically
cause the dial up to appear, on others not, but in either case if you
get back a vaild ping, you'll know they are connected.

Jozef wrote:
Is there a way to test that an internet connection exists and that you are
able to see the webserver before performing any connections? The reason
being, when I try to connect to an SQL database it takes sooo long to time
out if the connection isn't available. If a user is on a dial up
connection, I'd like to be able to determine that quickly, then ask the user
if they would like to connect for current information otherwise rely on the
local (and possibly outdated) resources.
Any help would be appreciated.


Nov 13 '05 #7

P: n/a
"Chuck Grimsby" <c.*******@worldnet.att.net> wrote in
news:11*********************@g49g2000cwa.googlegro ups.com:
You'll need to _ask_ the user if it's ok. You can ask them if
they never want to see the message again, but ask first. If
nothing else, it's the nice thing to do.

Then you can ping the server. On some machines this will
automatically cause the dial up to appear, on others not, but in
either case if you get back a vaild ping, you'll know they are
connected.


And, of course, even after that, the connection attempt could still
time out if there's something wrong on the SQL Server's end of
things.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #8

P: n/a
David W. Fenton wrote:
Salad <oi*@vinegar.com> wrote in
news:NU*****************@newsread3.news.pas.earthl ink.net:

Jozef wrote:
Hello,

Is there a way to test that an internet connection exists and
that you are able to see the webserver before performing any
connections? The reason being, when I try to connect to an SQL
database it takes sooo long to time out if the connection isn't
available. If a user is on a dial up connection, I'd like to be
able to determine that quickly, then ask the user if they would
like to connect for current information otherwise rely on the
local (and possibly outdated) resources.

Any help would be appreciated.

Thanks!


I don't know if the function at
http://www.mvps.org/access/api/api0067.htm will help. I ran this
IP (www.pcpitstop.com) and it returned the following

? fGetHostName("64.29.201.21")
port-64-1952021-da120383www002.devices.datareturn.com

Determining the network connection (is the user using dial-up or
broadband)...I suppose there's an API somewhere.

While that is interesting and useful code, it won't tell you if
you're connected to the Internet. All it tells you is if there is
DNS information to answer the question, which could be cached
locally, or could be hard-wired in a HOSTS file.

Try this:

Go to the System32 directory, browse to \Drivers\Etc and open HOSTS.
Add an entry like this:

192.168.1.123 BogusMachine

Then try your code for both the IP address and the machine name. It
will resolve even if you're not connected to the Internet.

Now, find a machine on the Internet and enter it's IP address and
name in HOSTS. Disconnect from the Internet and run the code. You'll
see that it will return results.

So, this code is not a good proxy for answering the question "am I
connected to the Internet?"

I was unsure whether or not the code would be of use. I'm glad others
are had some input.

In one of my applications, I check for the existence of the classname of
the email application. If it is not open, I don't allow the sending of
the email unless it's open.
Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.