My Assistant
![]() ![]() |
|
|
Feb 23 2012, 09:43 PM
Post
#1
|
|
|
New Member Posts: 6 From: Adelaide, South Australia |
So I'm trying to migrate an existing database to a front end/back end model as part of having multiple user access.
Unfortunately the database creates a new table for each day it is used (very rarely as it is). I've managed to get the front end to create the new table in the backend and link it, however the other front ends don't see this new table. I tried the following code found from Google to automatically add all tables from a backed database, but it doesn't seem to work. I suspect it may be because it's looking for an mdb file, whereas my backend is an accdb... Thoughts? I realise this is a horrible way to do it and it makes me cry every time I have to patch stuff, but I'm not in the position where I can rewrite the application from scratch (the previous developer even has spaces and slashes in field names!) CODE '
'Link All Tables From Back End Database ' 'The function LinkTables creates links to all the tables in the remote database. 'The DbPath parameter should contain the complete path and database name. 'The subroutine CallLinkTables has sample code to call the function. Function LinkTables(DbPath As String) As Boolean 'This links to all the tables that reside in DbPath, whether or not they already reside in this database. 'This works when linking to an Access .mdb file, not to ODBC. 'This keeps the same table name on the front end as on the back end. Dim rs As Recordset On Error Resume Next 'get tables in back end database Set rs = CurrentDb.OpenRecordset("SELECT Name " & _ "FROM MSysObjects IN '" & DbPath & "' " & _ "WHERE Type=1 AND Flags=0") If Err <> 0 Then Exit Function 'link the tables While Not rs.EOF If DbPath <> Nz(DLookup("Database", "MSysObjects", "Name='" & rs!Name & "' And Type=6")) Then 'delete old link, assuming front and back end table have the same name DoCmd.DeleteObject acTable, rs!Name 'make new link DoCmd.TransferDatabase acLink, "Microsoft Access", DbPath, acTable, rs!Name, rs!Name End If rs.MoveNext Wend rs.Close LinkTables = True End Function Sub CallLinkTables() Dim Result As Boolean 'sample call: Result = LinkTables("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb") Debug.Print Result End Sub |
|
|
|
Feb 24 2012, 12:17 AM
Post
#2
|
|
|
Access Wiki and Forums Moderator Posts: 48,595 From: SoCal, USA |
Hi,
(IMG:style_emoticons/default/welcome2UA.gif) What version of Access are you using? Knowing the Access version may help us determine a more appropriate response to your question. Your code looks fine. How are you calling the function? |
|
|
|
Feb 24 2012, 12:29 AM
Post
#3
|
|
|
New Member Posts: 6 From: Adelaide, South Australia |
Hi, (IMG:style_emoticons/default/welcome2UA.gif) What version of Access are you using? Knowing the Access version may help us determine a more appropriate response to your question. Your code looks fine. How are you calling the function? Oops, sorry! Using Access 2007. I am calling the function with the example code in an OnClick event... CODE Dim Result As Boolean Result = LinkTables("C:\FCMTITS\FCMTITS-BACKEND.accdb") Debug.Print Result I realise the Debug.Print Result isn't needed but figured if I copy the example code verbatim it should work. All I get printed in the Debug log is "FALSE" |
|
|
|
Feb 24 2012, 12:43 AM
Post
#4
|
|
|
Access Wiki and Forums Moderator Posts: 48,595 From: SoCal, USA |
Hi,
Did you try stepping through the code? Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Feb 24 2012, 03:41 AM
Post
#5
|
|
|
UtterAccess VIP Posts: 7,646 From: South coast, England |
(IMG:style_emoticons/default/welcome2UA.gif)
PMFJI In A2007 (and probably A2010) the "DoCmd TransferDatabase acLink" method has the unfortunate side effect of making the Navigation Pane visible - whatever the orginal visibilty setting, so I use the following code: CODE Public Function LinkBackEndExmpl(strFileName As String) As Boolean On Error GoTo err_proc 'Function creates linked tables to all tables (except "msys*" tables) in BE database defined by strFilename Dim strSQL As String Dim dbs As DAO.Database Dim dbs1 As DAO.Database Dim dbs2 As DAO.Database Dim tdf As TableDef Dim tdf1 As TableDef Dim strTblName As String Set dbs2 = OpenDatabase(Application.CurrentProject.FullName) Set dbs1 = DBEngine.Workspaces(0).OpenDatabase(strFileName) Set dbs = db With dbs On Error Resume Next For Each tdf1 In dbs1.TableDefs strTblName = tdf1.Name If Left(strTblName, 4) <> "msys" Then dbs2.TableDefs.Delete strTblName End If Next tdf1 On Error GoTo err_proc For Each tdf1 In dbs1.TableDefs strTblName = tdf1.Name If Left(strTblName, 4) <> "msys" And strTblName <> "tblCurStatus" Then Debug.Print strTblName Set tdf = .CreateTableDef(strTblName) tdf.Connect = ";DATABASE=" & strFileName tdf.SourceTableName = strTblName .TableDefs.Append tdf End If Next tdf1 End With LinkBackEnd = True exit_proc: On Error Resume Next Set dbs = Nothing Set dbs2 = Nothing Set dbs1 = Nothing Exit Function err_proc: MsgBox err.Description Debug.Print strFileName & Chr(13) & strTblName Resume exit_proc End Function hth |
|
|
|
Feb 24 2012, 06:26 PM
Post
#6
|
|
|
New Member Posts: 6 From: Adelaide, South Australia |
Thanks pere - although with that code I get an 'Object Required' error.
I'm sure it's something simple I'm doing wrong at this end (IMG:style_emoticons/default/crazy.gif) |
|
|
|
Feb 24 2012, 07:53 PM
Post
#7
|
|
|
UtterAccess Guru Posts: 775 |
Just wondering, but what's this doing or supposed to be doing?
CODE Set dbs = db do you mean CODE Set dbs = CurrentDb ? This post has been edited by theDBguy: Feb 24 2012, 11:14 PM
Reason for edit: Replaced codebox with code tags.
|
|
|
|
Feb 25 2012, 05:22 AM
Post
#8
|
|
|
UtterAccess VIP Posts: 7,646 From: South coast, England |
Hi MadPiet
Good catch - Thanks - 'db' is a self healing variable - this means that the database does not have to be instantiated into memory each time 'CurrentDb' is called and hence can make the database a lot faster. I should have replaced 'Set dbs = db' with 'Set dbs = CurrentDb' before I posted (IMG:style_emoticons/default/blush.gif) This should also resolve TheWedgie's question. Cheers. |
|
|
|
Feb 26 2012, 05:13 AM
Post
#9
|
|
|
New Member Posts: 6 From: Adelaide, South Australia |
Marvellous - that fixed it.
Although the (linked) tables that used to be hidden are now not hidden, although I can fix that as they probably don't need to be linked. The sooner I get started on rewriting this from scratch the better! (IMG:style_emoticons/default/thanks.gif) |
|
|
|
Feb 26 2012, 08:56 AM
Post
#10
|
|
|
UtterAccess VIP Posts: 7,646 From: South coast, England |
(IMG:style_emoticons/default/yw.gif)
You can prevent the code linking unwanted tables by using a select case statement, as below: CODE Public Function LinkBackEndExmpl(strFileName As String) As Boolean On Error GoTo err_proc 'Function creates linked tables to all tables (except "msys*" tables) in BE database defined by strFilename Dim strSQL As String Dim dbs As DAO.Database Dim dbs1 As DAO.Database Dim dbs2 As DAO.Database Dim tdf As TableDef Dim tdf1 As TableDef Dim strTblName As String Set dbs2 = OpenDatabase(Application.CurrentProject.FullName) Set dbs1 = DBEngine.Workspaces(0).OpenDatabase(strFileName) Set dbs = db With dbs On Error Resume Next For Each tdf1 In dbs1.TableDefs strTblName = tdf1.Name If Left(strTblName, 4) <> "msys" Then dbs2.TableDefs.Delete strTblName End If Next tdf1 On Error GoTo err_proc For Each tdf1 In dbs1.TableDefs strTblName = tdf1.Name Debug.Print strTblName If Left(strTblName, 4) <> "msys" Then Select Case strTblName Case "TableName1", "TableName2" 'List tables you do not want linked, separated by a comma Case Else Set tdf = .CreateTableDef(strTblName) tdf.Connect = ";DATABASE=" & strFileName tdf.SourceTableName = strTblName .TableDefs.Append tdf End Select End If Next tdf1 End With LinkBackEnd = True exit_proc: On Error Resume Next Set dbs = Nothing Set dbs2 = Nothing Set dbs1 = Nothing Exit Function err_proc: MsgBox err.Description Debug.Print strFileName & Chr(13) & strTblName Resume exit_proc End Function hth |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 18th June 2013 - 08:23 PM |