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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linked Table Manager and Network Drives

Status
Not open for further replies.

MC77

Technical User
Jul 24, 2001
8
0
0
US
I am developing an application using a database on a local drive(c:),without access to a network. The app is then deployed to the user, we use the Linked Table Manager process to connect the app to the backend database. This is currently functioning as a manual effort...but is there a way to include this link to the network database when I create the .mde and bypass the manual linking effort?
Thanx
 
Private Sub UpdateLinks()
On Error GoTo Err_Hand
Dim dbsRemote As Database
Dim dbsLocal As Database
Dim tdfNew As TableDef
Dim tbl As TableDef
Dim i As Integer
Set dbsRemote = OpenDatabase("M:ACCESS\NewDB.MDB") 'Your destination table
Set dbsLocal = CurrentDb
For i = 0 To dbsRemote.TableDefs.Count - 1
Set tdfNew = dbsLocal.CreateTableDef(dbsRemote.TableDefs(i).Name)
With tdfNew
.Name = dbsRemote.TableDefs(i).Name
.Connect = ";DATABASE=m:\Access\NewDB.MDB"
.SourceTableName = dbsRemote.TableDefs(i).Name
dbsLocal.TableDefs.Append tdfNew
End With
Next i
Err_Hand:
If Err.Number = 3012 Then '3012 = already exsists. You'll probably need better errir trapping to exclude Msys objects
Resume Next
Else
MsgBox Err.Number
End If
End Sub Tyrone Lumley
augerinn@gte.net
 
This does the same thing, but deletes each attached table first. It also checks against a constant gcUserDataFileNm, which is the path to a local db if you want to attach some user info. If all your back-end tables are in the same mdb, remove that line to check. It furthermore writes the name of the attached file to a table called utblSysAdmin, you can get rid of that part of the code, too (look for the rst stuff near the end).
Code:
Public Function Reattach(ByVal pstrFullFile As String) As Boolean
On Error GoTo Err_Reattach

    Dim tdfSource   As DAO.TableDef
    Dim tdfMe       As DAO.TableDef
    Dim rst         As DAO.Recordset
    Dim dbMe        As DAO.Database
    Dim dbSource    As DAO.Database
    Dim i           As Integer
    Dim bleLinkLocal    As Boolean

    Dim intCount    As Integer
    Dim varSysCmd   As Variant
    
    DoCmd.Hourglass True
    Reattach = True
    
    Set dbSource = OpenDatabase(pstrFullFile)
    Set dbMe = CurrentDb
    
    With dbMe
        For i = .TableDefs.Count - 1 To 0 Step -1
            Set tdfMe = .TableDefs(i)
            With tdfMe
                If (.Attributes And dbAttachedTable) = dbAttachedTable Then
                    If Right(.Connect, Len(gcUserDataFileNm)) <> gcUserDataFileNm Then
                        dbMe.TableDefs.Delete (.Name)
                    End If
                End If
            End With
        Next i
    End With
    
    dbMe.TableDefs.Refresh
    i = 0
    
    With dbSource
        varSysCmd = SysCmd(acSysCmdInitMeter, &quot;Linking tables from &quot; & pstrFullFile & &quot;...&quot;, .TableDefs.Count)
        For Each tdfSource In .TableDefs
            bleLinkLocal = False
            If Left(tdfSource.Name, 4) <> &quot;MSys&quot; Then
                For Each tdfMe In dbMe.TableDefs
                    If tdfMe.SourceTableName = tdfSource.Name Then
                        bleLinkLocal = True
                        Exit For
                    End If
                Next tdfMe
                Set tdfMe = Nothing
                If Not bleLinkLocal Then
                    Set tdfMe = dbMe.CreateTableDef(tdfSource.Name)
                    With tdfMe
                        .Connect = &quot;;Database=&quot; & dbSource.Name
                        .SourceTableName = tdfSource.Name
                    End With
                    dbMe.TableDefs.Append tdfMe
                    'Hide table:  tdf.Attributes = dbHiddenObject
                    tdfMe.RefreshLink
                End If
                i = i + 1
                varSysCmd = SysCmd(acSysCmdUpdateMeter, i)
            End If
        Next tdfSource
        
        Set rst = .TableDefs(&quot;utblSysAdmin&quot;).OpenRecordset(dbOpenTable)
        
        With rst
            .Edit
                !AttachFile = pstrFullFile
            .Update
            .Close
        End With
        
        .Close
    End With

Exit_Reattach:
    Set dbMe = Nothing
    Set tdfMe = Nothing
    Set tdfSource = Nothing
    varSysCmd = SysCmd(acSysCmdRemoveMeter)
    Application.Echo True
    DoCmd.Hourglass False
    Exit Function
    
Err_Reattach:
    Reattach = False
    If Err = 3011 Then
        If MsgBox(&quot;Table &quot; & tdfMe.Name & &quot; not found.  Would you like to &quot; _
         & &quot;permanently remove the link to this table?&quot;, vbYesNo + vbQuestion) = vbYes Then
            dbMe.TableDefs.Delete (tdfMe.Name)
        End If
        Resume Next
    Else
        Reattach = False
        Resume Exit_Reattach
    End If
    
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top