Hi peeps,
i have a current need to change the linked tables in my latest access project, and want to do it using VB. normally i like most people use the linked tables manager.
What i am designing is a stock database to be used on multiple sites, and want to have a BE for each site (to reduce WAN lag with one massive DB), however i would like the option to re-link (in rare circumstances), i have found ADOX? linking code previously but couldnt seem to get it to work (so i left it alone) now i think it would be more useful.
I basically want to mirror the link tables manager functio in VB
Any help would be much appreciated.
Dan
NeoPa: TableDefs in Access have a property .Connection and a method .RefreshLink. First change the .Connection property to the new value required, then call the .RefreshLink method.
This code is not compiled or tested so let me know if there are any problems with it, but it should allow you to relink any of your Access linked tables to a new, specified destination database. - 'ReLink() Updates the links of a table to point to the specified database.
-
Public Sub ReLink(ByVal strTable As String, ByVal strDBName As String)
-
Dim intParam As Integer
-
Dim strMsg As String, strOldLink As String
-
Dim varLinkAry As Variant
-
-
On Error GoTo Error_RL
-
'Test that the table actually exists first.
-
With CurrentDB.TableDefs(strTable)
-
If .Attributes And dbAttachedTable Then
-
varLinkAry = Split(.Connect, ";")
-
For intParam = LBound(varLinkAry) To UBound(varLinkAry)
-
If Left(varLinkAry(intParam), 9) = "DATABASE=" Then Exit For
-
Next intParam
-
strOldLink = Mid(varLinkAry(intParam), 10)
-
If strOldLink <> strDBName Then
-
varLinkAry(intParam) = "DATABASE=" & strDBName
-
.Connect = Join(varLinkAry, ";")
-
Call .RefreshLink
-
strMsg = "[%T] relinked to ""%F"""
-
strMsg = Replace(strMsg, "%T", strTable)
-
strMsg = Replace(strMsg, "%F", strDBName)
-
Debug.Print strMsg
-
End If
-
End If
-
End With
-
Exit Sub
-
-
Error_RL:
-
Select Case Err.Number
-
Case 3265
-
MsgBox = Replace("Table (%T) not found in database", "%T", .strTable)
-
Case 3011, 3024, 3044, 3055, 7874
-
varLinkAry(intParam) = "DATABASE=" & strOldLink
-
.Connect = Join(varLinkAry, ";")
-
strMsg = "Database file (%F) not found.%L" & _
-
"Unable to ReLink [%T]."
-
strMsg = Replace(strMsg, "%F", strNewLink)
-
strMsg = Replace(strMsg, "%L", vbCrLf)
-
strMsg = Replace(strMsg, "%T", strTable)
-
End Select
-
Call MsgBox(Prompt:=strMsg, _
-
Buttons:=vbExclamation Or vbOKOnly, _
-
Title:="ReLink")
-
End Sub
20 13357
Here are 2 functions I use to link to external tables and files: - Public Function LinkMain(strFileName As String)
-
Dim strConnection As String
-
Dim sourceTable As String
-
Dim TableAlias As String
-
-
strConnection = "DATABASE=C:\Temp\Access\MY_MDB_be.mdb;TABLE=" & strFileName
-
-
sourceTable = strFileName 'Access Table Name
-
-
TableAlias = "MY_TABLE_ALIAS"
-
-
LinkExternal strConnection, sourceTable, TableAlias
-
-
End Function
-
-
Function LinkExternal(ByVal conString As String, sourceTable As String, TableAlias As String)
-
Dim db As Database
-
Dim linktbldef As TableDef, rst As Recordset
-
-
Set db = CurrentDb
-
Set linktbldef = db.CreateTableDef("tmptable") 'create temporary table definition
-
-
linktbldef.Connect = conString 'set the connection string
-
linktbldef.SourceTableName = sourceTable 'attach the source table
-
db.TableDefs.Append linktbldef 'add the table definition to the group
-
db.TableDefs.Refresh 'refresh the tabledefinitions
-
-
linktbldef.Name = TableAlias 'rename the tmptable to original source table name
-
-
db.Close
-
Set rst = Nothing
-
Set linktbldef = Nothing
-
Set db = Nothing
-
-
End Function
I don't know how much you may pick up from this but this can get you started. Let me know if/when you have any questions that I can clarify for you.
-AJ
HI,
Thanks for the response, firstly i got a error 3170 at line #25 of your posted code.
secondly is the strFileName the name of the table? it looks like it should be to me? is there a need for the table alias if this is true?
and i am unsure of the need for a tmptable? can you not just name it straight off? does it cause a problem if the tables are named the same (in the different dbs)?
Thanks
@Dan2kx
You will need to give me the error text, I don't have the time to look them up. @Dan2kx
Yes strFileName is the name of the table/file, the table alias is not required but I leave it in there if the need for an alias arises, which in my case has. @Dan2kx
You can do with the code what you wish, I won't take credit for creating this code, and I don't know what would happen because if it ain't broke, I won't fix it. Also, I am pretty sure you cannot overwrite tables with the same name using this code, so you will need to drop the table before you "relink".
-AJ
OK, just making sure/trying to understand the code properly
Could not find installable ISAM. (Error 3170)
The DLL for an installable ISAM file could not be found. This file is required for linking external tables (other than ODBC or Microsoft Jet database tables). The locations for all ISAM drivers are maintained in the Microsoft® Windows® Registry. These entries are created automatically when you install your application. If you change the location of these drivers, you need to correct your application Setup program to reflect this change and make the correct entries in the Registry.
Possible causes:
An entry in the Registry is not valid. For example, this error occurs if you are using a Paradox external database, and the Paradox entry points to a nonexistent directory or driver. Exit the application, correct the Windows Registry, and try the operation again.
One of the entries in the Registry points to a network drive and that network is not connected. Make sure the network is available, and then try the operation again.
Absolutely, I have no problem answering your questions.I could have posted more info with the code, but I feel we all learn better figuring it out on our own with some help and not being spoon fed the answers to everything. I found this code online and I think I learned more by having to figure out how to adapt it myself. =)
What version of Access are you using, and please post your code as you have adjusted it.
-AJ
Found what i was looking for http://support.microsoft.com/kb/209841
this is very similar to your example exception being the source table name,
and it works.
Took me a long time to find it though
Thanks for the help, problem solved
Dan, Out
NeoPa 32,556
Expert Mod 16PB
TableDefs in Access have a property .Connection and a method .RefreshLink. First change the .Connection property to the new value required, then call the .RefreshLink method.
@NeoPa
Hello NeoPa, could you elaborate for me??
NeoPa 32,556
Expert Mod 16PB @Dan2kx
Not in a vacuum Dan. What do you need clarified?
@NeoPa
Sorry NeoPa i dont know what to say, im just not sure what you mean?
NeoPa 32,556
Expert Mod 16PB @Dan2kx
That's a fair enough response Dan.
I think that what I have posted covers the whole subject completely. It's pretty well the whole shebang. If you need more then I need to understand what you need. I don't have time to put together an article covering everything remotely related to the subject, so I need to understand what you need clarification on.
So to re-map a linked table you would use something like this? - TableDef.Connection = "Database=C:\Blah......" 'Path to file
-
Tabdledef.RefreshLink
NeoPa 32,556
Expert Mod 16PB
That's about the size of it yes.
In case it helps, I will also include some code which remaps a linked table safely. There are various reasons why a relink will fail, if it does then the connection should revert to the value that actually matches the link. Give me a while to knock it up.
NeoPa 32,556
Expert Mod 16PB NeoPa: TableDefs in Access have a property .Connection and a method .RefreshLink. First change the .Connection property to the new value required, then call the .RefreshLink method.
This code is not compiled or tested so let me know if there are any problems with it, but it should allow you to relink any of your Access linked tables to a new, specified destination database. - 'ReLink() Updates the links of a table to point to the specified database.
-
Public Sub ReLink(ByVal strTable As String, ByVal strDBName As String)
-
Dim intParam As Integer
-
Dim strMsg As String, strOldLink As String
-
Dim varLinkAry As Variant
-
-
On Error GoTo Error_RL
-
'Test that the table actually exists first.
-
With CurrentDB.TableDefs(strTable)
-
If .Attributes And dbAttachedTable Then
-
varLinkAry = Split(.Connect, ";")
-
For intParam = LBound(varLinkAry) To UBound(varLinkAry)
-
If Left(varLinkAry(intParam), 9) = "DATABASE=" Then Exit For
-
Next intParam
-
strOldLink = Mid(varLinkAry(intParam), 10)
-
If strOldLink <> strDBName Then
-
varLinkAry(intParam) = "DATABASE=" & strDBName
-
.Connect = Join(varLinkAry, ";")
-
Call .RefreshLink
-
strMsg = "[%T] relinked to ""%F"""
-
strMsg = Replace(strMsg, "%T", strTable)
-
strMsg = Replace(strMsg, "%F", strDBName)
-
Debug.Print strMsg
-
End If
-
End If
-
End With
-
Exit Sub
-
-
Error_RL:
-
Select Case Err.Number
-
Case 3265
-
MsgBox = Replace("Table (%T) not found in database", "%T", .strTable)
-
Case 3011, 3024, 3044, 3055, 7874
-
varLinkAry(intParam) = "DATABASE=" & strOldLink
-
.Connect = Join(varLinkAry, ";")
-
strMsg = "Database file (%F) not found.%L" & _
-
"Unable to ReLink [%T]."
-
strMsg = Replace(strMsg, "%F", strNewLink)
-
strMsg = Replace(strMsg, "%L", vbCrLf)
-
strMsg = Replace(strMsg, "%T", strTable)
-
End Select
-
Call MsgBox(Prompt:=strMsg, _
-
Buttons:=vbExclamation Or vbOKOnly, _
-
Title:="ReLink")
-
End Sub
NeoPa (or anyone else), i need your help with this again,
I had it working before but want to use it in a new database here is my code: - Function TableReMap(tbl As String, Path As String)
-
Dim DB As DAO.Database, tblDef As DAO.TableDef
-
Set DB = CurrentDb
-
Set tblDef = DB.CreateTableDef(tbl)
-
tblDef.Connect = ";Database=" & Path
-
tblDef.RefreshLink
It errors at the refreshlink line and says "invalid operation" (3219)
Any ideas? (all the variables are set correctly (AFAIK)
Dan
False alarm, i found my original code - Function TableReMap(tbl As String, Path As String)
-
Dim tdf As DAO.TableDef, strPath As String
-
strPath = ";Database=" & Path
-
For Each tdf In CurrentDb.TableDefs
-
If tdf.Name = tbl Then
-
If strPath <> tdf.Connect Then
-
tdf.Connect = strPath
-
tdf.RefreshLink
-
End If
-
End If
-
Next
-
End Function
not quite sure how that differs (apart from the loop) but that access for ya huh (or just me i dont know)
Dan
NeoPa 32,556
Expert Mod 16PB
Your code for post #16 is actually trying to create a new TableDef rather than amend an existing one (See line #4 particularly).
@NeoPa
And TableDef object returned by DAO.Database.CreateTableDef method has to be appended to TableDefs collection via TableDefs.Append method to become an existing Access table.
Ok thanks fellas, i did try some other fiddles before i got to that code but they didnt seem to work either.
the trouble i had was trying to set a string value as a tabledef eg
how would you do that (for future reference)?
NeoPa 32,556
Expert Mod 16PB
If you look at post #15 then line #9 illustrates this (as a With version rather than a Set mind). For that you would use something like : - Set tbl = CurrentDB.TableDefs("TableName")
Sign in to post your reply or Sign up for a free account.
Similar topics
by: deko |
last post by:
Rather than using
File >> Get External Data >> Link Tables...
I'm wondering if I can automate the linking process with VBA. The Excel
Spreadsheet I want to link to will always have the same...
|
by: Jackson |
last post by:
I'm having a problem with some .DBF tables and Access 2002. When I try to
link them as external data, Access reports "External table is not in
expected format", help says the table has changed...
|
by: news.fl.comcast.giganews.com |
last post by:
Something strange has happened on my computer. I am unable to link tables
from SQL Server 2000. I have tried from Access 97 and 2000, but keep
getting the error "The Microsoft Jet database engine...
|
by: David |
last post by:
Hi,
We have an internal network of 3 users.
Myself & one other currently have individual copies of the front-end
MS Access forms and via our individual ODBC links we have used the:
File > Get...
|
by: ZRexRider |
last post by:
Hi,
I have an application that uses MS-SQL Linked tables. I have a utility
routine that drops all links and re-establishes them. It works great
when my connection string is a DSN connection...
|
by: sbowman |
last post by:
I'm linking a bunch of tables that are in SQL to my Access db and I get
the following error on 3 of the tables: "Seq.Group is not a valid
name." I tried doing an import instead of a link, but I got...
|
by: sbowman |
last post by:
How do I link tables in Access XP to a Lotus Notes Database or View? I
read an article on MS KB, but it only stated that you can link after
exporting data out of Lotus.
Thanks!
Shelley
|
by: guest |
last post by:
Hi, I need help on this error.
I am trying to link tables from a Sybase Database to MS Access, using Sybase system 11.
I tried conneting to the database using SQL advantage and it does connect...
|
by: aquablade |
last post by:
I have an Oracle 10g R2 database where my ERP transactions reside. I'm using MS Access to grant access to my power users where I use MS Access' Link Tables to provide direct access.
I noticed that...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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,...
|
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...
| |