Results 1 to 9 of 9
  1. #1
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368

    Ping from Access

    Im trying to ping an IP thats in a textbox from MS-access.



    I have found some code to do so but when i start pinging the result is allways false while i know the address im trying to ping is reachable.

    Now im trying a new code wich youll find below, but the message im now getting is that there is a variable missing.

    Code:
    Private Sub knpPingIP_Click()
    
    Dim strCommand As String
    Dim strPing As String
    
    
    strCommand = "%ComSpec% /C %SystemRoot%\system32\ping.exe -n 1 -w 500 " & myIP & " | " & "%SystemRoot%\system32\find.exe /i " & Chr(34) & "TTL=" & Chr(34)
    strPing = fShellRun(strCommand)
    
    
    If strPing = "" Then
        MsgBox "Not Connected"
    Else
        MsgBox "Connected!"
    End If
    End Sub
    So i figured it wants me to input an IP where & myIP & is, so ive added this line :
    MyIP = me.ctrlIPadress

    referring to the textbox that holds the IP.
    Anyone got any clue ?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    And what happens after that code adjustment?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    It prompts me that there is a missing variable.
    There is offcourse a module to go with the code, but im thinking thats not the problem.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Where do you get the error?
    The code works for me. I put this in a new module.
    Click in the sub "Test_setIP", then press the F5 key.
    Code:
    Option Compare Database    'should be at the top of EVERY module
    Option Explicit   '<<=should be at the top of EVERY module
    
    Dim myIP As String   'global variable
    
    Sub Test_setIP()
        myIP = "65.254.231.128"  ' BOA.com
        Call knpPingIP_Click
    End Sub
    
    
    Private Sub knpPingIP_Click()
    
        Dim strCommand As String
        Dim strPing As String
    
        strCommand = "%ComSpec% /C %SystemRoot%\system32\ping.exe -n 1 -w 500 " & myIP & " | " & "%SystemRoot%\system32\find.exe /i " & Chr(34) & "TTL=" & Chr(34)
        strPing = fShellRun(strCommand)
    
        If strPing = "" Then
            MsgBox "Not Connected"
        Else
            MsgBox "Connected!"
        End If
    End Sub
    '''''''''''''''''''''''''''
    
    Function fShellRun(sCommandStringToExecute)
    
        ' This function will accept a string as a DOS command to execute.
        ' It will then execute the command in a shell, and capture the output into a file.
        ' That file is then read in and its contents are returned as the value the function returns.
    
        ' "myIP" is a user-selected global variable
    
        Dim oShellObject, oFileSystemObject, sShellRndTmpFile
        Dim oShellOutputFileToRead, iErr
    
        Set oShellObject = CreateObject("Wscript.Shell")
        Set oFileSystemObject = CreateObject("Scripting.FileSystemObject")
    
        sShellRndTmpFile = oShellObject.ExpandEnvironmentStrings("%temp%") & oFileSystemObject.GetTempName
        On Error Resume Next
        oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True
        iErr = Err.Number
    
        On Error GoTo 0
        If iErr <> 0 Then
            fShellRun = ""
            Exit Function
        End If
    
        On Error GoTo err_skip
        fShellRun = oFileSystemObject.OpenTextFile(sShellRndTmpFile, 1).ReadAll
        oFileSystemObject.DeleteFile sShellRndTmpFile, True
    
        Exit Function
    
    err_skip:
        fShellRun = ""
        oFileSystemObject.DeleteFile sShellRndTmpFile, True
    
    End Function

  5. #5
    dugroup is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    2
    This is great guys I've been looking for something like this! Thank You!

    I do have one thing that I want to do with this. Instead of the MsgBox I want to make a greencircle.jpg visible if the address is connected and redcircle.jpg if it is not connected. How can I do this?

    Thanks for your help.

  6. #6
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Hi Ssanfu, your remarks worked out. Im not sure if it was the option explicit or the dimensioning of "MyIp".

    Thanks alot !
    Ill leave this thread unsolved as there is an additional question (wich i know the answer to, just not enough time to explain, sorry)

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,631
    Set up two Image controls on form and code something like:

    Me.imgNotConnected.Visible = (strPing = "")
    Me.imgConnected.Visible = (strPing <> "")

    or one Image control and set the ControlSource property:

    If strPing = "" Then
    Me.imgConnection.ControlSource = "path to external image file for redcircle.jpg"
    Else
    Me.imgConnection.ControlSource = "path to external image file for greencircle.jpg"
    End If

    However, be aware neither will work on form in Continuous or Datasheet view. In that situation, use a textbox and Conditional Formatting to control the background color.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    You can re-ping at form timer so you can see if the connection is lost. I do the same trick to see how many users are logged in to my application

  9. #9
    dugroup is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    2
    June7
    Thank you so much that was just what I was looking for!

    Thanks everyone

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums