CharlieT302
Instructor
I need to create a table link that looks for a file in the "My Documents" folder regardless of the workstation on which the dbase is running.
Can this be done?
Can this be done?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Private Declare Function SHGetFolderPath Lib "shell32.dll" Alias "SHGetFolderPathA" _
(ByVal hwnd As Long, ByVal csidl As Long, ByVal hToken As Long, ByVal dwFlags As Long, _
ByVal pszPath As String) As Long
Private Const CSIDL_PERSONAL = &H5
Private Const SHGFP_TYPE_CURRENT = &H0 'current value for user, verify it exists
Private Const SHGFP_TYPE_DEFAULT = &H1
Private Const MAX_LENGTH = 260
Private Const S_OK As Long = &H0
Private Const S_FALSE As Long = &H1
Private Const E_FAIL As Long = &H80004005
Private Const E_INVALIDARG As Long = &H80070057
Private Function fFolderPath() As String
Dim lRet As Long
Dim sPath As String * MAX_LENGTH
lRet = SHGetFolderPath(0, CSIDL_PERSONAL, 0, SHGFP_TYPE_CURRENT, sPath)
If lRet = S_OK Then
fFolderPath = fCleanPath(sPath)
Else
MsgBox lRet
End If
End Function
Private Function fCleanPath(s As String) As String
Dim i As Integer
i = InStr(s, Chr$(0))
fCleanPath = left$(s, i - 1)
End Function
Sub LinkXLS()
'Needs reference to Microsoft DAO 3.x Object Library
Dim db As Database
Dim rs As DAO.Recordset
Dim tdf As TableDef
Set db = CurrentDb
strpath = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
'Let us say there is an extra folder called data.
strfile = Dir(strpath & "\Data\*.xls")
Do While strfile <> ""
'The use of system tables is not supported
strSQL = "SELECT Database, Name From MSysObjects " _
& "WHERE Database Like '*" & strfile & "*'"
Set rs = db.OpenRecordset(strSQL)
If rs.EOF Then
'Missing, transferspreadsheet
ElseIf rs!Database <> strpath & strfile Then '"\" &
'Problem, relink
'You could delete and transfer.
Set tdf = db.TableDefs(rs!Name)
'This is 'for example'. Your connect
'string will be different. You could use
'Mid & Instr to get the first pat of
'the connection string.
tdf.Connect = "Excel 5.0;HDR=NO;IMEX=2;DATABASE=" & strpath & "\Data\" & strfile
tdf.RefreshLink
End If
strfile = Dir
Loop
End Sub