Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

refresh link is making a duplicate copy of the tables 1

Status
Not open for further replies.

Lhuffst

Programmer
Jun 23, 2003
503
US
I was given the following code to relink a FE/BE. Instead of refreshing the link, it creates a new table with a 1 in the name. I am using Access 2010
Before running the function I have
Table1a
after running the function I have
Table1a
Table1a1

I commented out the code and added a refresh statement but that gives me an error.
What I need is for the existing tables to be relinked but if there is a new on that was not linked before, it should link it as well.
I did not write this code and will be the first to say I am not sure why/what to change. I've looked at several samples but they are way over my skill level at this time. Can someone help please
Code:
Private Sub AttachBtn_Click()
    Dim OutSide_db As Database
    Dim tdf As TableDef
    Dim errCnt, TblCnt As Integer
        
    On Error GoTo err
    
    If isBlank(cboAttachTo) Then
       MsgBox "You must enter an ACCDB to import the file from!" & vbNewLine & _
          "Try Again..", , "Warning"
       Exit Sub
    End If
       
    If isBlank(Dir(cboAttachTo)) Then
       MsgBox "Your file, " & cboAttachTo & " was not found!" & vbNewLine & _
          "Try Again..", , "Warning"
       Exit Sub
    End If
    
    If right(cboAttachTo, 6) <> ".accdb" Then
       cboAttachTo = cboAttachTo & ".accdb"
       
       
    End If
    
    Set OutSide_db = OpenDatabase(cboAttachTo)

    For Each tdf In OutSide_db.TableDefs
'    For Each tdf In db.TableDefs
       ' Do not link system tables.
        If Not Left(tdf.Name, 4) = "Msys" Then
           nill = SysCmd(acSysCmdSetStatus, "ReLinking table:  " & tdf.Name)
           ' Deleting the attached tables....
            db.TableDefs.Refresh tdf.Name
           'db.TableDefs.Delete tdf.Name    [blue] original line [/blue]
           Debug.Print tdf.Name
           ' ReLinking the tables...
           DoCmd.TransferDatabase acLink, "Microsoft Access", _
               cboAttachTo, acTable, tdf.Name, tdf.Name
           TblCnt = TblCnt + 1
        End If
getNextOne:
    Next tdf
    
    OutSide_db.Close
    
    nill = SysCmd(acSysCmdSetStatus, "Finished relinking " & TblCnt & " tables from " & cboAttachTo)
    
    Exit Sub
    
err:

   If err.Number = 3265 Then 'If trying to delete a table that does not exist.
      If fraWhichTables = 1 Then   ' If getting only tables in this MDB.
         GoTo getNextOne           ' Skip linking this table and get next one to check.
      Else
         Resume Next               ' Getting all of the records from other MDB
      End If
   End If
   

'Stop     [red] originally was hitting this but I don't think I need it so commented it out [/red]

   If errCnt > 25 Then
     ' nill = AddErrorLog(Err.Number, Err.Description, "Attaching Tables: AttachBtn.Click", "Cannot Attach tables from the Attachment Form")
      DoCmd.Hourglass False
      MsgBox "Cannot Attach the tables: " & Error
      Exit Sub
   Else
      Resume Next
      errCnt = errCnt + 1
   End If
    
End Sub
 
that worked great for the tables that were already linked. How do I handle if a new table is added to the backend but hasn't been linked to the front end.
 
You should be able to determine if a table hasn't been linked with something like:

Code:
[COLOR=#4E9A06]
'See if the table is already linked and if not, create the link[/color]
If DCount("*","MsysObjects","[Name]='" & tdef.Name & "' AND [Type] = 6 ") = 0 Then
    DoCmd.TransferDatabase acLink, "Microsoft Access", _
        cboAttachTo, acTable, tdf.Name, tdf.Name
End If

Duane
Hook'D on Access
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top