Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining if Network is active

Status
Not open for further replies.

billheath

Technical User
Mar 17, 2000
299
US
I have designed a multiuser database but the network is slow and sown quite often. Therefore, I am putting each individuals data in a separate file on their hard drive so they can work on it when the network is down. Once the network is operating, the datafiles get joined back on the network drive.
I need the code to determine if the network is up. If it is, the user will go to the network data. If not, he or she can still get to their data.
I tried:

On Error GoTo errorhandler
If Dir("R:\Asset GB1.mdb") = "Asset GB1.mdb" Then
DoCmd.OpenForm "Passwords"
DoCmd.Close acForm, "frm Startup", acSaveNo
End If
Exit Sub

errorhandler:
MsgBox "You are not connected to 'R' drive. You can only work on the data located on this computer. Press OK to continue; Cancel to close Database.", vbOKCancel
DoCmd.OpenForm "Select Area"

"R" is the network drives assigned letter. The code works if the user is in the database before the network goes down. However, if the network is already down, I get a "Network or drive problem" error.

Is there a better way to check the network status and not get this message. The code above is called from the startup form's on open event.
Thanks
 
Will hang up as is. Try pinging your computer on a periodic basis.

Steve King

Option Compare Database
Option Explicit

' Ping a Computer
' Another sample found on the net somewhere....
'

'//
'// Structures
'//

Private Type ICMP_OPTIONS
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type


Private Type ICMP_ECHO_REPLY
Address As Long
status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
DataPointer As Long
Options As ICMP_OPTIONS
Data As String * 250
End Type


Private Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLen As Integer
hAddrList As Long
End Type


Private Const MAX_WSADescription = 256
Private Const MAX_WSASYSStatus = 128

Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To MAX_WSADescription) As Byte
szSystemStatus(0 To MAX_WSASYSStatus) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type


'//
'// Win32s
'//

Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long
Private Declare Function IcmpSendEcho Lib "icmp.dll" (ByVal IcmpHandle As Long, ByVal DestinationAddress As Long, ByVal RequestData As String, ByVal RequestSize As Integer, ByVal RequestOptions As Long, ReplyBuffer As ICMP_ECHO_REPLY, ByVal ReplySize As Long, ByVal Timeout As Long) As Long
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal szHost As String, dwHostLen As Long) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal szHost As String) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

'//
'// Constants
'//

Private Const IP_STATUS_BASE = 11000
Private Const IP_SUCCESS = 0
Private Const IP_BUF_TOO_SMALL = (11000 + 1)
Private Const IP_DEST_NET_UNREACHABLE = (11000 + 2)
Private Const IP_DEST_HOST_UNREACHABLE = (11000 + 3)
Private Const IP_DEST_PROT_UNREACHABLE = (11000 + 4)
Private Const IP_DEST_PORT_UNREACHABLE = (11000 + 5)
Private Const IP_NO_RESOURCES = (11000 + 6)
Private Const IP_BAD_OPTION = (11000 + 7)
Private Const IP_HW_ERROR = (11000 + 8)
Private Const IP_PACKET_TOO_BIG = (11000 + 9)
Private Const IP_REQ_TIMED_OUT = (11000 + 10)
Private Const IP_BAD_REQ = (11000 + 11)
Private Const IP_BAD_ROUTE = (11000 + 12)
Private Const IP_TTL_EXPIRED_TRANSIT = (11000 + 13)
Private Const IP_TTL_EXPIRED_REASSEM = (11000 + 14)
Private Const IP_PARAM_PROBLEM = (11000 + 15)
Private Const IP_SOURCE_QUENCH = (11000 + 16)
Private Const IP_OPTION_TOO_BIG = (11000 + 17)
Private Const IP_BAD_DESTINATION = (11000 + 18)
Private Const IP_ADDR_DELETED = (11000 + 19)
Private Const IP_SPEC_MTU_CHANGE = (11000 + 20)
Private Const IP_MTU_CHANGE = (11000 + 21)
Private Const IP_UNLOAD = (11000 + 22)
Private Const IP_ADDR_ADDED = (11000 + 23)
Private Const IP_GENERAL_FAILURE = (11000 + 50)
Private Const MAX_IP_STATUS = 11000 + 50
Private Const IP_PENDING = (11000 + 255)
Private Const PING_TIMEOUT = 200
Private Const WS_VERSION_REQD = &H101
Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD = 1
Private Const SOCKET_ERROR = -1
Public Function GetIPAddress() As String

Dim szHost As String * 256
Dim lpHost As Long
Dim HOST As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim i As Integer
Dim szIPAddr As String

If Not SocketsInitialize() Then
GetIPAddress = ""
Exit Function
End If

If gethostname(szHost, 256) = SOCKET_ERROR Then
GetIPAddress = ""
MsgBox "Windows Sockets error " & str(WSAGetLastError()) & " has occurred. Unable to successfully get Host Name.", vbExclamation
SocketsCleanup
Exit Function
End If

szHost = Trim$(szHost)
lpHost = gethostbyname(szHost)

If lpHost = 0 Then
GetIPAddress = ""
MsgBox "Windows Sockets are not responding. Unable to successfully get Host Name.", vbExclamation
SocketsCleanup
Exit Function
End If

RtlMoveMemory HOST, lpHost, Len(HOST)
RtlMoveMemory dwIPAddr, HOST.hAddrList, 4

ReDim tmpIPAddr(1 To HOST.hLen)
RtlMoveMemory tmpIPAddr(1), dwIPAddr, HOST.hLen

For i = 1 To HOST.hLen
szIPAddr = szIPAddr & tmpIPAddr(i) & "."
Next

GetIPAddress = Mid$(szIPAddr, 1, Len(szIPAddr) - 1)
SocketsCleanup

End Function
Public Function GetIPHostName() As String

Dim szHost As String * 256

If Not SocketsInitialize() Then
GetIPHostName = ""
Exit Function
End If

If gethostname(szHost, 256) = SOCKET_ERROR Then
GetIPHostName = ""
MsgBox "Windows Sockets error " & str(WSAGetLastError()) & " has occurred. Unable to successfully get Host Name.", vbExclamation
SocketsCleanup
Exit Function
End If

'// GetIPHostName = Trim$(szHost)
GetIPHostName = Left$(szHost, InStr(szHost, Chr(0)) - 1)
SocketsCleanup

End Function


Private Function HiByte(ByVal wParam As Integer)

HiByte = wParam \ &H100 And &HFF&

End Function

Private Function LoByte(ByVal wParam As Integer)

LoByte = wParam And &HFF&

End Function
Public Function Ping(szAddress As String) As Long

Dim hPort As Long, dwAddress As Long, szMessage As String
Dim x As Long
Dim ECHO As ICMP_ECHO_REPLY

szMessage = "Echo This."
dwAddress = AddressStringToLong(szAddress)

hPort = IcmpCreateFile()
x = IcmpSendEcho(hPort, dwAddress, szMessage, Len(szMessage), 0, ECHO, Len(ECHO), PING_TIMEOUT)
If x = 0 Then
Ping = ECHO.status * -1
Else
' the ping succeeded,
' .Status will be 0
' .RoundTripTime is the time in ms for the ping to complete, .Data is the data returned (NULL terminated)
' .Address is the Ip address that actually replied
' .DataSize is the size of the string in .Data
Ping = ECHO.RoundTripTime
Debug.Print ECHO.Address
Debug.Print ECHO.Data
End If

x = IcmpCloseHandle(hPort)

End Function

Function AddressStringToLong(szAddress As String) As Long

Dim szTmp As String, i As Integer, Octets(1 To 4) As String

szTmp = szAddress
i = 0

While InStr(szTmp, ".") > 0
i = i + 1
Octets(i) = Mid(szTmp, 1, InStr(szTmp, ".") - 1)
szTmp = Mid(szTmp, InStr(szTmp, ".") + 1)
Wend

i = i + 1
Octets(i) = szTmp

If i <> 4 Then
AddressStringToLong = 0
Exit Function
End If

AddressStringToLong = Val(&quot;&H&quot; & Right(&quot;00&quot; & Hex(Octets(4)), 2) & Right(&quot;00&quot; & Hex(Octets(3)), 2) & Right(&quot;00&quot; & Hex(Octets(2)), 2) & Right(&quot;00&quot; & Hex(Octets(1)), 2))

End Function
Public Function SocketsCleanup() As Boolean

Dim x As Long

x = WSACleanup()

If x <> 0 Then
MsgBox &quot;Windows Sockets error &quot; & Trim$(str$(x)) & &quot; occurred in Cleanup.&quot;, vbExclamation
SocketsCleanup = False
Else
SocketsCleanup = True
End If


End Function

Public Function SocketsInitialize() As Boolean

Dim WSAD As WSADATA
Dim x As Integer
Dim szLoByte As String, szHiByte As String, szBuf As String

x = WSAStartup(WS_VERSION_REQD, WSAD)

If x <> 0 Then
MsgBox &quot;Windows Sockets for 32 bit Windows environments is not successfully responding.&quot;, vbExclamation
SocketsInitialize = False
Exit Function
End If

If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
szHiByte = Trim$(str$(HiByte(WSAD.wVersion)))
szLoByte = Trim$(str$(LoByte(WSAD.wVersion)))
szBuf = &quot;Windows Sockets Version &quot; & szLoByte & &quot;.&quot; & szHiByte
szBuf = szBuf & &quot; is not supported by Windows Sockets for 32 bit Windows environments.&quot;
MsgBox szBuf, vbExclamation
SocketsInitialize = False
Exit Function
End If

If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
szBuf = &quot;This application requires a minimum of &quot; & Trim$(str$(MIN_SOCKETS_REQD)) & &quot; supported sockets.&quot;
MsgBox szBuf, vbExclamation
SocketsInitialize = False
Exit Function
End If

SocketsInitialize = True

End Function

Public Function DetermineIP(szTmp As String) As Boolean

On Error GoTo Handler

Dim i As Integer
Dim nOctet As Integer



While InStr(szTmp, &quot;.&quot;) > 0
i = i + 1
nOctet = Mid(szTmp, 1, InStr(szTmp, &quot;.&quot;) - 1)
szTmp = Mid(szTmp, InStr(szTmp, &quot;.&quot;) + 1)
Wend
If (i <> 3) Then
DetermineIP = False
Else
DetermineIP = True
End If
Exit Function
Handler:
DetermineIP = False
End Function Growth follows a healthy professional curiosity
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top