Connecting Tech Pros Worldwide Forums | Help | Site Map

How to Programmatically Create a Hyperlink on a Form

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#1   Nov 26 '07
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#Sub-Address#Control Tip Text. Before we begin, a few assumptions:
  1. Table Name: tblSales.
  2. Form Name: frmWeeklySales.
  3. RecordSource of frmWeeklySales is tblSales.
  4. A Field named WeeklyData, Data Type = Hyperlink, exists in tblSales and will actually store the Hyperlink.
  5. The Form Field (Text Box) Bound to [WeeklyData] is named txtSalesForWeek.
  6. For demo purposes, the actual File name retrieved will be: C:\Invoices\2007\November\Week 1\Weekly Data for Period 11-05-07 to 11-09-07.xls.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPopulateHyperlink_Click()
  2. 'First, set a Reference to the Microsoft Office XX.X Object Library
  3.  
  4. Dim strButtonCaption As String, strDialogTitle As String
  5. Dim strHyperlinkFile As String, strSelectedFile As String
  6.  
  7. 'Define your own Captions if necessary
  8. strButtonCaption = "Save Hyperlink"
  9. strDialogTitle = "Select File to Create Hyperlink to"
  10.  
  11. With Application.FileDialog(msoFileDialogFilePicker)
  12.   With .Filters
  13.     .Clear
  14.     .Add "All Files", "*.*"     'Allow ALL File types
  15.   End With
  16.   'The Show Method returns True if 1 or more files are selected
  17.     .AllowMultiSelect = False       'Critical Line
  18.     .FilterIndex = 1 'Database files
  19.     .ButtonName = strButtonCaption
  20.     .InitialFileName = vbNullString
  21.     .InitialView = msoFileDialogViewDetails     'Detailed View
  22.     .Title = strDialogTitle
  23.   If .Show Then
  24.     For Each varItem In .SelectedItems 'There will only be 1
  25.       'Display Text + # + Address of Hyperlink (Display Text#Hyperlink Address)
  26.       strSelectedFile = varItem
  27.       strHyperlinkFile = fGetBaseFileName(strSelectedFile) & "#" & strSelectedFile
  28.         Me![txtSalesForWeek] = strHyperlinkFile
  29.     Next varItem
  30.   End If
  31. End With
  32. End Sub
Expand|Select|Wrap|Line Numbers
  1. Public Function fGetBaseFileName(strFilePath As String) As String
  2. 'This Function accepts the Absolute Path to a File and returns the Base File
  3. 'Name (File Name without the Extension)
  4.  
  5. 'Make absolutely sure that it is a valid Path/Filename
  6. If Dir$(strFilePath) = "" Then Exit Function
  7.  
  8. Dim strFileName As String
  9. Dim strBaseFileName As String
  10.  
  11. strFileName = Right$(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
  12.  
  13. strBaseFileName = Left$(strFileName, InStr(strFileName, ".") - 1)
  14.   fGetBaseFileName = strBaseFileName
  15. End Function
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:
  1. File Name selected from FileDialog:
    Expand|Select|Wrap|Line Numbers
    1. C:\Invoices\2007\November\Week 1\Weekly Data for Period 11-05-07 to 11-09-07.xls
  2. Base File Name generated via fGetBaseFileName()
    Expand|Select|Wrap|Line Numbers
    1. Weekly Data for Period 11-05-07 to 11-09-07
  3. String copied to [txtSalesForWeek]:
    Expand|Select|Wrap|Line Numbers
    1. Weekly Data for Period 11-05-07 to 11-09-07#C:\Invoices\2007\November\Week 1\Weekly Data for Period 11-05-07 to 11-09-07.xls
  4. String displayed in [txtSalesForWeek] - (Display Text)
    Expand|Select|Wrap|Line Numbers
    1. Weekly Data for Period 11-05-07 to 11-09-07
  5. Actual value stored in Table:
    Expand|Select|Wrap|Line Numbers
    1. Weekly Data for Period 11-05-07 to 11-09-07#C:\Invoices\2007\November\Week 1\Weekly Data for Period 11-05-07 to 11-09-07.xls



Reply