473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Link Tables in VB

365 100+
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
Oct 22 '09 #1

✓ answered by NeoPa

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.
Expand|Select|Wrap|Line Numbers
  1. 'ReLink() Updates the links of a table to point to the specified database.
  2. Public Sub ReLink(ByVal strTable As String, ByVal strDBName As String)
  3.     Dim intParam As Integer
  4.     Dim strMsg As String, strOldLink As String
  5.     Dim varLinkAry As Variant
  6.  
  7.     On Error GoTo Error_RL
  8.     'Test that the table actually exists first.
  9.     With CurrentDB.TableDefs(strTable)
  10.         If .Attributes And dbAttachedTable Then
  11.             varLinkAry = Split(.Connect, ";")
  12.             For intParam = LBound(varLinkAry) To UBound(varLinkAry)
  13.                 If Left(varLinkAry(intParam), 9) = "DATABASE=" Then Exit For
  14.             Next intParam
  15.             strOldLink = Mid(varLinkAry(intParam), 10)
  16.             If strOldLink <> strDBName Then
  17.                 varLinkAry(intParam) = "DATABASE=" & strDBName
  18.                 .Connect = Join(varLinkAry, ";")
  19.                 Call .RefreshLink
  20.                 strMsg = "[%T] relinked to ""%F"""
  21.                 strMsg = Replace(strMsg, "%T", strTable)
  22.                 strMsg = Replace(strMsg, "%F", strDBName)
  23.                 Debug.Print strMsg
  24.             End If
  25.         End If
  26.     End With
  27.     Exit Sub
  28.  
  29. Error_RL:
  30.     Select Case Err.Number
  31.     Case 3265
  32.         MsgBox = Replace("Table (%T) not found in database", "%T", .strTable)
  33.     Case 3011, 3024, 3044, 3055, 7874
  34.         varLinkAry(intParam) = "DATABASE=" & strOldLink
  35.         .Connect = Join(varLinkAry, ";")
  36.         strMsg = "Database file (%F) not found.%L" & _
  37.                  "Unable to ReLink [%T]."
  38.         strMsg = Replace(strMsg, "%F", strNewLink)
  39.         strMsg = Replace(strMsg, "%L", vbCrLf)
  40.         strMsg = Replace(strMsg, "%T", strTable)
  41.     End Select
  42.     Call MsgBox(Prompt:=strMsg, _
  43.                 Buttons:=vbExclamation Or vbOKOnly, _
  44.                 Title:="ReLink")
  45. End Sub

20 13357
ajalwaysus
266 Expert 100+
Here are 2 functions I use to link to external tables and files:

Expand|Select|Wrap|Line Numbers
  1. Public Function LinkMain(strFileName As String)
  2. Dim strConnection As String
  3. Dim sourceTable As String
  4. Dim TableAlias As String
  5.  
  6. strConnection = "DATABASE=C:\Temp\Access\MY_MDB_be.mdb;TABLE=" & strFileName
  7.  
  8. sourceTable = strFileName 'Access Table Name
  9.  
  10. TableAlias = "MY_TABLE_ALIAS"
  11.  
  12. LinkExternal strConnection, sourceTable, TableAlias
  13.  
  14. End Function
  15.  
  16. Function LinkExternal(ByVal conString As String, sourceTable As String, TableAlias As String)
  17. Dim db As Database
  18. Dim linktbldef As TableDef, rst As Recordset
  19.  
  20. Set db = CurrentDb
  21. Set linktbldef = db.CreateTableDef("tmptable") 'create temporary table definition
  22.  
  23. linktbldef.Connect = conString 'set the connection string
  24. linktbldef.SourceTableName = sourceTable 'attach the source table
  25. db.TableDefs.Append linktbldef 'add the table definition to the group
  26. db.TableDefs.Refresh 'refresh the tabledefinitions
  27.  
  28. linktbldef.Name = TableAlias 'rename the tmptable to original source table name
  29.  
  30. db.Close
  31. Set rst = Nothing
  32. Set linktbldef = Nothing
  33. Set db = Nothing
  34.  
  35. 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
Oct 22 '09 #2
Dan2kx
365 100+
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
Oct 22 '09 #3
ajalwaysus
266 Expert 100+
@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
Oct 22 '09 #4
Dan2kx
365 100+
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.
Oct 22 '09 #5
ajalwaysus
266 Expert 100+
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
Oct 22 '09 #6
Dan2kx
365 100+
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
Oct 22 '09 #7
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.
Oct 23 '09 #8
Dan2kx
365 100+
@NeoPa
Hello NeoPa, could you elaborate for me??
Oct 23 '09 #9
NeoPa
32,556 Expert Mod 16PB
@Dan2kx
Not in a vacuum Dan. What do you need clarified?
Oct 23 '09 #10
Dan2kx
365 100+
@NeoPa
Sorry NeoPa i dont know what to say, im just not sure what you mean?
Oct 23 '09 #11
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.
Oct 24 '09 #12
Dan2kx
365 100+
So to re-map a linked table you would use something like this?

Expand|Select|Wrap|Line Numbers
  1. TableDef.Connection = "Database=C:\Blah......" 'Path to file
  2. Tabdledef.RefreshLink
Oct 24 '09 #13
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.
Oct 25 '09 #14
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.
Expand|Select|Wrap|Line Numbers
  1. 'ReLink() Updates the links of a table to point to the specified database.
  2. Public Sub ReLink(ByVal strTable As String, ByVal strDBName As String)
  3.     Dim intParam As Integer
  4.     Dim strMsg As String, strOldLink As String
  5.     Dim varLinkAry As Variant
  6.  
  7.     On Error GoTo Error_RL
  8.     'Test that the table actually exists first.
  9.     With CurrentDB.TableDefs(strTable)
  10.         If .Attributes And dbAttachedTable Then
  11.             varLinkAry = Split(.Connect, ";")
  12.             For intParam = LBound(varLinkAry) To UBound(varLinkAry)
  13.                 If Left(varLinkAry(intParam), 9) = "DATABASE=" Then Exit For
  14.             Next intParam
  15.             strOldLink = Mid(varLinkAry(intParam), 10)
  16.             If strOldLink <> strDBName Then
  17.                 varLinkAry(intParam) = "DATABASE=" & strDBName
  18.                 .Connect = Join(varLinkAry, ";")
  19.                 Call .RefreshLink
  20.                 strMsg = "[%T] relinked to ""%F"""
  21.                 strMsg = Replace(strMsg, "%T", strTable)
  22.                 strMsg = Replace(strMsg, "%F", strDBName)
  23.                 Debug.Print strMsg
  24.             End If
  25.         End If
  26.     End With
  27.     Exit Sub
  28.  
  29. Error_RL:
  30.     Select Case Err.Number
  31.     Case 3265
  32.         MsgBox = Replace("Table (%T) not found in database", "%T", .strTable)
  33.     Case 3011, 3024, 3044, 3055, 7874
  34.         varLinkAry(intParam) = "DATABASE=" & strOldLink
  35.         .Connect = Join(varLinkAry, ";")
  36.         strMsg = "Database file (%F) not found.%L" & _
  37.                  "Unable to ReLink [%T]."
  38.         strMsg = Replace(strMsg, "%F", strNewLink)
  39.         strMsg = Replace(strMsg, "%L", vbCrLf)
  40.         strMsg = Replace(strMsg, "%T", strTable)
  41.     End Select
  42.     Call MsgBox(Prompt:=strMsg, _
  43.                 Buttons:=vbExclamation Or vbOKOnly, _
  44.                 Title:="ReLink")
  45. End Sub
Oct 25 '09 #15
Dan2kx
365 100+
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:

Expand|Select|Wrap|Line Numbers
  1. Function TableReMap(tbl As String, Path As String)
  2. Dim DB As DAO.Database, tblDef As DAO.TableDef
  3.     Set DB = CurrentDb
  4.     Set tblDef = DB.CreateTableDef(tbl)
  5.     tblDef.Connect = ";Database=" & Path
  6.     tblDef.RefreshLink
It errors at the refreshlink line and says "invalid operation" (3219)

Any ideas? (all the variables are set correctly (AFAIK)

Dan
Jun 22 '10 #16
Dan2kx
365 100+
False alarm, i found my original code
Expand|Select|Wrap|Line Numbers
  1. Function TableReMap(tbl As String, Path As String)
  2. Dim tdf As DAO.TableDef, strPath As String
  3.     strPath = ";Database=" & Path
  4.     For Each tdf In CurrentDb.TableDefs
  5.         If tdf.Name = tbl Then
  6.             If strPath <> tdf.Connect Then
  7.                 tdf.Connect = strPath
  8.                 tdf.RefreshLink
  9.             End If
  10.         End If
  11.     Next
  12. 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
Jun 22 '10 #17
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).
Jun 23 '10 #18
FishVal
2,653 Expert 2GB
@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.
Jun 23 '10 #19
Dan2kx
365 100+
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

Expand|Select|Wrap|Line Numbers
  1. set tbl = "TableName"
how would you do that (for future reference)?
Jun 23 '10 #20
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 :
Expand|Select|Wrap|Line Numbers
  1. Set tbl = CurrentDB.TableDefs("TableName")
Jun 27 '10 #21

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

Similar topics

2
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...
2
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...
0
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...
2
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...
8
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...
4
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...
2
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
7
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...
17
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
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...

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.