• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 72
  • Last Modified:

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

0
Bob Schneider
Asked:
Bob Schneider
  • 6
  • 3
1 Solution
 
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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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
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.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now