Linked tables - changing the link 
Author Message
 Linked tables - changing the link

I am getting a strange error when trying to re-link tables using VBA. If I
address a table definition explicitly and individually I can alter the
connect property to point to a new database. However if I try and loop
through each table definition using a 'For Each' statement  I get an
'Invalid operation' error on the tdf.connect = line.

The following works fine;

Public Sub Link_Table(strClient As String)
Dim dbs As Database, tdf As TableDef
Dim strPath As String
Dim strFile As string
strPath = ";Database=h:\Data base\"
strFile = strPath & "HMS_" & strClient & ".mdb"
Set dbs = CurrentDb
Set tdf = dbs.TableDefs("tblWaves")

tdf.Connect = strPath & strFile
tdf.RefreshLink

Set dbs = Nothing

End Sub

however the following comes up with an 'Invalid Operation' error on the
'tdf.connect=.....' line.

Public Sub Link_Tables(strClient As String)
Dim dbs As Database, tdf As TableDef, lngFlag As Long
Dim strTblname As String
Dim strPath As String
Dim strFile As String

Set dbs = CurrentDb

strPath = ";Database=h:\Data base\"
strFile = "HMS_" & strClient & ".mdb"

For Each tdf In dbs.TableDefs
        tdf.Connect = ";Database=H:\data base\" & strFile
        tdf.RefreshLink
Next tdf

Set dbs = Nothing
End Sub

What am I doing wrong?



Mon, 29 Oct 2001 03:00:00 GMT  
 Linked tables - changing the link
Are all of your tables linked in the db, what happens when the connect
property does not exist because it is not linked table?

Justa guess/


Quote:
> I am getting a strange error when trying to re-link tables using VBA. If I
> address a table definition explicitly and individually I can alter the
> connect property to point to a new database. However if I try and loop
> through each table definition using a 'For Each' statement  I get an
> 'Invalid operation' error on the tdf.connect = line.

> The following works fine;

> Public Sub Link_Table(strClient As String)
> Dim dbs As Database, tdf As TableDef
> Dim strPath As String
> Dim strFile As string
> strPath = ";Database=h:\Data base\"
> strFile = strPath & "HMS_" & strClient & ".mdb"
> Set dbs = CurrentDb
> Set tdf = dbs.TableDefs("tblWaves")

> tdf.Connect = strPath & strFile
> tdf.RefreshLink

> Set dbs = Nothing

> End Sub

> however the following comes up with an 'Invalid Operation' error on the
> 'tdf.connect=.....' line.

> Public Sub Link_Tables(strClient As String)
> Dim dbs As Database, tdf As TableDef, lngFlag As Long
> Dim strTblname As String
> Dim strPath As String
> Dim strFile As String

> Set dbs = CurrentDb

> strPath = ";Database=h:\Data base\"
> strFile = "HMS_" & strClient & ".mdb"

> For Each tdf In dbs.TableDefs
>         tdf.Connect = ";Database=H:\data base\" & strFile
>         tdf.RefreshLink
> Next tdf

> Set dbs = Nothing
> End Sub

> What am I doing wrong?



Mon, 29 Oct 2001 03:00:00 GMT  
 Linked tables - changing the link
I believe your problem stems from not instantiating your tabledef object.
In your first example you "SET" the tabledef object, now your tdf variable
is assigned an object reference.  You don't do that with your For each
example.

Try instantiating your tdf variable like this

Dim tdf As New Tabledef

This might do the trick.

Kevin Kraus


Quote:
> I am getting a strange error when trying to re-link tables using VBA. If I
> address a table definition explicitly and individually I can alter the
> connect property to point to a new database. However if I try and loop
> through each table definition using a 'For Each' statement  I get an
> 'Invalid operation' error on the tdf.connect = line.

> The following works fine;

> Public Sub Link_Table(strClient As String)
> Dim dbs As Database, tdf As TableDef
> Dim strPath As String
> Dim strFile As string
> strPath = ";Database=h:\Data base\"
> strFile = strPath & "HMS_" & strClient & ".mdb"
> Set dbs = CurrentDb
> Set tdf = dbs.TableDefs("tblWaves")

> tdf.Connect = strPath & strFile
> tdf.RefreshLink

> Set dbs = Nothing

> End Sub

> however the following comes up with an 'Invalid Operation' error on the
> 'tdf.connect=.....' line.

> Public Sub Link_Tables(strClient As String)
> Dim dbs As Database, tdf As TableDef, lngFlag As Long
> Dim strTblname As String
> Dim strPath As String
> Dim strFile As String

> Set dbs = CurrentDb

> strPath = ";Database=h:\Data base\"
> strFile = "HMS_" & strClient & ".mdb"

> For Each tdf In dbs.TableDefs
>         tdf.Connect = ";Database=H:\data base\" & strFile
>         tdf.RefreshLink
> Next tdf

> Set dbs = Nothing
> End Sub

> What am I doing wrong?



Mon, 29 Oct 2001 03:00:00 GMT  
 Linked tables - changing the link
That's a good point.

Not all tabledef objects have the Connect property.

Kevin Kraus


Quote:
> Are all of your tables linked in the db, what happens when the connect
> property does not exist because it is not linked table?

> Justa guess/



> > I am getting a strange error when trying to re-link tables using VBA. If
I
> > address a table definition explicitly and individually I can alter the
> > connect property to point to a new database. However if I try and loop
> > through each table definition using a 'For Each' statement  I get an
> > 'Invalid operation' error on the tdf.connect = line.

> > The following works fine;

> > Public Sub Link_Table(strClient As String)
> > Dim dbs As Database, tdf As TableDef
> > Dim strPath As String
> > Dim strFile As string
> > strPath = ";Database=h:\Data base\"
> > strFile = strPath & "HMS_" & strClient & ".mdb"
> > Set dbs = CurrentDb
> > Set tdf = dbs.TableDefs("tblWaves")

> > tdf.Connect = strPath & strFile
> > tdf.RefreshLink

> > Set dbs = Nothing

> > End Sub

> > however the following comes up with an 'Invalid Operation' error on the
> > 'tdf.connect=.....' line.

> > Public Sub Link_Tables(strClient As String)
> > Dim dbs As Database, tdf As TableDef, lngFlag As Long
> > Dim strTblname As String
> > Dim strPath As String
> > Dim strFile As String

> > Set dbs = CurrentDb

> > strPath = ";Database=h:\Data base\"
> > strFile = "HMS_" & strClient & ".mdb"

> > For Each tdf In dbs.TableDefs
> >         tdf.Connect = ";Database=H:\data base\" & strFile
> >         tdf.RefreshLink
> > Next tdf

> > Set dbs = Nothing
> > End Sub

> > What am I doing wrong?



Mon, 29 Oct 2001 03:00:00 GMT  
 Linked tables - changing the link
Thanks this was it, the first table in the collection was not a linked
table, having excluded it everything works fine now.

Thanks

Quote:
> Are all of your tables linked in the db, what happens when the connect
> property does not exist because it is not linked table?

> Justa guess/



> > I am getting a strange error when trying to re-link tables using VBA. If
I
> > address a table definition explicitly and individually I can alter the
> > connect property to point to a new database. However if I try and loop
> > through each table definition using a 'For Each' statement  I get an
> > 'Invalid operation' error on the tdf.connect = line.

> > The following works fine;

> > Public Sub Link_Table(strClient As String)
> > Dim dbs As Database, tdf As TableDef
> > Dim strPath As String
> > Dim strFile As string
> > strPath = ";Database=h:\Data base\"
> > strFile = strPath & "HMS_" & strClient & ".mdb"
> > Set dbs = CurrentDb
> > Set tdf = dbs.TableDefs("tblWaves")

> > tdf.Connect = strPath & strFile
> > tdf.RefreshLink

> > Set dbs = Nothing

> > End Sub

> > however the following comes up with an 'Invalid Operation' error on the
> > 'tdf.connect=.....' line.

> > Public Sub Link_Tables(strClient As String)
> > Dim dbs As Database, tdf As TableDef, lngFlag As Long
> > Dim strTblname As String
> > Dim strPath As String
> > Dim strFile As String

> > Set dbs = CurrentDb

> > strPath = ";Database=h:\Data base\"
> > strFile = "HMS_" & strClient & ".mdb"

> > For Each tdf In dbs.TableDefs
> >         tdf.Connect = ";Database=H:\data base\" & strFile
> >         tdf.RefreshLink
> > Next tdf

> > Set dbs = Nothing
> > End Sub

> > What am I doing wrong?



Tue, 30 Oct 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Changing the linked table path without the linked table manager

2. Changing Linked Table link?

3. linking all fields in linked table?

4. To link procedure and table in visual linking...

5. Change Table Link programatically

6. With Code - CHANGE linked tables?? Sample

7. Changing linked table source by VBA

8. Changing Table Link Information

9. Linked table manager to be changed from (X:\) a server drive to (C:\) a local machine

10. Referential Integrity - changing in linked tables

11. Can I change the field size of a linked table

12. Changing Data Type while linking table to a CSV

 

 
Powered by phpBB® Forum Software