Connecting Tech Pros Worldwide Help | Site Map

Link Tables in VB

Needs Regular Fix
 
Join Date: Oct 2007
Location: Hull,UK
Posts: 284
#1: 4 Weeks Ago
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
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 244
#2: 4 Weeks Ago

re: Link Tables in VB


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
Needs Regular Fix
 
Join Date: Oct 2007
Location: Hull,UK
Posts: 284
#3: 4 Weeks Ago

re: Link Tables in VB


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
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 244
#4: 4 Weeks Ago

re: Link Tables in VB


Quote:

Originally Posted by Dan2kx View Post

firstly i got a error 3170 at line #25 of your posted code.

You will need to give me the error text, I don't have the time to look them up.

Quote:

Originally Posted by Dan2kx View Post

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?

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.

Quote:

Originally Posted by Dan2kx View Post

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)?

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
Needs Regular Fix
 
Join Date: Oct 2007
Location: Hull,UK
Posts: 284
#5: 4 Weeks Ago

re: Link Tables in VB


OK, just making sure/trying to understand the code properly

Quote:
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.
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 244
#6: 4 Weeks Ago

re: Link Tables in VB


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
Needs Regular Fix
 
Join Date: Oct 2007
Location: Hull,UK
Posts: 284
#7: 4 Weeks Ago

re: Link Tables in VB


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's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,662
#8: 4 Weeks Ago

re: Link Tables in VB


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.
Needs Regular Fix
 
Join Date: Oct 2007
Location: Hull,UK
Posts: 284
#9: 4 Weeks Ago

re: Link Tables in VB


Quote:

Originally Posted by NeoPa View Post

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.

Hello NeoPa, could you elaborate for me??
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,662
#10: 4 Weeks Ago

re: Link Tables in VB


Quote:

Originally Posted by Dan2kx View Post

Hello NeoPa, could you elaborate for me??

Not in a vacuum Dan. What do you need clarified?
Needs Regular Fix
 
Join Date: Oct 2007
Location: Hull,UK
Posts: 284
#11: 4 Weeks Ago

re: Link Tables in VB


Quote:

Originally Posted by NeoPa View Post

Not in a vacuum Dan. What do you need clarified?

Sorry NeoPa i dont know what to say, im just not sure what you mean?
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,662
#12: 4 Weeks Ago

re: Link Tables in VB


Quote:

Originally Posted by Dan2kx View Post

Sorry NeoPa i dont know what to say, im just not sure what you mean?

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.
Needs Regular Fix
 
Join Date: Oct 2007
Location: Hull,UK
Posts: 284
#13: 4 Weeks Ago

re: Link Tables in VB


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
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,662
#14: 3 Weeks Ago

re: Link Tables in VB


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's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,662
#15: 3 Weeks Ago

re: Link Tables in VB


Quote:

Originally Posted by NeoPa View Post

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
Reply