Verify a network connection

johnkrytus

Registered User.
Local time
Today, 13:54
Joined
Mar 7, 2013
Messages
91
I have a picture stored on a network file share that populates each time a particular form is opened. Every so often we lose our connection (which is a different issue altogether). Right now the error I get after much grinding away, is "Bad file name or number"

Is there a quicker way to check for the connection before it spends 45 seconds trying to find the whole path?

Code:
    Dim vFolderPath As String, dirFile As String, strFile As String
    
    vFolderPath = Nz(DLookup("FolderName", "tblCodes-FolderControl", "FolderKey = '" & "Profile" & "'"))
    
    dirFile = vFolderPath & Dir(vFolderPath & ctrl_people_id & " *", vbDirectory)
    strFile = dirFile & "\profile_pic.*"
    
    'Debug.Print dirFile
    On Error Resume Next
    
    If Dir(strFile) <> vbNullString Then
        Me.[ctrl_ImageFrame].Picture = dirFile & "\" & Dir(strFile)
    Else
        Me!ctrl_ImageFrame.Picture = "X:\~stuff\profile_icon.png"
    End If
 
I would insert a line
strFile = Dir("NetworkFile")
where NetworkFile is the name of a file you know exists on the connection, and test if strFile is not a zero length string.
 
That article gives a solution that is probably exactly what the doctor ordered. For what I need I can simplify their example quite a bit. I need help however. I barely can fake my way through VBA let alone script. Can you help me convert this code into good VBA?

Code:
    Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
            sHost & "/root/cimv2").ExecQuery("SELECT * FROM Win32_PingStatus " & _
            "WHERE Address = '" + sTarget + "'")
 
Last edited:
Code:
Function SystemOnline(ByVal ComputerName As String) As Boolean
 
' HostName can be a computer name or IP address.
 
Dim colPingResults As Variant
Dim oPingResult As Variant
Dim strQuery As String
    strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & ComputerName & "'"
 
    Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
    For Each oPingResult In colPingResults
 
        If oPingResult Is Nothing Then
            SystemOnline = False
        ElseIf oPingResult.StatusCode = 0 Then
            SystemOnline = True
        Else
            SystemOnline = False
        End If
 
    Next
 
End Function
 

Users who are viewing this thread

Back
Top Bottom