Solved

Update linked tables in access using vba

Posted on 2006-06-26
7
507 Views
Last Modified: 2008-01-09
I have a database with about 50 tables linked to a SQL Server catalog.  I have a catalog for each month using the following naming convention MTHYYYY.

To switch between catalogs I currently update my ODBC connection pointing to a new month then update all linked tables in my Access database using the "linked table manager" selecting "always prompt for new location".  The catalogs are secured usingSQL Server authenication and are the same for each catalog.

I would like to use VBA to update my linked tables and or point my ODBC connection to a new month allowing me to run queries across multiple months without have to change my ODBC connection and update all linked tables manually.  Also - it would be a bonus if other users / computers could connect to these tables w/o having to manually configure an ODBC connection.

Current Connection: DSN=Prime;APP=Microsoft Office 2003;WSID=SERVER;DATABASE=MTHYYYY;Network=ANON
0
Comment
Question by:rbcarver
  • 4
  • 3
7 Comments
 
LVL 54

Expert Comment

by:nico5038
ID: 16987778
I use this code to relink tables to a backend in the same folder as the current frontend database:

Function fncRelink()
'function to relink tables to the "_be" database
'It's assumed that the "_be" database is in the same folder as the frontend !

Dim td As DAO.TableDef

For Each td In CurrentDb.TableDefs
  If Len(td.Connect) > 0 Then
     td.Connect = ";DATABASE=" & Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\")) & Mid(td.Connect, InStrRev(td.Connect, "\") + 1)
     td.RefreshLink
  End If
Next

MsgBox "Ready, tables relinked"

End Function

But you can ofcourse change the path part.

Nic;o)
0
 

Author Comment

by:rbcarver
ID: 16987966
I changed the DATABASE="XYZ part to the database/catalog I was trying to connect to and received the following error:

Connection failed:
SQLState: '08004'
SQL Server Error: 4060
Server rejected the connectioN; Access to selected database has been denied

A window then popped up allowing me to point to the correct server / database but errors out.
0
 
LVL 54

Expert Comment

by:nico5038
ID: 16988024
Hmm, does the build string look like the:
DSN=Prime;APP=Microsoft Office 2003;WSID=SERVER;DATABASE=MTHYYYY;Network=ANON
you need ?
Just use a breakpoint and ?td.Connect in the immediate window to findout the value before and after the assign statement.

Nic;o)
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:rbcarver
ID: 16988126
I am sorry but I do not follow.  The database I am connecting to is not in the same folder as the frontend - it is a SQL Server.  Could this have something to do with my error?
0
 
LVL 54

Expert Comment

by:nico5038
ID: 16988178
The "working" td.Connect should be modified.
Thus my advise to check the present connection (e.g. in the queries properties popup form) and change the part that needs to be changed.
Can you show the string from a working linked table ? (Open the table in design mode and activate the properties)

Nic;o)
0
 

Author Comment

by:rbcarver
ID: 16988238
This is it:

ODBC;DSN=Prime;APP=Microsoft Office 2003;WSID=ADMFIN;DATABASE=MTHYYYY;Network=ANON;TABLE=dbo._Codes
0
 
LVL 54

Accepted Solution

by:
nico5038 earned 500 total points
ID: 16988310
I would have expected:
ODBC;DSN=Prime;APP=Microsoft Office 2003;WSID=ADMFIN;DATABASE=062006;Network=ANON;TABLE=dbo._Codes
Thus needing an assign statement to create for the TABLE=dbo._Codes a connect string like:
td.Connect = "ODBC;DSN=Prime;APP=Microsoft Office 2003;WSID=ADMFIN;DATABASE=" & strMonthYear & ";Network=ANON;TABLE=dbo._Codes"

as you'll have multiple tables you can use a dynamic version like:
td.connect = "ODBC;DSN=Prime;APP=Microsoft Office 2003;WSID=ADMFIN;DATABASE=" & strMonthYear & ";Network=ANON;TABLE=" & right(td.connect,instrrev(td.connect,"=")+1)

This will add the MonthYear and place the table at the end.

Nic;o)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now