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
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