Results 1 to 3 of 3
  1. #1
    jaryszek is offline Expert
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2016
    Posts
    568

    Loop through tables in separate database - postgresql/ms sql server/others

    Hi,



    I would like to import all tables from external database using VBA.

    My present code:

    Code:
    Function RemoveAndConnectToOdbcTables()
    
    
    Dim username, pass, strConnect, strConnectQuery As String
    Dim i As Long
    
    
    '''You can use a pass-through query to list the table names from your SQL Server database. Open a recordset based on that query. Then loop through the recordset rows and link each table.
    On Error GoTo ErroHandler
    
    
    strConnect = ConnSringFunction
    ''username = "admin"
    ''pass = "PasswordAccess"
    
    
     DoCmd.Hourglass True
        Dim db As DAO.Database
        Dim tdef As TableDef
        Dim qdef As QueryDef
        Dim missingTbl() As String
        Dim upper As Integer
        Dim strMsg As String
        Dim tdfCurrent As TableDef
        Dim TableName As String
        Dim CollTables As New Collection
        Dim DicTables As Object
        Dim CollItem As Variant
    
    
        Set DicTables = CreateObject("Scripting.dictionary")
    
      ''Here i am retriving list of all tables from database as collection
        Set CollTables = SetCollTables(strConnect)
        
        strConnect = "ODBC;" & strConnect
    
    '' I am looping through present database and based on that i am creating dictionary with full list of tables
        Set db = CurrentDb
        For i = 0 To CurrentDb.TableDefs.Count - 1
            Set tdef = db.TableDefs(i)
            If Len(tdef.Connect) > 0 And Left(tdef.Name, 1) = "t" And tdef.Name <> "t_currentschema" And tdef.Name <> "t_schemas" Then
                DicTables.Add tdef.Name, 1
                DoCmd.DeleteObject acTable, tdef.Name
            End If
        Next i
    
    
        Set tdef = Nothing
    
    ''Here based on added tables into dictionary i am creating new tables. 
    
    
        For Each CollItem In CollTables
    
    
            If DicTables.exists(CollItem) Then
                Set tdfCurrent = db.CreateTableDef(CollItem)
                tdfCurrent.Connect = strConnect
                tdfCurrent.SourceTableName = CollItem
                db.TableDefs.Append tdfCurrent
                db.TableDefs.Refresh
            End If
    
    
        Next CollItem
    
    
        For Each qdef In CurrentDb.QueryDefs
        Debug.Print qdef.Name
            If qdef.Type = dbQSQLPassThrough Then
                qdef.Connect = strConnect
            End If
        Next qdef
    
    
        DoCmd.Hourglass False
    
    
    Exit Function
    
    
    
    
    ErroHandler:
    MsgBox "Error number is: " & Err.Number & "; " & Err.Description
    DoCmd.Hourglass False
    
    
     End Function
    In code above i am using present linked tables in database to append new linked table based on existing ones.
    I want to:
    1. Delete all existing linked tables
    2. Entirely linked to database and create new linked tables from there

    Please help,
    Jacek

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Hi Jacek,

    I think you over complicate it. I use a local Access table (hlpTables) where I list all my linked tables from various databases. Then in a loop I check for len(tdf.Connect)>0 and delete all linked tables followed by another loop based on hlpTables where I link them back.
    Click image for larger version. 

Name:	hlpTables.PNG 
Views:	13 
Size:	4.8 KB 
ID:	34658

    Cheers,
    Vlad

  3. #3
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @jaryszek

    In your code, you have
    Code:
    Function RemoveAndConnectToOdbcTables()
    
    
    Dim username, pass, strConnect, strConnectQuery As String
    Dim i As Long
    Be aware that strConnectQuery is declared as a String and username, pass and strConnect are declared as variants.

    If you want all 4 to be declared as strings, you MUST explicitly declare each variable as a string type.
    Code:
    Function RemoveAndConnectToOdbcTables()
    
    
    Dim username As String, pass As String, strConnect As String, strConnectQuery As String
    Dim i As Long

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 05-23-2018, 09:14 AM
  2. Replies: 2
    Last Post: 03-23-2018, 07:54 AM
  3. Replies: 13
    Last Post: 06-06-2017, 01:43 PM
  4. Linking tables to mdb database on extern server
    By mayja in forum Import/Export Data
    Replies: 1
    Last Post: 11-23-2016, 08:57 AM
  5. Linking access database to tables in sql server is enough?
    By masoud_sedighy in forum SQL Server
    Replies: 6
    Last Post: 01-31-2012, 07:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums