Recently, there have been several questions and much confusion concerning the Topic of Hyperlinks. Specifically, Users wanted to know how to retrieve a File Name from a FileDialog Box, copy the Name to a Bound Text Box on a Form, and save the Hyperlink to the underlying Table. The code demos below will do just that: retrieve the Absolute Path of of File from a FileDialog Box, use the Base Name (no extension) as the Display Text for the Hyperlink, then store the Display Text and Hyperlink Address in the underlying Table via the Bound Text Box. It is also important to keep in mind that a Hyperlink can consist of up to 4 parts delimited by a # sign: Display Text#Address#Su b-Address#Control Tip Text. Before we begin, a few assumptions:
[CODE=vb]
Private Sub cmdPopulateHype rlink_Click()
'First, set a Reference to the Microsoft Office XX.X Object Library
Dim strButtonCaptio n As String, strDialogTitle As String
Dim strHyperlinkFil e As String, strSelectedFile As String
'Define your own Captions if necessary
strButtonCaptio n = "Save Hyperlink"
strDialogTitle = "Select File to Create Hyperlink to"
With Application.Fil eDialog(msoFile DialogFilePicke r)
With .Filters
.Clear
.Add "All Files", "*.*" 'Allow ALL File types
End With
'The Show Method returns True if 1 or more files are selected
.AllowMultiSele ct = False 'Critical Line
.FilterIndex = 1 'Database files
.ButtonName = strButtonCaptio n
.InitialFileNam e = vbNullString
.InitialView = msoFileDialogVi ewDetails 'Detailed View
.Title = strDialogTitle
If .Show Then
For Each varItem In .SelectedItems 'There will only be 1
'Display Text + # + Address of Hyperlink (Display Text#Hyperlink Address)
strSelectedFile = varItem
strHyperlinkFil e = fGetBaseFileNam e(strSelectedFi le) & "#" & strSelectedFile
Me![txtSalesForWeek] = strHyperlinkFil e
Next varItem
End If
End With
End Sub[/CODE]
[CODE=vb]
Public Function fGetBaseFileNam e(strFilePath As String) As String
'This Function accepts the Absolute Path to a File and returns the Base File
'Name (File Name without the Extension)
'Make absolutely sure that it is a valid Path/Filename
If Dir$(strFilePat h) = "" Then Exit Function
Dim strFileName As String
Dim strBaseFileName As String
strFileName = Right$(strFileP ath, Len(strFilePath ) - InStrRev(strFil ePath, "\"))
strBaseFileName = Left$(strFileNa me, InStr(strFileNa me, ".") - 1)
fGetBaseFileNam e = strBaseFileName
End Function[/CODE]
NOTE: The bulk of the code relates to setting up the FileDialog Box and extracting the Base File Name. The critical lines of code for purposes of this discussion are Lines 26 to 28.
Scenario and OUTPUT:
- Table Name: tblSales.
- Form Name: frmWeeklySales.
- RecordSource of frmWeeklySales is tblSales.
- A Field named WeeklyData, Data Type = Hyperlink, exists in tblSales and will actually store the Hyperlink.
- The Form Field (Text Box) Bound to [WeeklyData] is named txtSalesForWeek .
- For demo purposes, the actual File name retrieved will be: C:\Invoices\200 7\November\Week 1\Weekly Data for Period 11-05-07 to 11-09-07.xls.
[CODE=vb]
Private Sub cmdPopulateHype rlink_Click()
'First, set a Reference to the Microsoft Office XX.X Object Library
Dim strButtonCaptio n As String, strDialogTitle As String
Dim strHyperlinkFil e As String, strSelectedFile As String
'Define your own Captions if necessary
strButtonCaptio n = "Save Hyperlink"
strDialogTitle = "Select File to Create Hyperlink to"
With Application.Fil eDialog(msoFile DialogFilePicke r)
With .Filters
.Clear
.Add "All Files", "*.*" 'Allow ALL File types
End With
'The Show Method returns True if 1 or more files are selected
.AllowMultiSele ct = False 'Critical Line
.FilterIndex = 1 'Database files
.ButtonName = strButtonCaptio n
.InitialFileNam e = vbNullString
.InitialView = msoFileDialogVi ewDetails 'Detailed View
.Title = strDialogTitle
If .Show Then
For Each varItem In .SelectedItems 'There will only be 1
'Display Text + # + Address of Hyperlink (Display Text#Hyperlink Address)
strSelectedFile = varItem
strHyperlinkFil e = fGetBaseFileNam e(strSelectedFi le) & "#" & strSelectedFile
Me![txtSalesForWeek] = strHyperlinkFil e
Next varItem
End If
End With
End Sub[/CODE]
[CODE=vb]
Public Function fGetBaseFileNam e(strFilePath As String) As String
'This Function accepts the Absolute Path to a File and returns the Base File
'Name (File Name without the Extension)
'Make absolutely sure that it is a valid Path/Filename
If Dir$(strFilePat h) = "" Then Exit Function
Dim strFileName As String
Dim strBaseFileName As String
strFileName = Right$(strFileP ath, Len(strFilePath ) - InStrRev(strFil ePath, "\"))
strBaseFileName = Left$(strFileNa me, InStr(strFileNa me, ".") - 1)
fGetBaseFileNam e = strBaseFileName
End Function[/CODE]
NOTE: The bulk of the code relates to setting up the FileDialog Box and extracting the Base File Name. The critical lines of code for purposes of this discussion are Lines 26 to 28.
Scenario and OUTPUT:
- File Name selected from FileDialog:
[CODE=text]C:\Invoices\200 7\November\Week 1\Weekly Data for Period 11-05-07 to 11-09-07.xls[/CODE] - Base File Name generated via fGetBaseFileNam e()
[CODE=text]Weekly Data for Period 11-05-07 to 11-09-07[/CODE] - String copied to [txtSalesForWeek]:
[CODE=text]Weekly Data for Period 11-05-07 to 11-09-07#C:\Invoices\ 2007\November\W eek 1\Weekly Data for Period 11-05-07 to 11-09-07.xls[/CODE] - String displayed in [txtSalesForWeek] - (Display Text)
[CODE=text]Weekly Data for Period 11-05-07 to 11-09-07[/CODE] - Actual value stored in Table:
[CODE=text]Weekly Data for Period 11-05-07 to 11-09-07#C:\Invoices\ 2007\November\W eek 1\Weekly Data for Period 11-05-07 to 11-09-07.xls[/CODE]
Comment