Per Lewis Veale:
I know it used to only prompt for the required data source once, and
apply that to all the tables/views I needed to refresh, but now it
prompts for every table - which is VERY tedious!
I also have Access XP, and Access 2003 installed, but I am using
Access 2000 to attempt the refresh.
Any suggestions would be gratefully received.
It's probably wretched excess, but here's what I do.
It's all driven by the fact that my apps load using a custom .INI file that's on
the LAN and shared by all users. Also, in my app I have a little table that
contains a row for each connection that needs TB refreshed.
A way to do it without the .INI file is to invoke a Common File Dialog and let
the user navigate to the back end on their own.
I've done it that way too, but prefer the .INI file approach bc it's 100%
transparent to the user.
-------------------------------------------------------------------------
Option Compare Database 'Use database order for string comparisons
Option Explicit
' This module contains code for re-connecting to the back-end tables per the
..INI file's
' "BackEndDataDbPath parameter found in the "[TretsProgramParms]" group.
'
' Without this parameter, we are lost and the app cannot run
'
' This module's sole interfact to the outside world is the Public routine
'ConnectRefresh()'
' Next available line# series = 5000
Const mModuleName = "basConnect"
Global Const gPathDbMainParmName_LAN = "PathDbMain_LAN"
Global Const gPathDbMainParmName_Local = "PathDbMain_Local"
Const mPathDbDataLoaderParmName = "PathDbDataLoader"
Const mPathDbIndexStageParmName = "PathDbIndexStage"
Const mConnectionFatal = 0
Const mConnectionOK = -1
Const mConnectionNeedsRefresh = -2
Const mMainTestTableName = "zstblRecordNumbers" 'The single table
we test to see if all the connections of this type are OK
Const mDataLoaderTestTableName = "dbo_STG_PSN_PROD"
Const mIndexStageTestTableName = "zstblIndexLoad_Indexes" 'The single table
we test to see if all the connections of this type are OK
Const mHomeDirParmName = "HomeDirOnLan"
Const mIniGroupName = "ProgramParms"
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName
As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize
As Long, ByVal lpFileName As String) As Long
Private Function connectionOK(ByVal theParameterName As String, ByVal theDbPath
As String, ByVal theTestTable As String) As Integer
2000 debugStackPush mModuleName & ": connectionOK"
2001 On Error GoTo connectionOK_err
' PURPOSE: To performs a 3-phase check on the DB/Table in question:
' 1) Checks to see if DB exists and can be opened as a MS Access DB
' 2) Checks to see if theTestTable can be opened in the DB
' 3) Checks the connection string for theTestTable to see if it
matches theDbPath
' ACCEPTS: - The name of the .INI file group/parameter that we look for to
tell us the path
' to the back end DB. Used only in case of trouble to notify
the user that the
' Group and/or parameter cannot be found in the .INI file.
2010 Dim thisWS As DAO.Workspace
Dim thisDB As DAO.Database
Dim remoteDB As DAO.Database
Dim myRS As DAO.Recordset
Dim myTD As TableDef
Dim skipLine As String
skipLine = Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10)
Const invalidPath = 3044
Const cannotOpenDB = 3049
Const cannotFindFile = 3024
Const objectNotFound = 3011321
2020 Set thisWS = DBEngine(0)
2040 Set thisDB = CurrentDb()
On Error Resume Next
Set remoteDB = thisWS.OpenDatabase(theDbPath)
If Err > 0 Then
MsgBox ".INI file " & SysCmd(SYSCMD_INIFILE) & " specifies '" &
theParameterName & "=" & theDbPath & "'." & skipLine & "That file was not found
or is not a MS Access database." & skipLine & "Application cannot be run." &
skipLine & "If everything else looks OK, check the database name parameters in
the .INI file.", 48, "Fatal Error"
Else
Set myRS = remoteDB.OpenRecordset(theTestTable, DB_OPEN_TABLE)
If Err > 0 Then
MsgBox "Table '" & theTestTable & "' not found in " & theDbPath &
"'." & skipLine & "Looks like that isn't the right database or is damaged." &
skipLine & "Application cannot be run." & skipLine & "If everything else looks
OK, check the database name parameters in the .INI file.", 48, "Fatal Error"
Else
2120 On Error GoTo connectionOK_err
2130 Set myTD = thisDB.TableDefs(theTestTable)
2140 If myTD.Connect = ";Database=" & theDbPath Then
2150 connectionOK = mConnectionOK
2160 Else
2170 connectionOK = mConnectionNeedsRefresh
2180 End If
2190 End If
2999 End If
connectionOK_xit:
debugStackPop
On Error Resume Next
Set myTD = Nothing
myRS.Close
Set myRS = Nothing
thisDB.Close
Set thisDB = Nothing
Set thisWS = Nothing
Exit Function
connectionOK_err:
bugAlert True, ""
Resume connectionOK_xit
End Function
Private Function connectionsRefreshMdb(ByVal theDbType As String, ByVal
theDbPath As String) As Integer
3000 debugStackPush mModuleName & ": connectionsRefreshMdb"
3001 On Error GoTo connectionsRefreshMdb_err
' PURPOSE: To refresh all the connections that point to the specified outside
JET database
' ACCEPTS: - The type of DB. "Main" in this case. Allowing for different
types lets us
' cover a situation where the app (not this one right now...) may
have several
' outside DBs that it connects to.
' - The UNC or complete DOS path to the DB we are trying to connect
to.
3020 Dim thisDB As DAO.Database
Dim myRS As DAO.Recordset
Dim myQuery As DAO.QueryDef
Dim myTD As TableDef
Dim i As Integer
Dim connectCount As Integer
Dim curTableName As String
3030 Set thisDB = CurrentDb
3031 Set myQuery = thisDB.QueryDefs("qryConnectionsRefresh")
3032 myQuery.Parameters("theDbType") = theDbType
3033 Set myRS = myQuery.OpenRecordset(dbOpenDynaset)
3035 If (myRS.BOF And myRS.EOF) Then
3036 connectionsRefreshMdb = True
3039 Else
3050 StatusSet "Re-Connecting '" & theDbType & "' tables to '" & theDbPath &
"'..."
3081 With myRS
3060 .MoveFirst
3070 Do Until .EOF
3071 curTableName = !tableName
3075 Set myTD = thisDB.TableDefs(!tableName)
3080 StatusSet "Re-Connecting " & !tableName
3082 .Edit
3083 !OldConnect = myTD.Connect
3084 .Update
3085 connectCount = connectCount + 1
3090 myTD.Connect = ";Database=" & theDbPath
3091 myTD.RefreshLink
'LogTime False, "reconncect " & curTableName
3092 .MoveNext
3300 Loop
3099 End With
3997 connectionsRefreshMdb = True
3998 StatusSet ""
3999 End If
connectionsRefreshMdb_xit:
debugStackPop
On Error Resume Next
Set myTD = Nothing
Set myQuery = Nothing
myRS.Close
Set myRS = Nothing
Set thisDB = Nothing
Exit Function
connectionsRefreshMdb_err:
3800 bugAlert True, "Current table name='" & curTableName & "'."
3810 If connectCount > 0 Then
3820 myRS.MoveFirst 'If we had a problem, try to restore
original connections.,,
3830 For i = 1 To connectCount
3840 Set myTD = thisDB.TableDefs(myRS!tableName)
3850 myTD.Connect = myRS!OldConnect
3860 myTD.RefreshLink
3870 myRS.MoveNext
3880 Next i
3890 End If
3899 Resume connectionsRefreshMdb_xit
End Function
Public Function ConnectRefresh() As Boolean
1000 debugStackPush mModuleName & ": ConnectRefresh"
1001 On Error GoTo ConnectRefresh_err
' PURPOSE: To check the connection to a sample table and, if the connection's
target DB cannot be found,
' reconnect all tables according to the path found in the .INI file.
' ACCEPTS: Name of the .INI file parm that specifies which back-end DB to
connect to.
' RETURNS: - True if connections didn't need refreshing or they were
refreshed successfully
' - False if connections could not be refreshed
1010 Dim dbPath As String
Dim pathStrategy25 As String
Dim gotFatal As Integer
Dim myResult As Integer
Dim myParmName As String
1015 DoCmd.Hourglass True
1100 StatusSet "Checking 'Main' connections..."
1101 dbPath = pathDbMainGet(myParmName)
1102 If Len(dbPath) = 0 Then
1103 gotFatal = True
1104 Else
1105 myResult = connectionOK("[" & mIniGroupName & "]-" & myParmName, dbPath,
mMainTestTableName)
1109 Select Case myResult
Case mConnectionFatal
1111 gotFatal = True
1119 Case mConnectionOK
'(do nothing)
1120 Case mConnectionNeedsRefresh
1121 gotFatal = Not connectionsRefreshMdb("Main", dbPath)
1129 End Select
1199 End If
1200 StatusSet "Checking 'DataLoader' connections..."
1201 dbPath = IniValue_Get("ProgramParms", mPathDbDataLoaderParmName)
1202 If Len(dbPath) = 0 Then
1203 gotFatal = True
1204 MsgBox "'" & mPathDbDataLoaderParmName & "' parm not found in '" &
SysCmd(acSysCmdIniFile) & "'.", vbCritical, "Application Cannot Be Run"
1205 Else
1206 myResult = connectionOK("[" & mIniGroupName & "]-" &
mPathDbDataLoaderParmName, dbPath, mDataLoaderTestTableName)
1209 Select Case myResult
Case mConnectionFatal
1211 gotFatal = True
1219 Case mConnectionOK
'(do nothing)
1220 Case mConnectionNeedsRefresh
1221 gotFatal = Not connectionsRefreshMdb("DataLoader", dbPath)
1229 End Select
1299 End If
1300 StatusSet "Checking 'IndexStage' connections..."
1301 dbPath = IniValue_Get("ProgramParms", mPathDbIndexStageParmName)
1302 If Len(dbPath) = 0 Then
1303 gotFatal = True
1304 MsgBox "'" & mPathDbIndexStageParmName & "' parm not found in '" &
SysCmd(acSysCmdIniFile) & "'.", vbCritical, "Application Cannot Be Run"
1305 Else
1306 myResult = connectionOK("[" & mIniGroupName & "]-" &
mPathDbIndexStageParmName, dbPath, mIndexStageTestTableName)
1309 Select Case myResult
Case mConnectionFatal
1311 gotFatal = True
1319 Case mConnectionOK
'(do nothing)
1320 Case mConnectionNeedsRefresh
1321 gotFatal = Not connectionsRefreshMdb("IndexLoad", dbPath)
1329 End Select
1399 End If
1990 If gotFatal = False Then
1991 ConnectRefresh = True
1992 End If
1997 StatusSet ""
1999 DoCmd.Hourglass False
ConnectRefresh_xit:
StatusSet ""
debugStackPop
On Error Resume Next
Exit Function
ConnectRefresh_err:
bugAlert True, ""
Resume ConnectRefresh_xit
End Function
Private Function homeDirOnLanGet() As String
debugStackPush mModuleName & ": homeDirOnLanGet"
On Error GoTo homeDirOnLanGet_err
' PURPOSE: To return the name of the back-end DB path as it was specified in the
..INI file
' and, if necessary, to load it from the .INI file
Static myPathDat As String
Dim l As Long
Dim ParmValue As String
Dim myIniPath As String
Const myNotFound = "{NotFound}"
Const myParameterName = mHomeDirParmName
If Len(myPathDat & "") = 0 Then
ParmValue = Space(255)
myIniPath = UCase$(SysCmd(acSysCmdIniFile))
l = GetPrivateProfileString(mIniGroupName, myParameterName, myNotFound,
ParmValue, 255, myIniPath)
If l And Left(ParmValue, 10) <> myNotFound Then
myPathDat = Left(ParmValue, l)
Else
MsgBox "'" & myParameterName & "' parameter not found in " & myIniPath &
vbCrLf & vbCrLf & "Application cannot be run." & vbCrLf & vbCrLf & "Please
notify your LAN manager.", vbCritical, "Fatal Error"
End If
End If
homeDirOnLanGet = myPathDat
homeDirOnLanGet_xit:
debugStackPop
On Error Resume Next
Exit Function
homeDirOnLanGet_err:
bugAlert True, ""
Resume homeDirOnLanGet_xit
End Function
Private Function pathDbMainGet(ByRef theParmName As String) As String
debugStackPush mModuleName & ": pathDbMainGet"
On Error GoTo pathDbMainGet_err
' PURPOSE: To return the name of the back-end DB path as it was specified in the
..INI file
' and, if necessary, to load it from the .INI file
' ACCEPTS: - Name of .INI file parm that specifies location of back-end DB
' RETURNS: Path to back-end DB
' SETS: Name of the .INI parm that specifies back end db
Dim l As Long
Dim ParmValue As String
Dim myIniPath As String
Dim myPathDat As String
Dim myParmName As String
Const myNotFound = "{NotFound}"
If LocalMode_Get = True Then
myParmName = gPathDbMainParmName_Local
Else
myParmName = gPathDbMainParmName_LAN
End If
ParmValue = Space(255)
myIniPath = UCase$(SysCmd(acSysCmdIniFile))
l = GetPrivateProfileString(mIniGroupName, myParmName, myNotFound, ParmValue,
255, myIniPath)
If l And Left(ParmValue, 10) <> myNotFound Then
myPathDat = Left(ParmValue, l)
Else
MsgBox "'" & myParmName & "' parameter not found in " & myIniPath & vbCrLf &
vbCrLf & "Application cannot be run." & vbCrLf & vbCrLf & "Please notify your
LAN manager", vbCritical, "Fatal Error"
End If
theParmName = myParmName
pathDbMainGet = myPathDat
pathDbMainGet_xit:
debugStackPop
On Error Resume Next
Exit Function
pathDbMainGet_err:
bugAlert True, ""
Resume pathDbMainGet_xit
End Function
-------------------------------------------------------------------------
--
PeteCresswell