Question : ODBC DSN Connection refresh for multiple tables

MS Access dB with hundreds of tables linking to SQL Server using SQL user and pwd.  String functions correctly in ODBC admin test and when relinking ONE table at a time the connection persists.  Problem is that I have over a hundred tables to relink and the Linked Table manager is prompting for every table even though the DSN is already defined and working.

Answer : ODBC DSN Connection refresh for multiple tables

>>  I had a family emergency last week and completely disengaged.

We have all had things happen that are beyond our control. I hope all is well now.

>> I am aware of DSN-less connections but I would like to refresh in code.

I use the code below if I have to create multiple DSNs on the fly. This can load up on on the first form open and create all the ones needed.

>>  Is there a looping function like DO/WHILE or FOR/NEXT
>> that can be called on startup  

I have that code as well, but it should only need to be done once in a while once the tables are linked in.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
DSN_List  Table:
Data_Source_Name	Database_Name	DSN_Description		Server_Name
MyDSN			Master		Connex to MyServer	MyServer
MyDSN1			Northwind	Connex to Northwind	MyServer1
...blah....
--------------------------------------------------
 
Option Compare Database
Option Explicit
Option Base 1
 
Private Const REG_SZ = 1    'Constant for a string variable type.
Private Const HKEY_LOCAL_MACHINE = &H80000002
 
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _
   "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
   phkResult As Long) As Long
 
Private 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
 
Private Declare Function RegCloseKey Lib "advapi32.dll" _
   (ByVal hKey As Long) As Long
 
Public Function Add_System_DSN()
 
'If you need to add multiple ODBC calls just copy from the Public Function _
 to the bottom and rename the function. And make the changes as indicated _
 by the comments. You only need the declarations above in the module once.
 
'This function is designed for creating system level ODBC calls to MS SQL _
 Server databases on the fly.
 
'The form events are listed below in the order that they occur.  The call to _
 this function has to be done in the Open Event, because the Load Event is _
 where it will attempt to use the ODBC call to start pulling in data.
 
'Open Þ Load Þ Resize Þ Activate Þ Current
 
Dim DB As Database
Dim RS As Recordset
Dim SQL As String
 
Dim DataSourceName As String
Dim DatabaseName As String
Dim Description As String
Dim DriverPath As String
Dim DriverName As String
Dim LastUser As String
Dim Regional As String
Dim Server As String
Dim AddSetup As String
 
Dim lResult As Long
Dim hKeyHandle As Long
 
Set DB = CurrentDb()
Set RS = DB.OpenRecordset("DSN_List")
 
RS.MoveFirst
Do Until RS.EOF = True
 
'Index_Num   Data_Source_Name    Database_Name   DSN_Description Server_Name
'1   LCSQL1  Master  Connex to LCSQL1    LCSQL1
   
   'Specify the DSN parameters.
 
   DataSourceName = RS!Data_Source_Name           '<--- The name of the ODBC Call. (1) See below.
   DatabaseName = RS!Database_Name             '<--- This is the database you are targeting.
   Description = RS!DSN_Description        '<--- (2) See below.
   DriverPath = "C:\WINDOWS\system32"   '<--- Default value. Don't change it.
   LastUser = "sa"                      '<--- This can be any valid SQL Userid.
   Server = RS!Server_Name          '<--- The server or DNS value you are targeting.
   DriverName = "SQL Server"            '<--- Default value. Don't change it.
   AddSetup = "No"                      '<--- This is for Additional Setup Options
 
    '(1) This is what you want the ODBC call to be named, such as "VDW" or _
         "ISIR". You do need to be aware that if you are putting a front-end _
         on a delivered application that you are not overwriting some setting _
         that the application comes with.  You can check this by looking at _
         at a workstation that the application is installed on.
    
    '(2) Description: This is optional, but if you don't use it delete it _
         in the set values section below, or change it to a single space.
 
   'Create the new DSN key.
 
   lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\" & _
        DataSourceName, hKeyHandle)
 
   'Set the values of the new DSN key.
 
   'The first three below (QuotedId, AnsiNPW, AutoTranslate) are optional depending _
    on the ODBC setup that you are creating.  If you were setting up an ODBC call _
    manually on the second to last page it has check boxes for _
        Use ANSI quoted identifiers. = QuotedId _
        Use ANSI nulls, paddings and warnings. = AnsiNPW _
    and on the last page it has a check box for _
        Perform translation for character data. = AutoTranslate _
    On our home grown databases and many of our delivered ones these are turned off _
    by default. If they would need to be turned on (checked), you just delete the _
    line that matches it.
 
   lResult = RegSetValueEx(hKeyHandle, "QuotedId", 0&, REG_SZ, _
      ByVal AddSetup, Len(AddSetup))
   lResult = RegSetValueEx(hKeyHandle, "AnsiNPW", 0&, REG_SZ, _
      ByVal AddSetup, Len(AddSetup))
   lResult = RegSetValueEx(hKeyHandle, "AutoTranslate", 0&, REG_SZ, _
      ByVal AddSetup, Len(AddSetup))
   lResult = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ, _
      ByVal DatabaseName, Len(DatabaseName))
   lResult = RegSetValueEx(hKeyHandle, "Description", 0&, REG_SZ, _
      ByVal Description, Len(Description))
   lResult = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, _
      ByVal DriverPath, Len(DriverPath))
   lResult = RegSetValueEx(hKeyHandle, "LastUser", 0&, REG_SZ, _
      ByVal LastUser, Len(LastUser))
   lResult = RegSetValueEx(hKeyHandle, "Server", 0&, REG_SZ, _
      ByVal Server, Len(Server))
 
   'Close the new DSN key.
 
   lResult = RegCloseKey(hKeyHandle)
 
   'Open ODBC Data Sources key to list the new DSN in the ODBC Manager.
   'Specify the new value.
   'Close the key.
 
   lResult = RegCreateKey(HKEY_LOCAL_MACHINE, _
      "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle)
   lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _
      ByVal DriverName, Len(DriverName))
   lResult = RegCloseKey(hKeyHandle)
 
    RS.MoveNext
Loop
End Function
Random Solutions  
 
programming4us programming4us