469,267 Members | 1,012 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

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?
Sep 9 '10 #1

✓ answered by TheSmileyCoder

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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_GoTo_Click()
  2.  
  3. End Sub
Modify this to:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_GoTo_Click()
  2.   dim strPath as string
  3.   'Error check
  4.   If isNull(Me.cmb_CompanyName) Then
  5.     Msgbox "No document selected",vbokonly
  6.     Exit Sub
  7.   End If
  8.  
  9.   'Get path
  10.   strPath=Dlookup("InfoPDF","tbl_Doc","FileID=" & Me.cmb_CompanyName)
  11.   application.FollowHyperlink strPath
  12. End Sub

6 25565
TheSmileyCoder
2,321 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. tbl_Documents
  2.   Key_Document, Autonumber
  3.   tx_DocTitle, Text
  4.   tx_DocFullPath, Text
I can allready say that once you have the correct path, its a simple matter of:
Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 9 '10 #2
On the form
ComboBox: CompanyName
Controlsource value: CompanyName
Row Source value: SELECT CompanyInfo.FileID, CompanyInfo.CompanyName FROM CompanyInfo ORDER BY CompanyInfo.CompanyName;

On the table:
Autonumber: FileID
Text Column: CompanyName
Hyperlink Column: InfoPDF
Sep 9 '10 #3
TheSmileyCoder
2,321 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_GoTo_Click()
  2.  
  3. End Sub
Modify this to:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_GoTo_Click()
  2.   dim strPath as string
  3.   'Error check
  4.   If isNull(Me.cmb_CompanyName) Then
  5.     Msgbox "No document selected",vbokonly
  6.     Exit Sub
  7.   End If
  8.  
  9.   'Get path
  10.   strPath=Dlookup("InfoPDF","tbl_Doc","FileID=" & Me.cmb_CompanyName)
  11.   application.FollowHyperlink strPath
  12. End Sub
Sep 9 '10 #4
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("InfoPDF", "CompanyInfo", "FileID=" & Me.cmb_CompanyName) Then
Application.FollowHyperlink strPath
Else
MsgBox "No supporting document submitted", vbExclamation
End If
Sep 10 '10 #5
TheSmileyCoder
2,321 Expert Mod 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. strPath = nz(DLookup("InfoPDF", "CompanyInfo", "FileID=" & Me.cmb_CompanyName),"")
  2.  
  3. if strPath & ""<>"" Then
  4. Application.FollowHyperlink strPath
  5. Else
  6. MsgBox "No supporting document submitted", vbExclamation
  7. End If 
When posting code, please post full code, and remember to use the code tags around your code [code]...[/code]
Sep 10 '10 #6
Thank you so much!!
Sep 10 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Mariame | last post: by
3 posts views Thread by Agnes | last post: by
4 posts views Thread by Marine | last post: by
2 posts views Thread by =?Utf-8?B?Q2hhcnRz?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.