Hi Experts,
I have a club member registration application consisting of a program database and a data database. They reside in the same directory. There is an autoexec macro which runs at the beginning. This macro has to find the DATAdb and make sure that the required tables are linked (Not all tables). If it cannot find the DATAdb and then has to make a call to the user to use a different procedure.
Now to find the DATAdb I have used the following code that works. The function GetPathAndName parses the string db.name and outputs the path and name of the PROGdb. Up to here no problems.
Then I use the following code to get the fullpath of the last database used and thereby find the path and name of the last DATAdb. The path of PROGdb is used in place of the existing path of DATAdb to check whether the file exists.
As the final step I use the table definitions to refresh links that already exist and connect tables that are not linked The code is below. But I can not get it to work. It links 7 tables instead of 16 and when tested in different folder says invalid operation and so on. There is fundamental error some place but I could not figure it out. Help please.
I have a club member registration application consisting of a program database and a data database. They reside in the same directory. There is an autoexec macro which runs at the beginning. This macro has to find the DATAdb and make sure that the required tables are linked (Not all tables). If it cannot find the DATAdb and then has to make a call to the user to use a different procedure.
Now to find the DATAdb I have used the following code that works. The function GetPathAndName parses the string db.name and outputs the path and name of the PROGdb. Up to here no problems.
Code:
GetPathAndName db.Name, FrontPath, dName
Code:
Dim rs As Recordset, cPath As String, cName As String
Set rs = CurrentDb.OpenRecordset("SELECT Database, Type " & _
"FROM MSysObjects " & _
"WHERE ((MsysObjects.Type) = 6) AND ((MsysObjects.Name) = 'MembersTbl') " & _
"ORDER BY MsysObjects.DateUpdate DESC;")
If rs.RecordCount = 0 Then
MsgBox "There is no database linked in the folder" & Chr(13) & _
"Use Change Data button", , GC_Title
Exit Function
End If
rs.MoveFirst
While Not rs.EOF
Debug.Print "rs!DataBase="; rs!Database
GetPathAndName rs!Database, cPath, cName
If FileExists(FrontPath + cName) Then GoTo EndLoop
rs.MoveNext
Wend
MsgBox "Data file can not be found, Use CHANGE DATA button to give data file name", , GC_Title
Exit Function
EndLoop:
BaseName = FrontPath + cName 'NEW FULLPATH OF DATADB
Debug.Print "BaseName="; BaseName
'-----------------------------------------------------------------------------------
Code:
On Error GoTo Error_Linking
Debug.Print "TableDefs.Count="; db.TableDefs.Count
For I = 0 To db.TableDefs.Count - 1
Debug.Print "TableDefs("; I; ").Connect="; db.TableDefs(I).Connect
Debug.Print "TableDefs("; I; ").Name="; db.TableDefs(I).Name
Debug.Print "TableDefs("; I; ").SourceTableName="; db.TableDefs(I).SourceTableName
tConn = Nz(db.TableDefs(I).Connect, "") 'path and Name of connection database
tName = Nz(db.TableDefs(I).Name, "") 'Linked table name
tSour = Nz(db.TableDefs(I).SourceTableName, "") 'Source table name
'------------------------------------------------------------------------------------
' see if the table is not a system or temporary table or null
' if it is originally connected to backend refreshlink
'------------------------------------------------------------------------------------
Debug.Print "tName="; tName
If (((Not IsNull(tName)) And (tName <> "") And (Left(tName, 4) <> "MSys") And _
(Left(tName, 4) <> "~TMP")) Or tSour <> "") Then
If ((BackPath) = tConn) Then
Debug.Print "2"
db.TableDefs(I).RefreshLink
J1 = J1 + 1
Else
Debug.Print "3"
db.TableDefs(I).Connect = BackPath + bName
J2 = J2 + 1
End If
End If
GoNext:
Next