SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Linked Tables

  1. #1
    SitePoint Member
    Join Date
    Oct 2003
    Location
    UK
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Linked Tables

    I am writing an ASP internet application using access 97.

    The problem I have is that the goalposts have been moved (yet again!) and the database that was one, may have to become many, being used on subdomains of the same server sharing some information but not all from a 'global' database.

    The solution I initially found was to used linked tables and developing it on pws - this was perfect. I linked local databases to the external table data in the 'global' database hey presto the application ran smoothly. There was only 1 connection to each local database and I could retrieve all the information I needed from the global one. However on uploading I have no idea how to set this via asp or sql as online the path of the linked tables is what was on my local machine and I have found no way of being able to edit this or indeed know if this is possible.

    Please could someone help with this or suggest an alternative direction to look for a solution. I realise I could do this with 2 separate connections with a bit of reworking of code but I would rather find a way with 1.

    thanks
    Stephen.

  2. #2
    SitePoint Guru asterix's Avatar
    Join Date
    Jun 2003
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have several options:
    1) Use UNC paths \\server\path
    2) Change the paths in the DB to what they should look like when the DB has been uploaded
    3) You can upload the DB, and open it from the remote location? Then you can run this code:

    http://www.mvps.org/access/tables/tbl0009.htm

    HTH, Richard

  3. #3
    SitePoint Guru asterix's Avatar
    Join Date
    Jun 2003
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and here is the code from that site here, including the copyright notice:
    Code:
    '***************** Code Start ***************
    ' This code was originally written by Dev Ashish.
    ' It is not to be altered or distributed,
    ' except as part of an application.
    ' You are free to use it in any application,
    ' provided the copyright notice is left unchanged.
    '
    ' Code Courtesy of
    ' Dev Ashish
    '
    Function fRefreshLinks() As Boolean
    Dim strMsg As String, collTbls As Collection
    Dim i As Integer, strDBPath As String, strTbl As String
    Dim dbCurr As DATABASE, dbLink As DATABASE
    Dim tdfLocal As TableDef
    Dim varRet As Variant
    Dim strNewPath As String
    Const cERR_USERCANCEL = vbObjectError + 1000
    Const cERR_NOREMOTETABLE = vbObjectError + 2000
    	On Local Error GoTo fRefreshLinks_Err
    	If MsgBox("Are you sure you want to reconnect all Access tables?", _
    			vbQuestion + vbYesNo, "Please confirm...") = vbNo Then Err.Raise cERR_USERCANCEL
    	'First get all linked tables in a collection
    	Set collTbls = fGetLinkedTables
    	'now link all of them
    	Set dbCurr = CurrentDb
    	strMsg = "Do you wish to specify a different path for the Access Tables?"
    	
    	If MsgBox(strMsg, vbQuestion + vbYesNo, "Alternate data source...") = vbYes Then
    		strNewPath = fGetMDBName("Please select a new datasource")
    	Else
    		strNewPath = vbNullString
    	End If
    	For i = collTbls.Count To 1 Step -1
    		strDBPath = fParsePath(collTbls(i))
    		strTbl = fParseTable(collTbls(i))
    		varRet = SysCmd(acSysCmdSetStatus, "Now linking '" & strTbl & "'....")
    		If Left$(strDBPath, 4) = "ODBC" Then
    			'ODBC Tables
    			'ODBC Tables handled separately
    		   ' Set tdfLocal = dbCurr.TableDefs(strTbl)
    		   ' With tdfLocal
    		   '	 .Connect = pcCONNECT
    		   '	 .RefreshLink
    		   '	 collTbls.Remove (strTbl)
    		   ' End With
    		Else
    			If strNewPath <> vbNullString Then
    				'Try this first
    				strDBPath = strNewPath
    			Else
    				If Len(Dir(strDBPath)) = 0 Then
    					'File Doesn't Exist, call GetOpenFileName
    					strDBPath = fGetMDBName("'" & strDBPath & "' not found.")
    					If strDBPath = vbNullString Then
    						'user pressed cancel
    						Err.Raise cERR_USERCANCEL
    					End If
    				End If
    			End If
    			'backend database exists
    			'putting it here since we could have
    			'tables from multiple sources
    			Set dbLink = DBEngine(0).OpenDatabase(strDBPath)
    			'check to see if the table is present in dbLink
    			strTbl = fParseTable(collTbls(i))
    			If fIsRemoteTable(dbLink, strTbl) Then
    				'everything's ok, reconnect
    				Set tdfLocal = dbCurr.TableDefs(strTbl)
    				With tdfLocal
    					.Connect = ";Database=" & strDBPath
    					.RefreshLink
    					collTbls.Remove (.Name)
    				End With
    			Else
    				Err.Raise cERR_NOREMOTETABLE
    			End If
    		End If
    	Next
    	fRefreshLinks = True
    	varRet = SysCmd(acSysCmdClearStatus)
    	MsgBox "All Access tables were successfully reconnected.", _
    			vbInformation + vbOKOnly, _
    			"Success"
    fRefreshLinks_End:
    	Set collTbls = Nothing
    	Set tdfLocal = Nothing
    	Set dbLink = Nothing
    	Set dbCurr = Nothing
    	Exit Function
    fRefreshLinks_Err:
    	fRefreshLinks = False
    	Select Case Err
    		Case 3059:
    		Case cERR_USERCANCEL:
    			MsgBox "No Database was specified, couldn't link tables.", _
    					vbCritical + vbOKOnly, _
    					"Error in refreshing links."
    			Resume fRefreshLinks_End
    		Case cERR_NOREMOTETABLE:
    			MsgBox "Table '" & strTbl & "' was not found in the database" & _
    					vbCrLf & dbLink.Name & ". Couldn't refresh links", _
    					vbCritical + vbOKOnly, _
    					"Error in refreshing links."
    			Resume fRefreshLinks_End
    		Case Else:
    			strMsg = "Error Information..." & vbCrLf & vbCrLf
    			strMsg = strMsg & "Function: fRefreshLinks" & vbCrLf
    			strMsg = strMsg & "Description: " & Err.Description & vbCrLf
    			strMsg = strMsg & "Error #: " & Format$(Err.Number) & vbCrLf
    			MsgBox strMsg, vbOKOnly + vbCritical, "Error"
    			Resume fRefreshLinks_End
    	End Select
    End Function
    Function fIsRemoteTable(dbRemote As DATABASE, strTbl As String) As Boolean
    Dim tdf As TableDef
    	On Error Resume Next
    	Set tdf = dbRemote.TableDefs(strTbl)
    	fIsRemoteTable = (Err = 0)
    	Set tdf = Nothing
    End Function
    Function fGetMDBName(strIn As String) As String
    'Calls GetOpenFileName dialog
    Dim strFilter As String
    	strFilter = ahtAddFilterItem(strFilter, _
    					"Access Database(*.mdb;*.mda;*.mde;*.mdw) ", _
    					"*.mdb; *.mda; *.mde; *.mdw")
    	strFilter = ahtAddFilterItem(strFilter, _
    					"All Files (*.*)", _
    					"*.*")
    	fGetMDBName = ahtCommonFileOpenSave(Filter:=strFilter, _
    								OpenFile:=True, _
    								DialogTitle:=strIn, _
    								Flags:=ahtOFN_HIDEREADONLY)
    End Function
    Function fGetLinkedTables() As Collection
    'Returns all linked tables
    	Dim collTables As New Collection
    	Dim tdf As TableDef, db As DATABASE
    	Set db = CurrentDb
    	db.TableDefs.Refresh
    	For Each tdf In db.TableDefs
    		With tdf
    			If Len(.Connect) > 0 Then
    				If Left$(.Connect, 4) = "ODBC" Then
    				'	collTables.Add Item:=.Name & ";" & .Connect, KEY:=.Name
    				'ODBC Reconnect handled separately
    				Else
    					collTables.Add Item:=.Name & .Connect, Key:=.Name
    				End If
    			End If
    		End With
    	Next
    	Set fGetLinkedTables = collTables
    	Set collTables = Nothing
    	Set tdf = Nothing
    	Set db = Nothing
    End Function
    Function fParsePath(strIn As String) As String
    	If Left$(strIn, 4) <> "ODBC" Then
    		fParsePath = Right(strIn, Len(strIn) _
    						- (InStr(1, strIn, "DATABASE=") + 8))
    	Else
    		fParsePath = strIn
    	End If
    End Function
    Function fParseTable(strIn As String) As String
    	fParseTable = Left$(strIn, InStr(1, strIn, ";") - 1)
    End Function
    '***************** Code End ***************

  4. #4
    SitePoint Member
    Join Date
    Oct 2003
    Location
    UK
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your response
    >1) Use UNC paths \\server\path
    Could you tell me more about this?

    >2) Change the paths in the DB to what they should look like when the DB has been uploaded

    I thought of this one too but how easy would it be to replicate the drive letter?

    >3) You can upload the DB, and open it from the remote location?

    The only access to the server I have is for ftp

  5. #5
    SitePoint Member
    Join Date
    Oct 2003
    Location
    UK
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have implemented your suggestion 2 for now and it is working fine thanks. It is something that may cause problems on new servers.

    Incidentally do you know how costly it is to have tables linked?
    I thought it would be better than having 2 physical connections to separate databases.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •