Checking for online access in vb6

I want to use this script to determine if I have access to my server from within my vb6 program.  I want it to NOT try to open the server db if I am not connected.  It is getting inside the if-then statement even when I don't have internet.  Can someone see a problem and/or is there a better way to do this?

Thanks!

Public Sub OnlineAccess()
    'On Error GoTo ErrorHandler
        
    Dim aux As String * 255
    Dim r As Long
        
    bOnlineAccess = False
    
    r = InternetGetConnectedStateEx(r, aux, 254, 0)

    If r = 1 Then
        bOnlineAccess = True

        Set srvr_conn = New ADODB.Connection
        srvr_conn.Open "Provider=SQLNCLI11;Server=12.34.56.789;Database=db;Uid=uid;Pwd=pwd;"
    End If
    
    Exit Sub
ErrorHandler:
    Beep
    ErrorHandler
End Sub

Open in new window

Bob SchneiderCo-OwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Add this code to a module. You can move the code that's in Sub Main to somewhere else.

Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" _
(ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, _
ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Long

Dim sConnType As String * 255
Sub Main()

    If (CheckInternetConnection = True) Then
        MsgBox "We have an active Internet connection!"
    Else
        MsgBox "We don't have an active Internet connection!"
    End If
End Sub
Function CheckInternetConnection() As Boolean
    Dim aux As String * 255
    Dim r As Long
    r = InternetGetConnectedStateEx(r, aux, 254, 0)
    If r = 1 Then
        CheckInternetConnection = True
    Else
        CheckInternetConnection = False
    End If
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob SchneiderCo-OwnerAuthor Commented:
Thank you!

I'm sorry.  I forgot to tell you that I had this in the module:

Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Long

Open in new window


Then I have what I indicated above.  I call that with a Call OnlineAccess when I want to check my internet status.

How does this information change things?
0
Martin LissOlder than dirtCommented:
I'm not sure. I've had the code I posted for a long time and it works for me. I've attached my test project.
29080652.zip
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Martin LissOlder than dirtCommented:
Bob, have I given you the information you need to resolve this problem?
0
Bob SchneiderCo-OwnerAuthor Commented:
It doesn't work.  I have no idea why not because it works for you... I haven't had a chance to look at the test project.
0
Martin LissOlder than dirtCommented:
I haven't had a chance to look at the test project.
Let me know what happens when you do try it.
0
Martin LissOlder than dirtCommented:
I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
0
Bob SchneiderCo-OwnerAuthor Commented:
I have found the problem...maybe?  It looks like if I turn my wireless off the code works.  If I leave it on but I am just in an area where there is no internet, or spotty internet, it will throw an error.  Ostensibly, this happens when there is a glimmer of internet but not enough to open the remote database?  Thoughts?
0
Martin LissOlder than dirtCommented:
No, I'm sorry, but I don't. However if you ask a new question then someone out there who has the answer or ideas might see it.
0
Nick BallardIT ManagerCommented:
I appreciate that it's not a programmatic solution, per se, but could you possibly drop to a command prompt and do, say, a 10-second ping to the server IP address (piped to a file, which is read in) and confirm that you get 10 successful responses from the server. This would show that the server is accessible and a reliable connection has been established? Obviously, you could reduce the number of pings, to speed up the check.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.