loop through TableDefs? (DSNless connection) (1 Viewer)

madEG

Registered User.
Local time
Today, 08:05
Joined
Jan 26, 2007
Messages
309
Hello,

Trying to finally learn DSNless connections... :)

This works fine, to connect me to a single named table:

Code:
     '####### DSNless Connection #######
    Dim sConnect As String
    sConnect = "ODBC;DRIVER={sql server};DATABASE=PrioPlansDB3;SERVER=Alex;Trusted_Connection=Yes;"

    Dim tdf As DAO.TableDef
    Dim db As DAO.Database

    Set db = CurrentDb
    Set tdf = db.TableDefs("dbo_AppUsers")

    tdf.Connect = sConnect
    tdf.RefreshLink

    Set tdf = Nothing
    Set db = Nothing
...but when i wanted to loop through the various linked tables, connecting all of them, and try the below:

Code:
    '####### DSNless Connection #######
    Dim sConnect As String
    sConnect = "ODBC;DRIVER={sql server};DATABASE=PrioPlansDB3;SERVER=Alex;Trusted_Connection=Yes;"

    Dim tdf As DAO.TableDef
    Dim db As DAO.Database

    Set db = CurrentDb
    
    For Each tdf In db.TableDefs
        ' ignore system and temp tables
        If Not (tdf.Name Like "msys" Or tdf.Name Like "~*") Then
            Set tdf = tdf.Name
            tdf.Connect = sConnect
            tdf.RefreshLink
        End If
    Next
I get a "Compile error: Type Mismatch" on the open form event where this is being run from...

Can someone push me in the right direction? I thought this would work...?

Thanks!
-Matt G.
 

madEG

Registered User.
Local time
Today, 08:05
Joined
Jan 26, 2007
Messages
309
Doh!

removed:
Code:
Set tdf = tdf.Name

...and I'm good to go.

Thanks anyway!
 

madEG

Registered User.
Local time
Today, 08:05
Joined
Jan 26, 2007
Messages
309
Oh, and for the record, I had to edit the IF line to include an wildcard:

Code:
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") Then
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:05
Joined
May 7, 2009
Messages
19,245
you add another test on your code:

If (Not (tdf.Name Like "msys" Or tdf.Name Like "~*")) And Instr(tdf.Connect,"DATABASE")>0 Then

in case there are local access table in your db, it will be excluded.
 

madEG

Registered User.
Local time
Today, 08:05
Joined
Jan 26, 2007
Messages
309
Thank you! You're absolutely right! - I had a 'Paste Errors" table created when messing with data and suddenly I couldn't through the line:

Code:
tdf.Connect = sConnect

without getting an error 3219...

It was the local table attempted to being connected to!

Did I already say thanks? :)
 

Users who are viewing this thread

Top Bottom