Getting ODBC Information Using Visual Basic

It is clear that in order to use ODBC effectively, the programmer may need to know what drivers and data sources exist on a particular computer. This information is accessible through a few ODBC API calls.

The following code includes a procedure called ListODBCSources, which prints (to the Immediate window) a list of all data sources on a system, and ListODBCDrivers, which prints a list of ODBC drivers on the system. This code can be placed in an Access code module:

Const SQL_NULL_HANDLE = 0 Const SQL_HANDLE_ENV = 1 Const SQL_FETCH_NEXT = 1 Const SQL_FETCH_FIRST = 2 Const SQL_SUCCESS = 0 Const SQL_ATTR_ODBC_VERSION = 200 Const SQL_OV_ODBC2 = 2 Const SQL_IS_INTEGER = -6 Dim nRetCode As Long Declare Function SQLDrivers Lib "odbc32.dll" (ByVal _ EnvironmentHandle As Long, ByVal Direction As Integer, _ ByVal DriverDescription As String, ByVal BufferLength1 As Integer, _ DescriptionLengthPtr As Integer, ByVal DriverAttributes As String, _ ByVal BufferLength2 As Integer, AttributesLengthPtr As Integer) _ As Integer ' Note that pointers to numbers are passed as numbers by reference! Declare Function SQLDataSources Lib "odbc32.dll" (ByVal _ EnvironmentHandle As Long, ByVal Direction As Integer, _ ByVal ServerName As String, ByVal BufferLength1 As Integer, _ NameLength1Ptr As Integer, ByVal Description As String, _ ByVal BufferLength2 As Integer, NameLength2Ptr As Integer) As Integer Declare Function SQLFreeHandle Lib "odbc32.dll" (ByVal _ HandleType ...

Get Access Database Design and Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.