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

Linking 1 table in code 1

Status
Not open for further replies.

nickperez22

Technical User
Jun 13, 2001
62
US
I just created a new table in code, how do I link it in code to the front end. Table name "Titles
 
Hi Nick
Have the path of the connecting database ready and copy it into function in quotes
eg
Function ConnectToAccess("c:\mydocuments\db1")

copy the code below into a module eg Bas_Attach

call the function either from the immediate window or a command button

It returns an integer to validate successful completion
Code:
Function ConnectToAccess(DatabasePath As String) As Integer
On Error GoTo connectToAccesserr
    Dim tbl As TableDef
    Dim stablename As String
    
 
    For Each tbl In CurrentDb.TableDefs
        If tbl.Connect <> "" Then       ' Isn't a local table
            stablename = tbl.Name
            CurrentDb.TableDefs.Delete (stablename)
            DoCmd.TransferDatabase acLink, "Microsoft Access", _
                            DatabasePath, acTable, stablename, stablename
        End If
    Next
    
    CurrentDb.TableDefs.Refresh
   DoCmd.OpenForm "switchboard"

 
 
ConnectToAccess = -1
connecttoaccesserrexit:
Err = 0
Exit Function

connectToAccesserr:
MsgBox Error$(Err)
ConnectToAccess = 0
GoTo connecttoaccesserrexit


End Function

Hope this is what you want

regards

Jo
 
Thanks, but I'm having a slight problem and that is that I have 2 backend databases linked to the front end, so as soon as the function finds one link that is not in the database the funtion is pointing to I get an error.

In my case I do not need to refresh links or go through all the tables to re-establish links. I simply need the code to link this one table the front end after I have created it.
 
H Nick try this
Just copy the whole into your module
and try it by calling the sub from the immediate window

Code:
Sub linkOneTable()
dim stablename as string
        stablename = "Your Table name here"
        CurrentDb.TableDefs.Delete (stablename)
        DoCmd.TransferDatabase acLink, "Microsoft Access",  DatabasePath, acTable, stablename, stablename
CurrentDb.TableDefs.Refresh
end sub

regards

Jo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top