A Hyperlink button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a field on a form which is a hyperlink. Currently, to insert a
hyperlink the user needs to use the dropdown menu from Insert or click on the
hyperlink icon on the top toolbar. We constantly have new users of this
database who don't know how to insert a hyperlink, unless they are shown.
Therefore, is it possible to add a command button next to this field which
the user can click-on and directs them to the Insert Hyperlink panel?
many thanks
Steven
 
I used the following in the command button's Click event:

Me.txtHyperlink.SetFocus
DoCmd.RunCommand acCmdInsertHyperlink

txtHyperlink is the text box that is bound to the hyperlink field. I have
not been able to find much discussion on this subject, but from my own
experiments it seems that the focus needs to be in a control bound to the
hyperlink field before acCmdInsertHyperlink will work.
 
BruceM thanks
Does this mean I need to have a 3 items on my form to get this to work -
text box (txtHyperlink) bound to the hyperlink field (Web); the command
button with the Click event code below; and the original hyperlink field
(Web)? This gets confusing. I have tried to hide the txtHyperlink text box,
but the command wont work. The only way is to hide the field "Web" and then
the textbox becomes the "field" on the form. Is this a correct solution or is
there something better? Because I don't want to have 2 fields
(ie.txtHyperlink and Web) displaying the same hyperlink information on the
form.
thanks
SJW
 
When dealing with a hyperlink field in a similar situation I devised my own
solution, since I could not find any past posting that related specifically
to my needs, nor could I elicit an answer to my question in this forum. My
"solution" was to make txtHyperlink quite small, make it flat, set its back
and fore colors to the same color as the Detail back color (assuming
txtHyperlink is in the form's Detail section), and tuck it away in an
inconspicuous corner. If there is a better way I would like to know about
it, but this is what I was able to contrive.
I'm a little confused when you refer to the field (Web) being on the form.
If it is on the form it needs to be in a control such as a text box. In
design view, when you view the "field's" properties, is it identified as a
text box or what? If there is already a text box or something bound to the
Web field you could just apply what I have already written to that control's
name instead of to txtHyperlink. You do not need a text box named
txtHyperlink. That was intended to be a generic name for whatever you
already have.
I hope this clarifies it.
 
Bruce
Again Thanks.
This works OK for me. the only problem is when you click the command button,
the insert hyperlink frame appears. If the user chooses to cancels this op
without inserting a hyperlink, then a Runtime 2501 error occurs.
The field 'Web' is where the hyperlink for the website is held on the form
for users to click on and be directed to the relevant site.
Have taken your advice at latter part of below and works fine.
I certainly don't know of any other way of doing this.
cheers
SJW
 
You can trap the error. I am calling the command button cmdHyperlink.

Private Sub cmdHyperlink_Click()

On Error GoTo ProcErr

Me.txtHyperlink.SetFocus
DoCmd.RunCommand acCmdInsertHyperlink

ProcExit:
Exit Sub

ProcErr:
If Err.Number = 2501 Then
GoTo ProcExit
Else
msgbox "Error #" & Err.Number & ", " & Err.Description & " -
cmdHyperlink_Click"
Resume ProcExit
End If

End Sub

I use some variant of this code for all VBA error handling. If I do not
need to trap an error I use just the following two lines between ProcErr and
End Sub:
msgbox "Error #" & Err.Number & ", " & Err.Description & " -
cmdHyperlink_Click"
Resume ProcExit

Note that ProcErr: and ProcExit: need to be at the left edge of the code
window, and need the colons. I like to use just the generic terms ProcErr:
and ProcExit:, but you could use Err_cmdHyperlink_Click: and
Exit_cmdHyperlink_Click:, or whatever you choose. The message box words " -
cmdHyperlink_Click" are to identify the source of the error (very helpful
during development). You can customize the message box to suit your
preferences.
 
Bruce
I have tried to follow your advice but am not successful. I am using the
following code for Command93 on-click:

Private Sub Command93_Click()
Me.txtHyperlink.SetFocus
DoCmd.RunCommand acCmdInsertHyperlink


On Error GoTo ProcErr

Me.txtHyperlink.SetFocus
DoCmd.RunCommand acCmdInsertHyperlink

ProcExit:
Exit Sub

ProcErr:
If Err.Number = 2501 Then
GoTo ProcExit
Else
MsgBox "Error #" & Err.Number & ", " & Err.Description & " - "
Command93_Click()"
Resume ProcExit
End If

End Sub

I keep getting a syntax error on the 4th last line of the code
(Command93_Click()")
By the way, my code does differ slightly from yours : " and () were added
automatically in this line of code.
thanks
SJW
 
If you had entered my code exactly as I provided it, I think it would have
worked. I wonder why you didn't try that.
Everything between a pair of double quotes is treated as literal text. You
can put anything you want between quote marks, except that when you want
quote marks to show up in your message box it can get a bit more complex.
When the code window added the quote mark after the hyphen, it added just
the hypen to the message box text. It didn't know what to do with the rest
of that line of code, since it was not a recognizable command. The
automatic feature can be very handy, but it doesn't always guess correctly.
..
Another, slightly simpler, way of entering the ProcErr: code would be:

If Err.Number = 2501 Then
GoTo ProcExit
End If
MsgBox "Error #" & Err.Number & ", " & Err.Description & " -
Command93_Click()"
Resume ProcExit

This eliminates the need for Else. If Error 2501 occurs, Exit Sub occurs;
otherwise, the rest of the code runs. Try putting an apostrophe in front of
the first three lines of the code above (the line is ignored when you do
this) so that you can see the message box format when Error 2501 occurs.

By the way if you want to wrap the text in a code window, add a space
followed by an underscore, then press the Enter key:

MsgBox "Error #" & Err.Number & ", " & Err.Description & _
" - Command93_Click()"

Note that you can wrap immediately before or immediately after an ampersand,
not in the middle of a command.

As an aside, you would be doing yourself a favor in the long run if you used
meaningful names for your controls such as command buttons.
 
Bruce
I tried many times to enter the code exactly as you provided ie. copy/paste.
But the automatic feature in the code window continually added quote marks
after the hyphen in the MsgeBox error line of code. I do not know how to
'turn off' this automatic feature, so everytime I ran the code there were
syntax errors. However, the final MsgeBox code you provided at the end of
your advice re. wrapping text in the code window works for me. With the
underscore and space, the automatic feature did not add quotation marks
again.
I accept your advice re. naming command buttons. I usually do name tables,
forms, command buttons etc. with more meaningful names, but on this occasion
I forgot. Many thanks for your help and patience.
SJW
 
You should be able to go back and edit the line. You are not limited to
what the automatic feature adds. I am using Access 2000. If you are using
a different version, maybe there is a bug. If so, you could try starting
with just the quotes, then inserting the text and hyphen between them. You
could also use some other punctuation, or none at all, as you prefer. The
point is just to identify the source of the error.
Here's a trick that can come in handy. You can use the code for the ASCII
character in place of the character itself. Your message box code could be:
MsgBox "Error #" & Err.Number & ", " & Err.Description & " " & chr(45) & "
Command93_Click()"
This technique can come in especially handy when you want quote marks within
a text string. Chr(34) is the code for a double quote.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top