If possible would somebody share a sample of how to determine if a query or linked table already exist in a database before being re=linked or created.
Function IsTable(DbName As String, TName As String) As Integer
Dim db As Database
Dim Found As Integer
Dim Test As String
Const Name_Not_In_Collection = 3265
Found = False
On Error Resume Next
'If the database name is empty..
If Trim$(DbName) = "" Then 'If db name not specified in function call isTable("",xx)
'...set the db to current db
Set db = CurrentDb()
Else
'otherwise set db to specified open database
Set db = DBEngine.Workspaces(0).OpenDatabase(DbName)
'see if an error occurred
If Err Then
'ONE CAUSE OF BEING HERE IS IF _BE NOT FOUND (MAYBE ON REMOTE PC WITH ONLY FE)
MsgBox "modDTE could not find database to open: " & DbName
IsTable = False
Exit Function
End If
End If
'see if the name is in the Tables collection
Test = db.TableDefs(TName$).Name
If Err <> Name_Not_In_Collection Then Found = True
db.Close
IsTable = Found
End Function
Code to check for table:
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'Specify paths to frontend and backend
Set db = DBEngine.OpenDatabase("C:\NCTCComm\NCTC Subscriber_be.mdb" '<---SET THIS BEFORE DELIVERING
strPath = "C:\NCTCComm\NCTC Subscriber_be.mdb" '<----------SET THIS BEFORE DELIVERING
strFePath = "C:\NCTCComm\NCTC Subscriber.mdb" '<----------SET THIS BEFORE DELIVERING
'Link #1 tbl ErrorLog table
If Not IsTable(strFePath, "tbl ErrorLog" And IsTable(strPath, "tbl ErrorLog" Then
bSuccess = CreateAttached("tbl ErrorLog", strPath, "tbl ErrorLog"
If bSuccess = True Then
'REM MsgBox "Developer Message #9:" & vbcrlf & vbcrlf &"'tbl ErrorLog' link established.", vbInformation
Else
MsgBox "Attempt to link 'tbl ErrorLog' failed." 'Do something more here???
End If
Else
End If
By using a macro to test for linked tables, here is the code I found for reconnecting linked table.
Function Reconnect()
'**************************************************************
'* START YOUR APPLICATION (MACRO: AUTOEXEC) WITH THIS FUNCTION
'* AND THIS PROGRAM WILL CHANGE THE CONNECTIONS AUTOMATICALLY
'* WHEN THE 'DATA.MDB' AND THE 'PRG.MDB'
'* ARE IN THE SAME DIRECTORY!!!
'* PROGRAMMING BY PETER VUKOVIC, Germany
'* 100700.1262@compuserve.com
'* ************************************************************
Dim db As Database, source As String, path As String
Dim dbsource As String, I As Integer, J As Integer
Set db = DBEngine.Workspaces(0).Databases(0)
'*************************************************************
'* RECOGNIZE THE PATH *
'*************************************************************
For I = Len(db.Name) To 1 Step -1
If Mid(db.Name, I, 1) = Chr(92) Then
path = Mid(db.Name, 1, I)
MsgBox (path)
Exit For
End If
Next
'*************************************************************
'* CHANGE THE PATH AND CONNECT AGAIN *
'*************************************************************
For I = 0 To db.TableDefs.Count - 1
If db.TableDefs(I).Connect <> " " Then
source = Mid(db.TableDefs(I).Connect, 11)
Debug.Print source
For J = Len(source) To 1 Step -1
If Mid(source, J, 1) = Chr(92) Then
dbsource = Mid(source, J + 1, Len(source))
source = Mid(source, 1, J)
If source <> path Then
db.TableDefs(I).Connect = ";Database=" + path + dbsource
db.TableDefs(I).RefreshLink
Debug.Print ";Database=" + path + dbsource
End If
Exit For
End If
Next
End If
Next
End Function
Just add it to the module section.
For Query table check under the help for information on detecting query table.
If existTable("dbo_PSC_ye_AvgeragesAndFixed_Data" = False Then
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DSN=CMSDEV;UID=sa;PWD=bulletpr00f;DATABASE=CMSDEV", acTable, "_PSC_ye_AvgeragesAndFixed_Data", "dbo_PSC_ye_AvgeragesAndFixed_Data", False, True
End If
Procedure in my Utilities Module that gets called...
Public Function existTable(tblName As String) As Boolean
Dim test As String
existTable = False
'see if the name is in the Tables collection
On Error Resume Next
test = CurrentDb.TableDefs(tblName).Name
'MsgBox Err.Number
If Err = 0 Then existTable = True
Err.Clear
End Function
Thanks for the help it got me in the right direction.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.