Results 1 to 5 of 5

Thread: [Resolved][Access 97] Deleting Linked tables

  1. #1

    Thread Starter
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Resolved [Resolved][Access 97] Deleting Linked tables

    Hi,

    I have access 97 db and i want to deleted the tables that are linked to another DB. the reason for this is i have the live DB and want to relink to a development dataset.

    The below code works however it has errors. the reason for the error is that the index of the table def changes once i have deleted a table.

    can anyone sugest how i can keep the index while deleting the linked tables

    Code:
    Public Sub delTableLinks()
    
        Dim db As Database
        Dim tdfNew As TableDef
        Dim i As Integer
        Dim fld As Field
        Dim j As Integer
    
        
    Set db = currentdb
        j = db.TableDefs.Count
        For i = 1 To j
            Set tdfNew = db.TableDefs(i)
                If Len(tdfNew.Connect) > 1 Then
                db.TableDefs.Delete (tdfNew.Name)
            End If
        Next
                
    End Sub
    Thanks in advance

    David
    Last edited by Davadvice; Nov 21st, 2007 at 08:57 AM. Reason: Resolved

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [Access 97] Deleting Linked tables

    vb Code:
    1. Public Sub delTableLinks()
    2.  
    3.     Dim db As Database
    4.     Dim tdfNew As TableDef
    5.     Dim i As Integer
    6.     Dim fld As Field
    7.     Dim j As Integer
    8.  
    9.    
    10. Set db = currentdb
    11.     j = db.TableDefs.Count
    12.     For i = 1 To j
    13.         Set tdfNew = db.TableDefs(i)
    14.             If Len(tdfNew.Connect) > 1 Then
    15.             db.TableDefs.Delete (tdfNew.Name)
    16.             If db.TableDefs.Count > 0 Then
    17.                 j=db.TableDefs.count
    18.             Else
    19.                 Exit Sub
    20.             End If
    21.         End If
    22.     Next
    23.            
    24. End Sub

  3. #3

    Thread Starter
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: [Access 97] Deleting Linked tables

    hi Max,

    thanks for the above code.

    unfortunetly The same problem still exists, the reason for it is the TableDefs.Count is reduced each time a table is deleted.

    I was thinking that I may need to create a new procedure to manage the indexes.


    Thanks

    David

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [Access 97] Deleting Linked tables

    This will probably work if you use a common trick for removing items from an array (like the list of a ListBox, etc), which is to loop downwards instead of upwards, eg:
    Code:
        For i = j To 1 Step -1

  5. #5

    Thread Starter
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: [Access 97] Deleting Linked tables

    Hello,

    Si that worked a treat, thanks

    Public Sub delTableLinks()
    'Created 20/11/07, This procedure is used to delete all linked tables from an access 97 DB
    'thanks to MaximilianMayrhofer and Si_The_Geek (VBforums.com) for their assistance in ironing out the errors.

    Code:
    Public Sub delTableLinksx()
    'Created 20/11/07, This procedure is used to delete all linked tables from an access 97 DB
    'thanks to MaximilianMayrhofer and Si_The_Geek (VBforums.com) for their assistance in ironing out the errors.
    
    Dim db As Database
    Dim tdfNew As TableDef
    Dim i As Integer
    Dim j As Integer
    
    Set db = currentdb
    
    j = db.TableDefs.Count - 1
    
     For i = j To 1 Step -1
        Set tdfNew = db.TableDefs(i)
        If Len(tdfNew.Connect) > 1 Then
            db.TableDefs.Delete (tdfNew.Name)
            If db.TableDefs.Count > 0 Then
                j = db.TableDefs.Count
            Else
                Exit Sub
            End If
        End If
    Next
    End Sub
    cheers

    David

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width