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!

Crude table re-linking at runtime

Status
Not open for further replies.

ahmun

IS-IT--Management
Jan 7, 2002
432
US
The following tip provides the basic concepts for linking a table at runtime in Access 2000. I developed this in response to needing to re-connect to a back-end Access Database when the front end was distributed as Access Runtime(thus limiting the user to do any editing of the database).

This tip is broken into 3 different sections:
1. Get path and file name of Database to link to.
2. Use the TransferDatabase method to link a table.
3. Sources

note: this code needs to be modified before reuse.
note to tek-tips moderators: I could not post to the FAQ section. There seems to be some kind of code error when I click on "Preview FAQ" button.


1. Get path and file name of Database to link to.
This uses the API for a general open file dialog box in Windows. The Function PromptFileName returns a string that is the full Windows path and file name of the file the user selected through the open file dialog box. The coding and logic comes from the following website: where the original writer offers the code for free provided his name is cited.


Code:
Code:
' Parts of this code are liscensend and the information can be found
Code:
Code:
' on the following web page:
Code:
Code:
' [URL unfurl="true"]http://www.vbapi.com/ref/g/getopenfilename.html[/URL]
Code:
Code:
' Declarations and such needed for the example:
Code:
Code:
' (Copy them to the (declarations) section of a module.)
Code:
Public Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustomFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_PATHMUSTEXIST = &H800
Public Declare Function GetOpenFileName Lib "comdlg32.dll" _
    Alias "GetOpenFileNameA" (lpofn As OPENFILENAME) As Long

Public Function PromptFileName() As String
    Dim filebox As OPENFILENAME
Code:
' open file dialog structure
Code:
    Dim fname As String
Code:
' filename the user selected
Code:
    Dim result As Long
Code:
' result of opening the dialog
Code:
Code:
' Configure how the dialog box will look
Code:
    With filebox
Code:
' Size of the structure.
Code:
        .lStructSize = Len(filebox)
Code:
' Handle to window opening the dialog.
Code:
        .hwndOwner = 0 'Me.Hwnd
Code:
' Handle to calling instance (not needed).
Code:
        .hInstance = 0
Code:
' File filters to make available: Access Databases and All Files
Code:
        .lpstrFilter = "Access Databases (*.mdb)" & vbNullChar & "*.mdb" & _
            vbNullChar & "All Files (*.*)" & vbNullChar & "*.*" & _
            vbNullChar & vbNullChar
Code:
'.lpstrCustomFilter is ignored -- unused string
Code:
        .nMaxCustomFilter = 0
Code:
' Default filter is the first one (Text Files, in this case).
Code:
        .nFilterIndex = 1
Code:
' No default filename.  Also make room for received
Code:
Code:
' path and filename of the user's selection.
Code:
        .lpstrFile = Space(256) & vbNullChar
        .nMaxFile = Len(.lpstrFile)
Code:
' Make room for filename of the user's selection.
Code:
        .lpstrFileTitle = Space(256) & vbNullChar
        .nMaxFileTitle = Len(.lpstrFileTitle)
Code:
' Initial directory is C:\.
Code:
        .lpstrInitialDir = "C:\" & vbNullChar
Code:
' Title of file dialog.
Code:
        .lpstrTitle = "Select a File" & vbNullChar
Code:
' The path and file must exist; hide the read-only box.
Code:
        .flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
Code:
' The rest of the options aren't needed.
Code:
        .nFileOffset = 0
        .nFileExtension = 0
Code:
'.lpstrDefExt is ignored -- unused string
Code:
        .lCustData = 0
        .lpfnHook = 0
Code:
'.lpTemplateName is ignored -- unused string
Code:
    End With
Code:
' Display the dialog box.
Code:
    result = GetOpenFileName(filebox)
    If result <> 0 Then
Code:
' Remove null space from the file name.
Code:
        fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
Code:
'Debug.Print &quot;The selected file: &quot;; fname
Code:
    End If
Code:
'return the string of the file name
Code:
    PromptFileName = fname
    
End Function

2. Use the TransferDatabase method to link a table. (See Access Help for the specifics on this method) This is the crude method. This part involves capturing the names of the tables in the Database, Deleting their links, and re-linking them with the file chosen in step 1.

note: step 2 is in no way optimized nor does it do any checking. It literally grabs all table names, deletes them, and assumes that the back-end selected by the user has all the same tables that were deleted. If you have a mix of internal tables and linked tables, use your own method of findng table names other than the loop shown below.

Code:
Private Sub cmdLinkTables_Click()
On Error GoTo Err_cmdLinkTables_Click

    Dim strFileName, strTableName As String
    
    strFileName = PromptFileName()
    
    Dim obj As AccessObject, dbs As Object
    
    Set dbs = Application.CurrentData
Code:
' Search for open AccessObject objects in AllTables collection.
Code:
    For Each obj In dbs.AllTables
        strTableName = obj.Name
Code:
'Some other objects in the .AllTables Collection are not tables
Code:
        If Not (Left(strTableName, 4) = &quot;MSys&quot;) Then
Code:
'1. Delete the current link
Code:
            DoCmd.DeleteObject acTable, strTableName
Code:
'This MsgBox was used to debug.  Comment out or delete as you like
Code:
Code:
'2. Re-Link the table
Code:
            MsgBox &quot;Linking &quot; & strTableName & &quot;.&quot;
            DoCmd.TransferDatabase acLink, &quot;Microsoft Access&quot;, strFileName, _ 
                acTable, strTableName, strTableName
        End If
    Next obj

Exit_cmdLinkTables_Click:
    Exit Sub

Err_cmdLinkTables_Click:
    MsgBox Err.Description
    Resume Exit_cmdLinkTables_Click

End Sub

3. Sources
[ul][li]Threads I found useful and led me to the solutions: thread700-194277, and thread700-93254 [/li]
[li]Microsoft's version (works for Access 97): [/li]
[li]Download page for Microsoft's solutions (solutions.mdb): [/li]
[li]Source Code Example for solution in Access 2000: [/li]
[/ul] Earnie Eng - Newbie Access Programmer/DBA
If you are born once, you will die twice.
If you are born twice, you will die once
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top