ACC97: How to Create Shortcuts from a Field of Hyperlinks (181231)



The information in this article applies to:

  • Microsoft Access 97

This article was previously published under Q181231

SUMMARY

Advanced: Requires expert coding, interoperability, and multi-user skills.

This article shows you how to create sample Visual Basic for Applications code that creates Internet shortcuts from a Microsoft Access table that contains a Hyperlink field with one or many records. The shortcuts are then placed in a folder on your hard disk.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To create a folder of Internet shortcuts from a field of hyperlinks, follow these steps:
  1. In Windows Explorer, create the following folder:

    C:\MyShortcuts

  2. Start Microsoft Access 97, open a database and create the following table:
    Table: tblHyperlinks
           -----------------------
           Field Name: fldLink
              Data Type: Hyperlink
    					
  3. Save the table as tblHyperlinks. When prompted to create a primary key, click Yes.
  4. Switch to Datasheet View and type a valid Internet address into the fldLink field of the first record.
  5. Right-click the address, point to Hyperlink, and type a descriptive name in the Display Text box. Although the displaytext portion of the Hyperlink data type is optional, you can hide the URL address and display a more user-friendly name for the hyperlink if you specify display text.
  6. Repeat steps 4 and 5 until you have added several records with different Internet addresses.
  7. Create a new module and type or paste the following code:
           Sub ProcessHyperlinkTable(strTableName As String, _
              strFieldName As String)
    
              ' Declare variables.
              Dim DBS As Database
              Dim RST As Recordset
              Dim strSQL As String
              Dim strAddress As String
              Dim strDisplayText As String
              Dim strDirectoryPath As String
              Dim varLink As Variant
    
              ' Set error trapping.
              On Error GoTo Error_handler
    
              ' Assign the current database to the database variable.
              Set DBS = CurrentDb
    
              ' Set the path to the existing directory
              ' where the shortcuts will be created.
              ' Note: the path should end with a backslash ("\").
              strDirectoryPath = "c:\MyShortcuts\"
    
              ' Set the SQL string to return the field
              ' containing the hyperlinks from the
              ' table you have specified.
              strSQL = "SELECT " & strFieldName _
                 & " FROM " & strTableName
    
              ' Open the Recordset.
              Set RST = DBS.OpenRecordset(strSQL)
    
              ' Move to the first record.
              RST.MoveFirst
    
              ' Loop until reaching the end of the table.
              Do Until RST.EOF
                 ' Check to see if the HomePage field contains a link.
                 If Not IsNull(RST!fldLink) Then
    
                    ' Set varLink equal to the current Hypertext link field.
                    varLink = RST!fldLink
    
                    ' Pull the Address from varLink.
                    strAddress = HyperlinkPart(varLink, acAddress)
    
                    ' Pull the display text, if any, from varLink.
                    strDisplayText = HyperlinkPart(varLink, acDisplayText)
    
                    ' If there is no display text, set strDisplayText to
                    ' be the same as the URL.
                    If strDisplayText = "" Then
    
                       ' If strAddress starts with "Http://",
                       ' or "Ftp://", leave that out of strDisplayText.
                       If Left(strAddress, 7) = "Http://" Or _
                          Left(strAddress, 6) = "Ftp://" Then
    
                             strDisplayText = Right(strAddress, _
                                Len(strAddress) _
                                - (InStr(1, strAddress, "/") + 1))
                       Else
                       ' Otherwise, simply set strDisplayText
                       ' equal to strAddress.
                          strDisplayText = strAddress
                       End If
    
                    End If
    
                    ' Pass the URL, the link name, and the directory path to
                    ' the function.
                    Create_Hyperlink_Shortcut strAddress, strDisplayText, _
                       strDirectoryPath
    
                 End If
    
                 ' Move to the next record.
                 RST.MoveNext
    
              Loop
    
           ' Error handling section
    
           ProcessHyperlinkTable_Exit:
    
              DBS.Close
    
              ' Exit the Procedure.
              Exit Sub
    
           Error_handler:
              MsgBox Err.Description
              Resume ProcessHyperlinkTable_Exit
    
           End Sub
    
           Function Create_Hyperlink_Shortcut(strAddress As String, _
              LinkFileName As String, LinkFilePath As String)
    
              ' Declare output file variable.
              Dim fileNum As Integer
              ' Declare counter variable.
              Dim i As Integer
    
              ' Set error trapping.
              On Error GoTo Error_handler
    
              ' Set fileNum to the next available file number.
              fileNum = FreeFile
    
              ' Create the new shortcut file.
              Open LinkFilePath & LinkFileName & ".url" For Output _
                 As #fileNum
              Print #fileNum, "[InternetShortcut]"
              Print #fileNum, "URL=" & strAddress
              Close #fileNum
    
              ' Exit the function when done.
              Exit Function
    
              ' Error handling section
           Error_handler:
    
              ' Trap for errors caused by a bad file name
              ' and prompt the user to correct it.
              Select Case Err
                 ' "Bad file name or number" error caused by
                 ' invalid characters in file name.
                 Case 52
                    LinkFileName = InputBox("Cannot create file for " _
                       & strAddress & Chr(13) _
                       & "The Internet Shortcut Name shown below is invalid." _
                       & Chr(13) & Chr(13) _
                       & "It contains one or more of the following: " _
                       & "\ / : * ? " & """" & " < > |" & Chr(13) & Chr(13) _
                       & Chr(13) & "Please enter a valid Shortcut name.", _
                       "Invalid Shortcut Name", LinkFileName)
    
                 ' "Path not found" error caused by slashes
                 ' in the file name.
                 Case 76
                    LinkFileName = InputBox("Cannot create file for " _
                       & strAddress & Chr(13) & Chr(13) _
                       & "The shortcut name shown below is invalid. " _
                       & "It may contain one or more slashes." _
                       & Chr(13) & Chr(13) _
                       & "Please remove any slashes from the Shortcut name.", _
                       "Path not found", LinkFileName)
    
                 ' If it is any other error, exit the function
                 ' without creating the shortcut file.
                 Case Else
                    MsgBox "Error Number: " & Err & " while attempting to " _
                       & "create" & "shortcut for " & strAddress
                    Exit Function
              End Select
    
              ' If the user entered a blank shortcut name in the error dialog,
              ' prompt for a correct one, allowing for three tries.
              For i = 1 To 3
                 If Len(LinkFileName) = 0 Then
                    LinkFileName = InputBox("Cannot create file for " _
                       & strAddress & Chr(13) _
                       & "The Internet Shortcut Name is blank" _
                       & Chr(13) & Chr(13) _
                       & "Please enter a valid Shortcut name.", _
                       "Invalid Shortcut Name", LinkFileName)
                 End If
              Next i
    
              ' If the user has still left the name blank, let the user know
              ' the shortcut will not be created and then exit the function.
              If Len(LinkFileName) = 0 Then
                 MsgBox ("A shortcut will not be created for " _
                    & strAddress & ".")
                 Exit Function
              End If
    
              ' Resume at the line that generated the error.
              Resume
    
           End Function
    					
  8. On the Debug menu, click "Compile and Save All Modules".
  9. Save the Module as HyperlinkCode.
  10. To test the code, type the following line in the Debug window, and then press ENTER:
    ProcessHyperLinkTable "tblHyperlinks", "fldLink"
    					
  11. Check the C:\MyShortcuts folder and notice the new Internet shortcuts.
  12. Double-click any Internet shortcut to run your browser and move to the respective URL.

REFERENCES

For more information about the parts of a Hyperlink field, search the Help Index for "hyperlinks, returning hyperlink information" or "hyperlinks, addresses" and then choose the "About hyperlink addresses in hyperlink fields and controls."

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbProgramming KB181231