Network detection using VB6

VB6 application development with XP OS running on a WAN using Active Directory.

Is it possible to create code in an application that is built using VB6 to detect if the computer running the application is connected to a network. I am synchronizing an Access DB thru my VB6 Code and want to make sure the user has there laptop connected to the network.
Who is Participating?
quiklearnerConnect With a Mentor Commented:
to do it programmatically, yo uCOULD use a winsock control (can be created both with or without a form) however i am not sure about a way to explicitly find out if it connected.  You could check the localip property and i would imagine it may be reset depending on if you use dhcp or not.  Another possibility that would specifically do exactly waht you asked would be to use a windows shell object to execute a command line like: ipconfig.exe /all >>c:\temp.txt
This would place the contents of running the ipconfig /all into the file named c:\temp.txt that you could then parse to see if the cable is actually "disconnected" or not.
My last recommendation which would be easiest wuold be to just error trap the db.connect method, and assume a connection problem if you get an error there (although it could be the db down or some other network problem)

Hope this helps and if you need more details on any suggestions just let me know which ones..
p912sConnect With a Mentor Commented:
>>Access DB thru my VB6 Code and want to make sure the user has there laptop connected to the network.
Is this a mapped drive when connected to the network? You could test for the *.mdb

If Dir("M:\SomeFolder\ServerFile.mdb")<>"" Then
   'file exists do your sync...
End If
AmericaFanAuthor Commented: is a mapped drive when synchronizing so your answer should be the simplest one and I will probably use it in my code, but I wanted to split the points because I am very interested in learning quiklearner's methods. If quiklearner could provided me with a little more info on the methods other than the error trapping I would be most appreciative.
Thanks to both of you,
You're welcome.

Thanks for the points and grade.
To use the winsock control:
If you have a form, go into components and select the "Microsoft Winsock Control".  Draw one on your form.
If you don't have a form, you will need to go into references, click browse, and in your system32, find the file mswinsck.ocx.  Of course, make sure a reference shows up called "Microsoft Winsock Control" and is checked.  Then you will need to create a winsock object.
Regardless of if it is a control or an object, it will have a property called LocalIP.  This is the current LocalIP.  Play with this property with it both on and off the network to see what the difference will be.  When I did it just now, it was when the network was off and my local 192 address when it is on.

Heres a sample to demonstrate the second:
    Dim sTempFileName As String, sContents As String
    sTempFileName = "c:\temp.txt"
    CreateObject("WScript.Shell").Run "cmd /c ipconfig > " & sTempFileName, 0, True
    sContents = CreateObject("Scripting.FileSystemObject").OpenTextFile(sTempFileName, 1).ReadAll
    Kill sTempFileName
    If InStr(1, sContents, "disconnected") And Not InStr(1, sContents, "IP Address") Then
        MsgBox "Looks like it is disconnected"
    End If

First I declared all my vars and set the temp file name.
I execute the Run method of the shell object, executing a program, 0 indicating a hidden window, and True indicating I want to wait until the application has completed. What I run is the ipconfig application.  Since the ipconfig is a command-line app, I actually have to run it within a cmd shell.  The > redirects the output of the command-line application to a file.
Next I got the contents of the temp file placed into a string var to make for easy checking.  1 in the OpenTextFile method indicates ForReading.
I then remove the temp file.
Lastly I check and determine whether or not it looks disconected.  I just used criteria that worked on my system and with my protocols.  You will probably will want to verify the results of the ipconfig on your target system(s) to ensure proper function.  The way I coded it, it would work for computers that are TCP/IP and the check should work for computers with multiple nics (even if one or another wern't currently connected)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.