Network detection using VB6

Posted on 2007-04-06
Last Modified: 2012-08-14
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.
Question by:AmericaFan
  • 2
  • 2

Accepted Solution

quiklearner earned 250 total points
ID: 18867373
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..
LVL 18

Assisted Solution

p912s earned 250 total points
ID: 18867516
>>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

Author Comment

ID: 18869177 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,
LVL 18

Expert Comment

ID: 18869207
You're welcome.

Thanks for the points and grade.

Expert Comment

ID: 18871606
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)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now