Internet Connection

W

Web_Builder

I need help! I looked at a prior posting on how to determine if you are
connected to the internet and I couldn't get it to work.
Could someone please help me with the coding to determine if a computer is
connected so that i can do something if it is and put up a msgbox if it isn't?
I would greatly appreciate any help!
Thanks
 
T

Tom Ogilvy

You might want to post the exact code you are using which you say does not
work. Also, when you say does not work, does it raise an error or does it
just not give the proper answer - is it consistently giving one answer or
the other regardless of the actual situation? Which answer?
 
W

Web_Builder

I'm starting from scratch here. I got myself so confused I don't know what i
would even keep. I hate to admit it but i am way over my head working with
this code. But I have to get it done. I couldn't even get i to check for the
connection before it gave me an error. I changed the code so many times
trying to get it to work that i just deleted it.
 
T

Tom Ogilvy

I pasted this into a standard module (insert=>module in the VBE) and ran
Test. It worked for me:

This is code from Randy Birch's site which was posted by Harald Staff to
this newsgroup:


--------------
A rather lengthy solution -from Randy Birch, page
http://www.mvps.org/vbnet/code/network/internetgetconnectedstate.htm
Run Sub Test to see how it works.
HTH. Best wishes Harald


'Top of standard module********************************
Option Explicit

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

'Local system uses a modem to connect to the Internet.
Public Const INTERNET_CONNECTION_MODEM As Long = &H1

'Local system uses a LAN to connect to the Internet.
Public Const INTERNET_CONNECTION_LAN As Long = &H2

'Local system uses a proxy server to connect to the Internet.
Public Const INTERNET_CONNECTION_PROXY As Long = &H4

'No longer used.
Public Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8

Public Const INTERNET_RAS_INSTALLED As Long = &H10
Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40


Sub Test()
MsgBox GetNetConnectString
End Sub

'InternetGetConnectedState wrapper functions
Public Function IsNetConnectViaLAN() As Boolean

Dim dwflags As Long

'pass an empty variable into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)

'return True if the flags indicate a LAN connection
IsNetConnectViaLAN = dwflags And INTERNET_CONNECTION_LAN

End Function


Public Function IsNetConnectViaModem() As Boolean

Dim dwflags As Long

'pass an empty variable into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)

'return True if the flags indicate a modem connection
IsNetConnectViaModem = dwflags And INTERNET_CONNECTION_MODEM

End Function


Public Function IsNetConnectViaProxy() As Boolean

Dim dwflags As Long

'pass an empty variable into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)

'return True if the flags indicate a proxy connection
IsNetConnectViaProxy = dwflags And INTERNET_CONNECTION_PROXY

End Function


Public Function IsNetConnectOnline() As Boolean

'no flags needed here - the API returns True
'if there is a connection of any type
IsNetConnectOnline = InternetGetConnectedState(0&, 0&)

End Function


Public Function IsNetRASInstalled() As Boolean

Dim dwflags As Long

'pass an empty variable into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)

'return True if the flags include RAS installed
IsNetRASInstalled = dwflags And INTERNET_RAS_INSTALLED

End Function


Public Function GetNetConnectString() As String

Dim dwflags As Long
Dim msg As String

'build a string for display
If InternetGetConnectedState(dwflags, 0&) Then

If dwflags And INTERNET_CONNECTION_CONFIGURED Then
msg = msg & "You have a network connection configured." & vbCrLf
End If

If dwflags And INTERNET_CONNECTION_LAN Then
msg = msg & "The local system connects to the Internet via a LAN"
End If

If dwflags And INTERNET_CONNECTION_PROXY Then
msg = msg & ", and uses a proxy server. "
Else: msg = msg & "."
End If

If dwflags And INTERNET_CONNECTION_MODEM Then
msg = msg & "The local system uses a modem" & _
"to connect to the Internet. "
End If

If dwflags And INTERNET_CONNECTION_OFFLINE Then
msg = msg & "The connection is currently offline. "
End If

If dwflags And INTERNET_CONNECTION_MODEM_BUSY Then
msg = msg & "The local system's modem is" & _
"busy with a non-Internet connection. "
End If

If dwflags And INTERNET_RAS_INSTALLED Then
msg = msg & "Remote Access Services are installed on this system."
End If

Else

msg = "Not connected to the internet now."

End If

GetNetConnectString = msg

End Function
 
Top