473,387 Members | 1,834 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,387 software developers and data experts.

Create a linked table in Access 2007 using VB

I have data in a few external Access tables that I want to link to my Access 2007 database. Depending on situation (and permissions of the person using the database) I want to dynamically link to one or more of the external tables when a specific user form opens. Then I want to delete the links when the form closes.

How does one go about creating and deleting a linked table using VB? I know the name of the database files and the tables I want to link but I'm not sure which objects & methods to use (so I don't know where to look in the the Access help file).

Could someone point me in the right direction? I don't need the complete code, just an object and/or method name so I know where to look in the help files. I've been digging for a couple of hours at this point and I'm pretty frustrated that I can't figure this one out on my own.

Thanks,
sphinney
Jun 24 '08 #1
5 10195
mshmyob
904 Expert 512MB
Here is some simple code to link your tables for Access. This is hard coded so you need to know in advance where your BE is to be located.

Expand|Select|Wrap|Line Numbers
  1. Function RefreshLinks() As Boolean
  2. Dim collTbls As Collection
  3. Dim i As Integer
  4. Dim strTbl As String
  5. Dim dbCurr As Database
  6. Dim dbLink As Database
  7. Dim tdfTables As TableDef
  8. Dim strBeFile As String
  9. Dim collTables As New Collection
  10. Dim tdf As TableDef
  11.  
  12. ' get the current linked table definitions
  13.     Set dbCurr = CurrentDb
  14.     dbCurr.TableDefs.Refresh
  15. ' end
  16.  
  17.     'First get all linked tables in a collection
  18.     For Each tdf In dbCurr.TableDefs
  19.         With tdf
  20.             If Len(.Connect) > 0 Then
  21.                     collTables.Add Item:=.Name & .Connect, Key:=.Name
  22.             End If
  23.         End With
  24.     Next
  25.     Set collTbls = collTables
  26.  
  27. ' change this string to your drive/directory/filename               
  28. strBeFile = "c:\any directory\backend file name.mdb"
  29.  
  30.             Set dbLink = DBEngine(0).OpenDatabase(strBeFile)
  31.  
  32.   ' start linking your tables - start from the last and work your way down
  33.      For i = collTbls.Count To 1 Step -1
  34.             strTbl = Left$(collTbls(i), InStr(1, collTbls(i), ";") - 1)
  35.                 Set tdfTables = dbCurr.TableDefs(strTbl)
  36.                 With tdfTables
  37.                     .Connect = ";Database=" & strBeFile
  38.                     .RefreshLink
  39.                 End With
  40.     Next
  41.    End Function
  42.  
To use this you must have at one time manually linked the tables. After that you can use this code to relink them anytime to any server/directory. Works with runtime also.

Hope this gets you started.

cheers,
Jun 24 '08 #2
Thank you! This certainly does get me started. Much obliged.

Here is some simple code to link your tables for Access. This is hard coded so you need to know in advance where your BE is to be located.

Expand|Select|Wrap|Line Numbers
  1. Function RefreshLinks() As Boolean
  2. Dim collTbls As Collection
  3. Dim i As Integer
  4. Dim strTbl As String
  5. Dim dbCurr As Database
  6. Dim dbLink As Database
  7. Dim tdfTables As TableDef
  8. Dim strBeFile As String
  9. Dim collTables As New Collection
  10. Dim tdf As TableDef
  11.  
  12. ' get the current linked table definitions
  13.     Set dbCurr = CurrentDb
  14.     dbCurr.TableDefs.Refresh
  15. ' end
  16.  
  17.     'First get all linked tables in a collection
  18.     For Each tdf In dbCurr.TableDefs
  19.         With tdf
  20.             If Len(.Connect) > 0 Then
  21.                     collTables.Add Item:=.Name & .Connect, Key:=.Name
  22.             End If
  23.         End With
  24.     Next
  25.     Set collTbls = collTables
  26.  
  27. ' change this string to your drive/directory/filename               
  28. strBeFile = "c:\any directory\backend file name.mdb"
  29.  
  30.             Set dbLink = DBEngine(0).OpenDatabase(strBeFile)
  31.  
  32.   ' start linking your tables - start from the last and work your way down
  33.      For i = collTbls.Count To 1 Step -1
  34.             strTbl = Left$(collTbls(i), InStr(1, collTbls(i), ";") - 1)
  35.                 Set tdfTables = dbCurr.TableDefs(strTbl)
  36.                 With tdfTables
  37.                     .Connect = ";Database=" & strBeFile
  38.                     .RefreshLink
  39.                 End With
  40.     Next
  41.    End Function
  42.  
To use this you must have at one time manually linked the tables. After that you can use this code to relink them anytime to any server/directory. Works with runtime also.

Hope this gets you started.

cheers,
Jun 24 '08 #3
mshmyob
904 Expert 512MB
You are welcome. Good luck.

cheers,

Thank you! This certainly does get me started. Much obliged.
Jun 25 '08 #4
neelsfer
547 512MB
where do you copy this code to? how do you link it to button
Oct 10 '10 #5
mshmyob
904 Expert 512MB
You can put the code anywhere. The way I have shown it was putting it in a function in the form and then calling the function with the OnClick event of the button.

You could also put it in a module and call it that way or you can remove the 1st and last line (the function lines) and just put the whole code in the OnClick event of the button.

cheers,
Oct 11 '10 #6

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

Similar topics

0
by: Chris Powell | last post by:
I am using Excel/Access 2000 and have two large Excel files (25,000 rows each) that I wish to create linked tables in Access rather than importing into Access. The two source Excel files change...
4
by: intl04 | last post by:
How do I create a data input form in Access that is external to the Access database to which it's connected (if that's possible, which I believe it is)? For example, if someone clicks on an Access...
4
by: Wayne Wengert | last post by:
I am trying to create a VB.NET Windows application to move some data from a local Access DB table to a table in a SQL Server. The approach I am trying is to open an OLEDB connection to the local...
11
by: AustinClark | last post by:
During login and logout, the workstations on my network dump some info into a text file. Part of this info is the current date and time, rendered by doing "echo %date%,%time%" at the command line. ...
1
by: Dalien51 | last post by:
Has anyone else encountered this? I wrote a Access 2000 database which has a linked table to a HTML file which worked perfectly. However, I Have recently installed Access 2007 and now when I use...
13
by: Daedalus | last post by:
Ok I'm once again showing my complete (or almost) ineptitude with all things Access. I'm using Access 2007 and need to do the following: SETUP: I have a table of membership information, and...
5
by: Neil | last post by:
I'm running Access 2000 with a SQL 7 back end, using ODBC linked tables in an MDB file. The db is used by about 30 users on a LAN, and an additional 10 or so on a WAN. Recently, one of the WAN...
3
by: ARC | last post by:
I'm having trouble here with Access 2007 and connecting to a different database. The code below works great IF the previous back-end database connection is still present and you are trying to...
6
tuxalot
by: tuxalot | last post by:
I've tested many ways to accomplish this, but none of the code I've found works quite right. I am using the code written by Dev Ashish but it replicates some of the functionality I already have. ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.