Solved

ping button in an Access database

Posted on 1998-07-08
1
221 Views
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.
0
Comment
Question by:Lozz
1 Comment
 
LVL 1

Accepted Solution

by:
Geffl earned 100 total points
Comment Utility
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
        WSACleanup
        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
    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
    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 = ""
   
    For i = 0 To WSADESCRIPTION_LEN
        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
    Else
        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"
    Else
        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)
    DoEvents
    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
    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


0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now