ping button in an Access database

Posted on 1998-07-08
Last Modified: 2013-11-13
I need to create a button in my Access database that will ping the IP of the selected client field.
Question by:Lozz
1 Comment

Accepted Solution

Geffl earned 100 total points
ID: 1465210
Hi Lozz!

Paste this code in a module and try the function 'Ping'. The hostname can be the computername or IP-Adress.
The function returns true if ping was successfull.

Public RespondingHost As String
Public TraceRT As Boolean
Public TTL As Integer
Public Const WS_VERSION_MAJOR = &H101 \ &H100 And &HFF&
Public Const WS_VERSION_MINOR = &H101 And &HFF&
Public Const MIN_SOCKETS_REQD = 0
Public Function Ping(Host$) As Boolean

    vbWSAStartup                ' Initialize Winsock
    If Not vbGetHostByName(Host$) Then
        Exit Function
    End If

    vbIcmpCreateFile            ' Get ICMP Handle
    pIPo.TTL = 255

    Ping = vbIcmpSendEcho       ' Send the ICMP Echo Request

    vbIcmpCloseHandle           ' Close the ICMP Handle

    vbWSACleanup                ' Close Winsock

End Function
Sub vbWSAStartup()
    iReturn = WSAStartup(&H101, WSAdata)

    If iReturn <> 0 Then    ' If WSock32 error, then tell me about it
        MsgBox "WSock32.dll is not responding!", vbOKOnly, "VB4032-ICMPEcho"
    End If

    If LoByte(WSAdata.wVersion) < WS_VERSION_MAJOR Or (LoByte(WSAdata.wVersion) = WS_VERSION_MAJOR And HiByte(WSAdata.wVersion) < WS_VERSION_MINOR) Then
        sHighByte = Trim$(Str$(HiByte(WSAdata.wVersion)))
        sLowByte = Trim$(Str$(LoByte(WSAdata.wVersion)))
        sMsg = "WinSock Version " & sLowByte & "." & sHighByte
        sMsg = sMsg & " is not supported "
        MsgBox sMsg, vbOKOnly, "VB4032-ICMPEcho"
    End If

    If WSAdata.iMaxSockets < MIN_SOCKETS_REQD Then
        sMsg = "This application requires a minimum of "
        sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
        MsgBox sMsg, vbOKOnly, "VB4032-ICMPEcho"
    End If
    MaxSockets = WSAdata.iMaxSockets
    If MaxSockets < 0 Then
        MaxSockets = 65536 + MaxSockets
    End If

    MaxUDP = WSAdata.iMaxUdpDg
    If MaxUDP < 0 Then
        MaxUDP = 65536 + MaxUDP
    End If
    Description = ""
        If WSAdata.szDescription(i) = 0 Then Exit For
        Description = Description + Chr$(WSAdata.szDescription(i))
    Next i
    Status = ""
    For i = 0 To WSASYS_STATUS_LEN
        If WSAdata.szSystemStatus(i) = 0 Then Exit For
        Status = Status + Chr$(WSAdata.szSystemStatus(i))
    Next i

End Sub
Private Function vbGetHostByName(ByVal HostName$) As Boolean

    Dim szString As String

    Host = HostName$    ' Set Variable Host to Value in Text1.text

    szString = String(64, &H0)
    Host = Host + Right$(szString, 64 - Len(Host))

    If gethostbyname(Host) = SOCKET_ERROR Then          ' If WSock32 error, then tell me about it
        Exit Function
        PointerToPointer = gethostbyname(Host)          ' Get the pointer to the address of the winsock hostent structure
        CopyMemory Hostent.h_name, ByVal _
        PointerToPointer, Len(Hostent)                  ' Copy Winsock structure to the VisualBasic structure

        ListAddress = Hostent.h_addr_list               ' Get the ListAddress of the Address List
        CopyMemory ListAddr, ByVal ListAddress, 4       ' Copy Winsock structure to the VisualBasic structure
        CopyMemory IPLong, ByVal ListAddr, 4            ' Get the first list entry from the Address List
        CopyMemory Addr, ByVal ListAddr, 4

        vbGetHostByName = True

    End If

End Function
Sub vbGetHostName()
    Host = String(64, &H0)      ' Set Host value to a bunch of spaces
    If gethostname(Host, HostLen) = SOCKET_ERROR Then   ' This routine is where we get the host's name
        sMsg = "WSock32 Error" & Str$(WSAGetLastError())    ' If WSOCK32 error, then tell me about it
        MsgBox sMsg, vbOKOnly, "VB4032-ICMPEcho"
        Host = Left$(Trim$(Host), Len(Trim$(Host)) - 1)     ' Trim up the results
        Text1.Text = Host                                   ' Display the host's name in label1
    End If

End Sub
Private Function vbIcmpSendEcho() As Boolean

    szBuffer = "abcdefghijklmnopqrstuvwabcdefghijklmnopqrstuvwabcdefghijklmnopqrstuvwabcdefghijklmnopqrstuvwabcdefghijklmnopqrstuvwabcdefghijklm"
    szBuffer = Left$(szBuffer, 32)
    bReturn = IcmpSendEcho(hIP, Addr, szBuffer, Len(szBuffer), pIPo, pIPe, Len(pIPe) + 8, 2700)

    If bReturn Then
        vbIcmpSendEcho = (pIPe.Status = 0)
    End If

End Function
Function HiByte(ByVal wParam As Integer)

    HiByte = wParam \ &H100 And &HFF&

End Function
Function LoByte(ByVal wParam As Integer)

    LoByte = wParam And &HFF&

End Function
Sub vbWSACleanup()
    iReturn = WSACleanup()

    If iReturn <> 0 Then    ' If WSock32 error, then tell me about it.
        sMsg = "WSock32 Error - " & Trim$(Str$(iReturn)) & " occurred in Cleanup"
        MsgBox sMsg, vbOKOnly, "VB4032-ICMPEcho"
    End If

End Sub
Sub vbIcmpCloseHandle()

    bReturn = IcmpCloseHandle(hIP)

    If bReturn = False Then
        MsgBox "ICMP Closed with Error", vbOKOnly, "VB4032-ICMPEcho"
    End If

End Sub
Sub vbIcmpCreateFile()

    hIP = IcmpCreateFile()

End Sub


Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How To Loop - Python 19 86
VBA filters 2 59
How to Add / Edit Windows Menu 4 60
IF ELSE Statement in Excel Macro VBA 16 62
If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article will show, step by step, how to integrate R code into a R Sweave document
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question