473,473 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Relinking ODBC Tables using VBA

MMcCarthy
14,534 Recognized Expert Moderator MVP
As a lot of my projects involve using an odbc linked backend data source I have found this piece of code to be very useful. I usually trigger it to run on system startup like in AutoExec macro. It avoids any difficulties down the road with broken links and if the DSN name or database server changes then the only edit that needs to be made is to the connection string.

Expand|Select|Wrap|Line Numbers
  1. Function relinkTables()
  2. Dim tdf As DAO.TableDef
  3.  
  4.     For Each tdf In CurrentDb.TableDefs
  5.         ' check if table is a linked table
  6.         If Len(tdf.Connect) > 0 Then
  7.             tdf.Connect = "odbc connection string to the DSN or database"
  8.             tdf.RefreshLink
  9.         End If
  10.     Next
  11.  
  12. End Function
  13.  
Note: If you have other linked tables aside from those connected by odbc you would have to allow for them in the code.

As usual all advice, critique and enhancements welcome :)

Mary
Nov 13 '09 #1
8 40317
NeoPa
32,556 Recognized Expert Moderator MVP
I have one that works specifically for Access database linked tables. It may be worth including that and making it less specific :
Expand|Select|Wrap|Line Numbers
  1. 'ReLink() Updates links of all tables that currently link to strDBName to point
  2. 'to strDBName in the strFolder folder (if specified, otherwise the same folder
  3. 'as the current database).
  4. Public Sub ReLink(ByVal strDBName As String, _
  5.                   Optional ByVal strFolder As String = "")
  6.     Dim intParam As Integer, intErrNo As Integer
  7.     Dim strOldLink As String, strOldName As String
  8.     Dim strNewLink As String, strMsg As String
  9.     Dim varLinkAry As Variant
  10.     Dim db As DAO.Database
  11.     Dim tdf As DAO.TableDef
  12.  
  13.     Set db = CurrentDb()
  14.     If strFolder = "" Then strFolder = CurrentProject.Path
  15.     If Right(strFolder, 1) = "\" Then _
  16.         strFolder = Left(strFolder, Len(strFolder) - 1)
  17.     strNewLink = strFolder & "\" & strDBName
  18.     For Each tdf In db.TableDefs
  19.         With tdf
  20.             If .Attributes And dbAttachedTable Then
  21.                 varLinkAry = Split(.Connect, ";")
  22.                 For intParam = LBound(varLinkAry) To UBound(varLinkAry)
  23.                     If Left(varLinkAry(intParam), 9) = "DATABASE=" Then Exit For
  24.                 Next intParam
  25.                 strOldLink = Mid(varLinkAry(intParam), 10)
  26.                 If strOldLink <> strNewLink Then
  27.                     strOldName = Split(strOldLink, _
  28.                                        "\")(UBound(Split(strOldLink, "\")))
  29.                     If strOldName = strDBName Then
  30.                         varLinkAry(intParam) = "DATABASE=" & strNewLink
  31.                         .Connect = Join(varLinkAry, ";")
  32.                         On Error Resume Next
  33.                         Call .RefreshLink
  34.                         intErrNo = Err.Number
  35.                         On Error GoTo 0
  36.                         Select Case intErrNo
  37.                         Case 3011, 3024, 3044, 3055, 7874
  38.                             varLinkAry(intParam) = "DATABASE=" & strOldLink
  39.                             .Connect = Join(varLinkAry, ";")
  40.                             strMsg = "Database file (%F) not found.%L" & _
  41.                                      "Unable to ReLink [%T]."
  42.                             strMsg = Replace(strMsg, "%F", strNewLink)
  43.                             strMsg = Replace(strMsg, "%L", vbCrLf)
  44.                             strMsg = Replace(strMsg, "%T", .Name)
  45.                             Call MsgBox(Prompt:=strMsg, _
  46.                                         Buttons:=vbExclamation Or vbOKOnly, _
  47.                                         Title:="ReLink")
  48.                             If intErrNo = 3024 _
  49.                             Or intErrNo = 3044 _
  50.                             Or intErrNo = 3055 Then Exit For
  51.                         Case Else
  52.                             strMsg = "[%T] relinked to ""%F"""
  53.                             strMsg = Replace(strMsg, "%T", .Name)
  54.                             strMsg = Replace(strMsg, "%F", strNewLink)
  55.                             Debug.Print strMsg
  56.                         End Select
  57.                     End If
  58.                 End If
  59.             End If
  60.         End With
  61.     Next tdf
  62. End Sub
The reason it's much longer is that it has to handle errors, and recover from them. The meat of it is very similar. Maybe we could look at a version of the ODBC one that handles the various failure scenarios too. That would be a very useful article to link to I expect.
Nov 13 '09 #2
MMcCarthy
14,534 Recognized Expert Moderator MVP
Nice one Ade, valuable addition

Mary
Nov 14 '09 #3
Denburt
1,356 Recognized Expert Top Contributor
Hello all, I thought I would throw my two cents in on this one. Correct me if I am mistaken but according to the following article if you want to improve performance I make sure that when I link to Access tables I make sure that the naming convention uses the 8.3 format. I know there is a knowledge base article on it (somewhere with a similar function) but I have a short function I call before I relink each of these Access tables.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Option Compare Database
  3. 'Dim BkEnd As String
  4. Declare Function GetShortPathName Lib "kernel32" _
  5.             Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
  6.            ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
  7. Function GetShortName(ByVal sLongFileName As String) As String
  8.            Dim lRetVal As Long, sShortPathName As String, iLen As Integer
  9.            'Set up a buffer area for the API function call return.
  10.            sShortPathName = Space(255)
  11.            iLen = Len(sShortPathName)
  12.  
  13.            'Call the function.
  14.            lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
  15.            'Remove unwanted characters.
  16.            GetShortName = Left(sShortPathName, lRetVal)
  17.  End Function
http://support.microsoft.com/kb/889588/

Use 8.3 file name conventions
Access calls the GetShortPathNameW function across the network on each append query if the database file name is longer than eight characters or if the database is located in a folder name that is longer than eight characters.

This behavior occurs with file names and folder names that are longer than the 8.3 file naming convention limits specify. Long file and folder names can increase the time that is required for the query to be completed. If the name of your database file or of the folder where your database is located is longer than eight characters, rename the file name or the folder name. The file and folder names must be no longer than eight characters, and the file name extension must be no longer than three characters. The following is an example of a database path that includes a short, 8.3 convention file and folder names:
\Folder_1\Folder_2\AccessDb.mdb
The following is an example of a database path that uses long file and folder names:
\FolderForFirstDatabase\FolderForSecondDatabase\Th isIsA_BigDatabase.mdb
Nov 19 '09 #4
MMcCarthy
14,534 Recognized Expert Moderator MVP
@Denburt
Useful information, indeed :)
Nov 19 '09 #5
Denburt
1,356 Recognized Expert Top Contributor
Here is another FYI...

http://office.microsoft.com/en-us/access/hp051874531033.aspx
"You can greatly enhance performance, when opening the main database and opening tables and forms, by forcing the linked database to remain open. To do this, create an empty table in the linked database, and link the table in the main database. Then, use the OpenRecordset method to open the linked table. This prevents the Microsoft Jet database engine from repeatedly opening and closing the linked database and from creating and deleting the associated .ldb file"
Nov 19 '09 #6
MMcCarthy
14,534 Recognized Expert Moderator MVP
@Denburt
This is interesting.

It hasn't been my experience that the database opening and closing has caused any problems (i.e. the *ldb file) but I have to say I haven't monitored it closely.

What affect does opening the recordset have, if any, on multiple front end users? Also I assume that the variable to hold this recordset has to be globally set and retain it's value (as in this variable cannot be reused) until the frontend closes.

Mary
Nov 19 '09 #7
Denburt
1,356 Recognized Expert Top Contributor
Interesting points, to be honest I haven't sat down and timed it or noticed a hit when multiple users are using it, but I do open a recordset when the Main Menu opens I use it for version updates. I use the timer of the main menu to compare the linked table of the recordset to a local table periodically to see if a newer version is available. I tend to update the front end quite frequently. When the Main menu closes it closes that linked recordset.
Nov 20 '09 #8
NeoPa
32,556 Recognized Expert Moderator MVP
A new question was posted in here. I've moved it to its own thread (How do I Use ReLink()) as posting questions within an article thread is not allowed, nor is it sensible. Who'd ever see it to respond to it after all?
Apr 25 '18 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ike_strong | last post by:
Can anyone help me to figure out why each time I try to access my database using: c:\mysql\bin\inmysql -root -p and afterwards:(i.e. after inputting my at the password prompt) Enter...
6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
0
by: mcast | last post by:
How do I modify Tables/Database's though ODBC without using Dynasets? Can point me in the right direction or show me some sample code. I'm new to Visual C++ programming.
11
by: DraguVaso | last post by:
Hi, I want to make a small application in VB.NET that relinks all the query's and tables in an Access database that are linked via ODBC to an SQL Server. It must be able to relink all the tables...
2
by: SKB | last post by:
Hi, I am absolutely new to this area. I am getting the following difficulty : Access denied for user 'ODBC'@'localhost' (using password: NO) when I try the mysql command from within the...
0
by: jrhowcroft | last post by:
I have a number of databases in MS Access 97. Some tables in these databases are linked to ODBC tables in Oracle9/10. We would like to change the user name and password on the Oracle tables so each...
10
by: Richard | last post by:
Hi folks, thanks for taking the time to read this (and hopefully point our where I'm going wrong). The scenario: I have a local Access2007 database which links in several read only mySql...
81
MMcCarthy
by: MMcCarthy | last post by:
Hi everyone I am using this as a test question to test the new Rate up/down feature for threads and posts. For anyone looking for a legitimate answer to this question please check out this...
3
by: kathikeyan | last post by:
hi i want to know the meaning of false,false in the following code for odbc connectivity using vb6.0. set db=opendatabase("dsn",false,false;"odbc;uid;pwd;dsn")
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.