How to Set tdf = dbs.TableDefs("YourTableName") for more than two tables

Hi Expert!

Just a small help!

I have seen a code for SQL table re-linker here see below:

Dim sConnect As String
sConnect = "ODBC;DRIVER={sql server};DATABASE=YourDatabaseName;SERVER=YourServerName;Trusted_Connection=Yes;"

Dim tdf As DAO.TableDef
Dim dbs As DAO.Database

Set dbs = CurrentDB
Set tdf = dbs.TableDefs("YourTableName")

tdf.Connect = sConnect

Now I have 64 tables to be re-linked as you can see above there is one provision for a table only(Set tdf = dbs.TableDefs("YourTableName")
), then how do I accommodate all the 64 tables there on MS Access start-up form Open event??????????


Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:

Do you have a list of the names of the 64 tables?

Is there a way to identify the 64 tables you want to re-link?

If you had the former you could loop through the list setting tdf to the TableDef for each table as you go.

If the latter was true you could loop through the entire TableDefs collection and only re-link the ones you want.
Fabrice LambertConsultingCommented:

You'll need to loop trough all tabledefs, and check the Connect property to know if it is a linked table or not:
Dim db As DAO.Database
Set db = CurrentDb

Dim tdf as DAO.TableDef
For Each tdf in db.TableDefs
    If (tdf.Connect <> vbNullString) Then
        tdf.Connect = ""ODBC;DRIVER={sql server};DATABASE=YourDatabaseName;SERVER=YourServerName;Trusted_Connection=Yes;"
    End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.