ADO vs DAO in Access 2007

L

Larry Novreske

What is the equivalent ADO code to open the ODBC dialog which allows a user
to select or create and then select the DSN of an ODBC data source.

In earlier versions of Access, using DAO the following code is used.

public function fnGetODBCConnect () as string
Dim wrkODBC As Workspace
Dim conPubs As Connection

Set wrkODBC = CreateWorkspace("NewODBCWorkspace", "admin", "", dbUseODBC)

Set conPubs = wrkODBC.OpenConnection("Connection", dbDriverPrompt, True,
"ODBC;DSN=MyDSN;")

fnGetODBCConnect = conPubs.Connect
Exit Function

Which would open up the ODBC dialog for creating/selecting a DSN and allow
me to get the connect string to an ODBC data source.

In all of the examples of ADO connections, all they show is keying in the
connect string which is not what I need.
 
J

Jim Snyder

Why not bypass the ODBC dialog altogether and use the API? This sample will
dynamically write the DSN profile for SQL Server to the registry.
_________________________________________________
Option Compare Database
Option Explicit

Declare Function GetSystemDirectory Lib "kernel32" Alias
"GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA"
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA"
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long,
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private psProcedure As String
Private bSuccess As Boolean
_________________________________________________
Public Function Open_App()

bSuccess = RegisterDb(sDSN:="Common name", _
sDSNData:="SQL Server", _
sDSNDescription:="Common name description", _
sServerName:="Server ID", _
sServerNameData:="dbmssocn,Network ID", _
sDatabaseName:="Database name")

If bSuccess Then
' assign your connect string
End If

End Function

_________________________________________________
Private Function RegisterDb(ByVal sDSN As String, ByVal sDSNData As String,
ByVal sDSNDescription As String, ByVal sServerName As String, ByVal
sServerNameData As String, ByVal sDatabaseName As String) As Boolean
On Error GoTo Err_RegisterDb

Dim sSQLDriverPath As String
Dim hKeyHandle As Long
Dim lRetVal As Long

Const REG_SZ = 1
Const HKEY_LOCAL_MACHINE = &H80000002

bSuccess = GetDriver(sSQLDriverPath)
If bSuccess Then
lRetVal = RegCreateKey(HKEY_LOCAL_MACHINE,
"SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo", hKeyHandle)
lRetVal = RegSetValueEx(hKeyHandle, sServerName, 0&, REG_SZ, ByVal
sServerNameData, Len(sServerNameData))
lRetVal = RegCloseKey(hKeyHandle)

lRetVal = RegCreateKey(HKEY_LOCAL_MACHINE,
"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle)
lRetVal = RegSetValueEx(hKeyHandle, sDSN, 0&, REG_SZ, ByVal
sDSNData, Len(sDSNData))
lRetVal = RegCloseKey(hKeyHandle)

lRetVal = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\"
& sDSN, hKeyHandle)
lRetVal = RegSetValueEx(hKeyHandle, "Description", 0&, REG_SZ, ByVal
sDSNDescription, Len(sDSNDescription))
lRetVal = RegSetValueEx(hKeyHandle, "Server", 0&, REG_SZ, ByVal
sServerName, Len(sServerName))
lRetVal = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ, ByVal
sDatabaseName, Len(sDatabaseName))
lRetVal = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, ByVal
sSQLDriverPath, Len(sSQLDriverPath))
lRetVal = RegSetValueEx(hKeyHandle, "OemToAnsi", 0&, REG_SZ, ByVal
"No", Len("No"))
lRetVal = RegSetValueEx(hKeyHandle, "LastUser", 0&, REG_SZ, ByVal
"", 1)
lRetVal = RegSetValueEx(hKeyHandle, "Trusted_Connection", 0&,
REG_SZ, ByVal "No", Len("No"))
lRetVal = RegCloseKey(hKeyHandle)
End If

Exit_RegisterDb:
RegisterDb = bSuccess
Exit Function

Err_RegisterDb:
psProcedure = "basOpen :: RegisterDb"
MsgBox Err.Number & ": " & Err.Description, vbCritical, psProcedure
bSuccess = False
Resume Exit_RegisterDb

End Function

_________________________________________________
Private Function GetDriver(ByRef sSQLDriverPath As String) As Boolean
On Error GoTo Err_GetDriver

Dim sPath As String
Dim lDirectoryFound As Long
Dim iLength As Integer

Const MAX_LENGTH = 50

sPath = String(255, 0)
lDirectoryFound = GetSystemDirectory(sPath, MAX_LENGTH)

If lDirectoryFound Then
iLength = InStr(sPath, Chr$(0)) - 1
sSQLDriverPath = Left$(sPath, iLength) & "\sqlsrv32.dll"
bSuccess = True
Else
bSuccess = False
End If

Exit_GetDriver:
GetDriver = bSuccess
Exit Function

Err_GetDriver:
psProcedure = "basOpen :: GetDriver"
MsgBox Err.Number & ": " & Err.Description, vbCritical, psProcedure
bSuccess = False
Resume Exit_GetDriver

End Function
_________________________________________________

All the best,
Jim
 
L

Larry Novreske

I appreciate your help, however the problem is more than just opening the
ODBC connection dialog. The DAO code sample not only opens the dialog, but
returns the selected dsn connect string using the selected DSN
 
L

Larry Novreske

Thanks for your response, however creating and writing an ODBC DSN to the
registry is not what I am looking for. My Access database has about 150
linked tables. I have a re-link form which goes out and gets a connect
string either a link to tables in another access .accdb or to a SQL ODBC link
and then re-attaches all of the linked tables to the tables based upon the
selected connection. In the DAO code the line of code that creates the ODBC
workspace to dbUseODBC is no longer supported, therefore doing the
..openConnection line doesn't work, it is these 2 lines of code that do all of
the work in the DAO version. Once the workspace is created, then the set of
the openConnection goes out and opens the ODBC dialog (dbDriverPrompt) and
sets the connect string for the conPubs. Once that is set getting its
connect string provides the needed information.
 
L

Larry Novreske

unfortunately the kb article was written in 2004 and does not really give any
usable information. Again thanks for the help, I appreciate you trying to
help me.
 
R

Robert Morley

I'm not sure what more you would want...Example 2 opens up the ODBC prompt,
just like the DAO code does, which is what you asked for.


Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top