R
Rob Parker
I'm using the code listed below to re-establish a link to a spreadsheet.
It's based on the code at http://www.mvps.org/access/tables/tbl0012.htm, but
it fails to reset the connection string. The existing connect string is:
Excel 5.0;HDR=YES;IMEX=2;DATABASE=C:\Documents and Settings\Rob\My
Documents\zzzDocument Index - New.xls
and the new connect string, generated from the selected filename, is either:
Excel 5.0;HDR=YES;IMEX=2;DATABASE=C:\Documents and Settings\Rob\My
Documents\Document Index - New.xls
or
Excel 5.0;HDR=YES;IMEX=2;DATABASE="C:\Documents and Settings\Rob\My
Documents\Document Index - New.xls"
or
;DATABASE=C:\Documents and Settings\Rob\My Documents\Document Index -
New.xls
or
;DATABASE="C:\Documents and Settings\Rob\My Documents\Document Index -
New.xls"
I tried the versions with quote delimiters to see whether the spaces in the
filename were causing the problem, and the shorter versions (starting with
;DATABASE = ) when the first two failed to work, thinking that perhaps I
didn't need the first portion (the MSysObjects has the "Excel
5.0;HDR=YES;IMEX=2;" in the Connect field, and the full filename in the
database field). However, none of these reset the link - the relevant lines
of code:
CurrentDb.TableDefs(strTableName).Connect = strNewConnect
CurrentDb.TableDefs(strTableName).RefreshLink
seem to do nothing; after the code runs, the connect string is unchanged
from its initial value.
What am I missing here?
TIA,
Rob
Public Function RelinkSheet(strTableName As String) As Boolean
Dim tdf As DAO.TableDef
Dim strFilter As String
Dim strXLS As String
Dim strSheet As String
Dim strOldPath As String
Dim strNewConnect As String
Dim iStart As Integer
Dim iLength As Integer
On Error GoTo RelinkSheet_Error
strSheet = Left(CurrentDb.TableDefs(strTableName).SourceTableName,
Len(CurrentDb.TableDefs(strTableName).SourceTableName) - 1)
Debug.Print strSheet
Debug.Print CurrentDb.TableDefs(strTableName).Connect
iStart = InStr(1, CurrentDb.TableDefs(strTableName).Connect, "DATABASE=")
+ 9
iLength = Len(CurrentDb.TableDefs(strTableName).Connect) - 9
strOldPath = Mid(CurrentDb.TableDefs(strTableName).Connect, iStart,
iLength)
strOldPath = Left(strOldPath, InStrRev(strOldPath, "\"))
' Debug.Print strOldPath
RelinkSheet = False
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.XLS)", "*.XLS")
strXLS = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
InitialDir:=strOldPath, _
DialogTitle:="Please select the Document Index file...", _
Flags:=ahtOFN_HIDEREADONLY)
' Debug.Print strXLS
If strXLS <> "NoFile" Then
'strNewConnect = Left(CurrentDb.TableDefs(strTableName).Connect,
iStart - 1) & strXLS
'strNewConnect = Left(CurrentDb.TableDefs(strTableName).Connect,
iStart - 1) & """" & strXLS & """"
'strNewConnect = ";DATABASE=" & strXLS
strNewConnect = ";DATABASE=" & """" & strXLS & """"
Debug.Print "strNewConnect = " & strNewConnect
CurrentDb.TableDefs(strTableName).Connect = strNewConnect
CurrentDb.TableDefs(strTableName).RefreshLink
Debug.Print "Connect set to " &
CurrentDb.TableDefs(strTableName).Connect
End If
'use custom function to check that new link is valid
If CheckLink(strTableName) Then
RelinkSheet = True
Else
RelinkSheet = False
End If
ExitPoint:
Debug.Print RelinkSheet
Set tdf = Nothing
Exit Function
RelinkSheet_Error:
RelinkSheet = False
If Err.Number = 3011 Then
MsgBox "The file you selected does not contain the '" & strSheet & "'
sheet.", vbExclamation, "Sheet Not Found"
Resume ExitPoint
End If
MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & vbNewLine _
& "in procedure RelinkSheet" & vbNewLine _
& "of Module RefreshLinks"
Resume ExitPoint
End Function
It's based on the code at http://www.mvps.org/access/tables/tbl0012.htm, but
it fails to reset the connection string. The existing connect string is:
Excel 5.0;HDR=YES;IMEX=2;DATABASE=C:\Documents and Settings\Rob\My
Documents\zzzDocument Index - New.xls
and the new connect string, generated from the selected filename, is either:
Excel 5.0;HDR=YES;IMEX=2;DATABASE=C:\Documents and Settings\Rob\My
Documents\Document Index - New.xls
or
Excel 5.0;HDR=YES;IMEX=2;DATABASE="C:\Documents and Settings\Rob\My
Documents\Document Index - New.xls"
or
;DATABASE=C:\Documents and Settings\Rob\My Documents\Document Index -
New.xls
or
;DATABASE="C:\Documents and Settings\Rob\My Documents\Document Index -
New.xls"
I tried the versions with quote delimiters to see whether the spaces in the
filename were causing the problem, and the shorter versions (starting with
;DATABASE = ) when the first two failed to work, thinking that perhaps I
didn't need the first portion (the MSysObjects has the "Excel
5.0;HDR=YES;IMEX=2;" in the Connect field, and the full filename in the
database field). However, none of these reset the link - the relevant lines
of code:
CurrentDb.TableDefs(strTableName).Connect = strNewConnect
CurrentDb.TableDefs(strTableName).RefreshLink
seem to do nothing; after the code runs, the connect string is unchanged
from its initial value.
What am I missing here?
TIA,
Rob
Public Function RelinkSheet(strTableName As String) As Boolean
Dim tdf As DAO.TableDef
Dim strFilter As String
Dim strXLS As String
Dim strSheet As String
Dim strOldPath As String
Dim strNewConnect As String
Dim iStart As Integer
Dim iLength As Integer
On Error GoTo RelinkSheet_Error
strSheet = Left(CurrentDb.TableDefs(strTableName).SourceTableName,
Len(CurrentDb.TableDefs(strTableName).SourceTableName) - 1)
Debug.Print strSheet
Debug.Print CurrentDb.TableDefs(strTableName).Connect
iStart = InStr(1, CurrentDb.TableDefs(strTableName).Connect, "DATABASE=")
+ 9
iLength = Len(CurrentDb.TableDefs(strTableName).Connect) - 9
strOldPath = Mid(CurrentDb.TableDefs(strTableName).Connect, iStart,
iLength)
strOldPath = Left(strOldPath, InStrRev(strOldPath, "\"))
' Debug.Print strOldPath
RelinkSheet = False
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.XLS)", "*.XLS")
strXLS = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
InitialDir:=strOldPath, _
DialogTitle:="Please select the Document Index file...", _
Flags:=ahtOFN_HIDEREADONLY)
' Debug.Print strXLS
If strXLS <> "NoFile" Then
'strNewConnect = Left(CurrentDb.TableDefs(strTableName).Connect,
iStart - 1) & strXLS
'strNewConnect = Left(CurrentDb.TableDefs(strTableName).Connect,
iStart - 1) & """" & strXLS & """"
'strNewConnect = ";DATABASE=" & strXLS
strNewConnect = ";DATABASE=" & """" & strXLS & """"
Debug.Print "strNewConnect = " & strNewConnect
CurrentDb.TableDefs(strTableName).Connect = strNewConnect
CurrentDb.TableDefs(strTableName).RefreshLink
Debug.Print "Connect set to " &
CurrentDb.TableDefs(strTableName).Connect
End If
'use custom function to check that new link is valid
If CheckLink(strTableName) Then
RelinkSheet = True
Else
RelinkSheet = False
End If
ExitPoint:
Debug.Print RelinkSheet
Set tdf = Nothing
Exit Function
RelinkSheet_Error:
RelinkSheet = False
If Err.Number = 3011 Then
MsgBox "The file you selected does not contain the '" & strSheet & "'
sheet.", vbExclamation, "Sheet Not Found"
Resume ExitPoint
End If
MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & vbNewLine _
& "in procedure RelinkSheet" & vbNewLine _
& "of Module RefreshLinks"
Resume ExitPoint
End Function