1

I have an Access database that links to six tables in other SQL databases using ODBC connections. I then have a couple of queries running checks on these tables generating some reports. However, before I run the queries, I want to ensure that all six tables are reachable. All the DB's are offsite and I connect to them over a VPN connection, so it does happen that a table might be offline for a couple of minutes due to connectivity constraints.

I have done so many searches but I am not able to find something that even remotely answers my question.

Ideally I would like to have a list of the tables with a little green / red dot next to each showing whether it is online or not. But I guess one needs to be realistic.

Any assistance will be greatly appreciated.

gdekoker
  • 185
  • 8
  • 2
    Your best bet would be to try to open the tables as recordsets and see what the result of this operation is. You can create a form with the table names and the green/red dots and use the OnTimer event to perform that check at whatever interval you think is necessary. – SunKnight0 Nov 20 '19 at 18:30
  • Thank you very much. I am still very new to Access and ODBC tables. How would I "open the tables as recordsets". I have no idea how to do that. – gdekoker Nov 20 '19 at 18:32
  • 1
    https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/database-openrecordset-method-dao – SunKnight0 Nov 20 '19 at 18:35

1 Answers1

2

You could simply “try” and open the table(s) in question. However, if your connection is gone, then you find some VERY nasty effects. First you get a huge delay (bad). And if you trigger an ODBC error, in most cases you have to exit the application.

To avoid the above?

It turns out there is a different pathway to get access to check if you have use of the server. It not only eliminates the dreaded and all evil ODBC error, but ALSO errors out MUCH faster to boot! You find the “error out” is VERY fast!

The way this works is you use a queryDef to check if you have a connection. This causes access to use a “different” path and test then opening a reocrdset.

So this approach avoids the long delay, and also ODBC errors.

You can use this routine for the test:

Function TestLogon(strCon As String) As Boolean

  On Error GoTo TestError

  Dim dbs          As DAO.Database
  Dim qdf          As DAO.QueryDef

  Set dbs = CurrentDb()
  Set qdf = dbs.CreateQueryDef("")

   qdf.Connect = strCon
   qdf.ReturnsRecords = False

   'Any VALID SQL statement that runs on server will work below.

   qdf.SQL = "SELECT 1"

   qdf.Execute

   TestLogon = True

   Exit Function

TestError:
   TestLogon = False
   Exit Function

End Function

Now, say in our code, we had a linked table. So, we can use/grab the connection of that linked table, and test like this:

Sub MyCoolUpdate()

  Dim strCon     As String

  Dim rstHotels  As DAO.Recordset

  strCon = CurrentDb.TableDefs("dbo_tblHotels").Connect

  If TestLogon(strCon) = True Then

     ' it is safe to open the linked table,
     'eg:
     Set rstHotels = CurrentDb.OpenRecordset("dbo_tblHotels", dbOpenDynaset, dbSeeChanges)

     ' we are conneced - do your stuff
     ' walk the dog - do updates

     rstHotels.Close

  End If

End Sub

So I much suggest you don't try and touch a linked table to the server until such time you test using the above trick.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51