How to have a button open a hyperlink?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chris24
    New Member
    • Sep 2010
    • 4

    How to have a button open a hyperlink?

    I have a form with a Combo box and a button.
    I want the user to be able to choose an option in the combo box, click the button, and a related .pdf is opened.

    The Combo box has a control source in a table. Within the same record as the control source, a 2nd column contains a hyperlink with a file path to a related .pdf.

    What I am struggling with is the code associated with the clicking of the button.
    How do I command Access/VBA to go to the record chosen, find the related .pdf, and open the .pdf with a click of a button?
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    Hi Chris and welcome to Bytes.
    This can easily be done, but it would be alot easier to give a precise and useful answer for you if you could provide:
    Name of your combobox, its Controlsource value, and Row Source value.
    Name of your document table, and fields like the example provided:
    Code:
    tbl_Documents
      Key_Document, Autonumber
      tx_DocTitle, Text
      tx_DocFullPath, Text
    I can allready say that once you have the correct path, its a simple matter of:
    Code:
    Application.FollowHyperlink strFullPath
    where strFullPath is the full path to your document. If you provide the required information im sure one of us can help with the rest.

    Comment

    • Chris24
      New Member
      • Sep 2010
      • 4

      #3
      On the form
      ComboBox: CompanyName
      Controlsource value: CompanyName
      Row Source value: SELECT CompanyInfo.Fil eID, CompanyInfo.Com panyName FROM CompanyInfo ORDER BY CompanyInfo.Com panyName;

      On the table:
      Autonumber: FileID
      Text Column: CompanyName
      Hyperlink Column: InfoPDF

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        Okay, first its a good idea if you use comboboxes in code (as we are about to) to rename them so they do not have the same name as the field in the table. So we rename the CompanyName Combobox to cmb_CompanyName .

        We then add a button, btn_GoTo, and if you look at its properties, select events, and then on Click, you select the 3 ... on the righthand side. Click them and choose Event Procedure.

        Then you will be taken to the Visual basic editing window where some code we be written allready:
        Code:
        Private Sub btn_GoTo_Click()
        
        End Sub
        Modify this to:
        Code:
        Private Sub btn_GoTo_Click()
          dim strPath as string
          'Error check
          If isNull(Me.cmb_CompanyName) Then
            Msgbox "No document selected",vbokonly
            Exit Sub
          End If
        
          'Get path
          strPath=Dlookup("InfoPDF","tbl_Doc","FileID=" & Me.cmb_CompanyName)
          application.FollowHyperlink strPath
        End Sub

        Comment

        • Chris24
          New Member
          • Sep 2010
          • 4

          #5
          Thank you very much TheSmileyOne.

          I am having further issues because not every record in my table has a hyperlink path.

          I have been working with a If statement, but i cannot get it to work. I am unsure what I am commanding the if statement to do, but I receive the Msgbox everytime (I changed "tbl_doc" to my table name):

          If strPath = DLookup("InfoPD F", "CompanyInf o", "FileID=" & Me.cmb_CompanyN ame) Then
          Application.Fol lowHyperlink strPath
          Else
          MsgBox "No supporting document submitted", vbExclamation
          End If

          Comment

          • TheSmileyCoder
            Recognized Expert Moderator Top Contributor
            • Dec 2009
            • 2322

            #6
            In the code you posted you don't show where you got strPath from. If you want to check whether or not anything is returned you can do:

            Code:
            strPath = nz(DLookup("InfoPDF", "CompanyInfo", "FileID=" & Me.cmb_CompanyName),"")
            
            if strPath & ""<>"" Then
            Application.FollowHyperlink strPath
            Else
            MsgBox "No supporting document submitted", vbExclamation
            End If
            When posting code, please post full code, and remember to use the code tags around your code [code]...[/code]

            Comment

            • Chris24
              New Member
              • Sep 2010
              • 4

              #7
              Answer Complete!!

              Thank you so much!!

              Comment

              Working...