Hi:
Some code for this --
Here --
strShadow is the current Database Path (Database you are working on)
strLinkedPath is the Linked Database Path - database you want to link to -
for you this is "C:\linkedTable\source2.dbf"
tblName is the Name of source table - this is table in linked database you
want to link to
newtblName is the Name of the table in current database which is linked to
tblName (this is normally same as tblName)
strLinkPwd is an Optional argument - specifiy if source database has a
password
Public Function LinkTable(strShadow As String, strLinkedPath As String,
tblName As String, newtblName As String, Optional strLinkPwd As String) As
Boolean
'NN - 7/27
'returns True if successfull
On Error GoTo errHandler
Dim db As DAO.Database
Dim tdfLinked As DAO.TableDef
Dim visPath As String
visPath = modGlobal.strLocalFolder
Set db = Workspaces(0).OpenDatabase(strShadow, False, False, ";PWD=" &
CONPWDE)
Set tdfLinked = db.CreateTableDef(newtblName)
tdfLinked.Connect = "MS Access;DATABASE=" & strLinkedPath & IIf(strLinkPwd
<> "", ";PWD=" & strLinkPwd, "")
tdfLinked.SourceTableName = tblName
On Error Resume Next
db.TableDefs.Delete newtblName
On Error GoTo errHandler
db.TableDefs.Append tdfLinked
LinkTable = True
db.Close
Set db = Nothing
Exit Function
errHandler:
Call modGlobal.GenericErrorHandler("LinkTable", Err.Description, Err.Number)
LinkTable = False
End Function
Regards,
Naresh Nichani
Microsoft Access MVP
> Hi,
>
[quoted text clipped - 21 lines]
>
> Thank you in advance.
>I have question with regards to link tables.
>
[quoted text clipped - 17 lines]
>Oh and by the way I am using Access 97. (not my fault but the people asking me
>this project are still with that version).
Hey, there's nothing wrong (and a lot that's right) about
A97.
Use DAO to change the TableDef's SourceTableName property.
You should also use the TableDef's RefreshLink method after
changing the TableDef object.

Signature
Marsh
MVP [MS Access]
felixc36@yahoo.com - 09 Feb 2005 17:07 GMT
I think I am close but still have problem changing the link to a link table.
Below is my code where the Sub Create_Connection creates a new linked table
and works fine.
The problem is that when i try to modify the SourceTableName property i get an
error message "Cannot set this propertyonce the object is part of a collection
(3268)"
Basically what I want to do is change the table "Linked_Table1" 's source to
read the rile AG151.dbf instead of AG102.dbf.
Here is my code and it's made in Access 97.
Regards
Sorry Marshal if I sent a previous message to your E-Mail I clicked on the
wrong button on my newsreader instead of posting it I accidentally sent it to
your E-Mail.....my mistake
Sub Create_Connection()
Dim db As Database
Dim t As TableDef
Set db = CurrentDb
Set t = db.CreateTableDef("Linked_Table1")
With t
.Connect = "dBase IV;DATABASE=C:\Access_Portfolio\test"
.SourceTableName = "ag102.dbf"
End With
db.TableDefs.Append t
End Sub
Sub change_connection()
Dim db As Database
Dim tbl As TableDef
Dim rst As Recordset
Set db = CurrentDb
Set tbl = db.TableDefs("Linked_Table1")
With tbl
.Connect = "dBase IV;DATABASE=C:\Access_Portfolio\test"
.SourceTableName = "ag151.dbf"
End With
End Sub
>>I have question with regards to link tables.
>>
[quoted text clipped - 26 lines]
>You should also use the TableDef's RefreshLink method after
>changing the TableDef object.
Marshall Barton - 09 Feb 2005 19:00 GMT
Sorry, I forgot that the SourceTableName could not be
changed once the tabledef was added to the tabledefs
collection.
You'll have to delete the old tabledef and create a new one.
Not too big a deal, since you can retrieve the relevant
properties from the old one, delete it, create a new one and
set t--
Marsh
MVP [MS Access]
>I think I am close but still have problem changing the link to a link table.
>
[quoted text clipped - 41 lines]
>End With
>End Sub