Thanks CajunCenturion for your input, saves me the time of changing the code. I am however having problems getting it to work as I would like. I have setup a timer to check every second to see if I am connected (I will change the period when I am finished testing). The code always detects that I am connected correctly, however it is not so clever detecting that I have disconnected. On a Lan for example if I pull out the network cable it does not detect that I am no longer connected. I have to go into Network settings and disable the LAN(Win2000). I also have the same problem when connecting Via a modem. When I disconnect it still assumes that I am connected. I have pasted the code that I have used below. Any assistence would be appreciated.
Module Code
----------------------------------
Option Explicit
Public onlinetime As Long
Public Declare Function InternetGetConnectedState Lib "wininet" _
(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
'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
Form Code
------------------------------------------
Private Sub Form_Load()
onlinetime = 0
End Sub
Private Sub Form_Unload(Cancel As Integer)
MsgBox "You were connected for " & onlinetime & " Seconds. Which Cost £" & txtCost.Text, vbOKOnly, "Online Time"
End Sub
Private Sub tmrCheckOffline_Timer()
If IsNetConnectViaModem = False Then
tmrCheckOnline.Enabled = True
tmrOnlineTime.Enabled = False
tmrCheckOffline.Enabled = False
MsgBox "You have been connected for " & onlinetime & " Seconds. Which Cost £" & txtCost.Text, vbOKOnly, "Online Time"
End If
End Sub
Private Sub tmrCheckOnline_Timer()
If IsNetConnectViaModem = True Then
tmrCheckOffline.Enabled = True
tmrOnlineTime.Enabled = True
tmrCheckOnline.Enabled = False
End If
End Sub
Private Sub tmrOnlineTime_Timer()
onlinetime = onlinetime + 1
txtOnlineTime = onlinetime
'If onlinetime >= 60 Then
txtCost = onlinetime * 0.016666 * 0.02 '/ 60
End Sub
Greg Palmer
----------------------------------------
Any feed back is appreciated.
And remember if it doesn't work just flush it down the toilet.
![[Flush] [Flush] [Flush]](/data/assets/smilies/Flush.gif)