Please Help?! Error handling.

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

Guest

I would like a message to appear on the click of a command button if the
associated record does not have a hyperlink associated with it. How would I
go about this?
Cheers
 
Hi, Ashley.
I would like a message to appear on the click of a command button if the
associated record does not have a hyperlink associated with it.

Try:

Private Sub ChkHypLinkBtn_Click()

On Error GoTo ErrHandler

If (Nz(Me!Work_Instruction.Value, "") = "") Then
MsgBox "This record has no hyperlink.", vbInformation + vbOKOnly, _
"No Hyperlink"
End If

Exit Sub

ErrHandler:

MsgBox "Error in ChkHypLinkBtn_Click( ) in " & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear

End Sub

.... where Work_Instruction is the name of the text box bound to a Hyperlink
field for the current record and ChkHypLinkBtn is the name of the button.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
On another more complex note is it possible to have a message box come up
saying "This Job has no Work Instruction. Would You Like to add one?" - Yes
or No
and then do nothing if no is selected and open a file if yes is selected.
Sorry if I'm asking too much!!
Your help has been awesome so far.
 
Hi, Ashley.
On another more complex note is it possible to have a message box come up
saying "This Job has no Work Instruction. Would You Like to add one?" - Yes
or No
and then do nothing if no is selected and open a file if yes is selected.

Yes. But with a caveat. Your Hyperlink is on an unattached label, and in
order to edit that Hyperlink, one must set focus to the control that stores
the Hyperlink. Unfortunately, one cannot set focus to an unattached label.
The solution of course is to use a text box. Unfortunately, only a bound
text box may hold a Hyperlink. Therefore, your table must store the
Hyperlink if you want this to work.

Here's the code (watch out for word wrap):

Private Sub ChkHypLinkBtn_Click()

On Error GoTo ErrHandler

Dim sDefaultDir As String
Dim sOrigText As String
Dim ans As Integer

If (Nz(Me!Work_Instruction.Value, "") = "") Then
ans = MsgBox("This Job has no Work Instruction." & vbCrLf & _
"Would You Like to add one?", vbInformation + vbYesNo, _
"No Work Instruction")

If (ans = vbYes) Then
sDefaultDir = "C:\Work" ' Default directory for the "Edit
Hyperlink" dialog box.

Me!txtHypLink.SetFocus
sOrigText = Me!txtHypLink.Text ' Save value in case
user doesn't edit the hyperlink.

Me!txtHypLink.Text = sDefaultDir
RunCommand acCmdEditHyperlink ' Call the "Insert/Edit
Hyperlink" dialog window.


'---------------------------------------------------------------------
' Determine whether user didn't edit the hyperlink.

'---------------------------------------------------------------------

If (Me!txtHypLink.Text = sDefaultDir) Then
Me!txtHypLink.Text = sOrigText ' Replace orig.
hyperlink string value.
End If
End If
End If

Exit Sub

ErrHandler:

If (Err.Number = 2501) Then ' User cancelled action.
Resume Next
Else
MsgBox "Error in ChkHypLinkBtn_Click( ) in " & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
End If

End Sub

.... where "C:\Work" is the default directory and txtHypLink is the name of
the text box bound to a Hyperlink field.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Not quite sure with this one. Code seems to work great, my Hyperlink is
stored in a table and is bound to a text box in the form (but it isn't
visible).
I get following error when clicking yes:

Error in ViewWI_Click() in
Enter Job Number (Subform) form.

Error #2110
Work Instruction 2005 cannot move the focus to the control Work Instruction

Excuse me if im not up to scratch, your doing an excellent job!
Cheers
 
Hi, Ashley.
Code seems to work great, my Hyperlink is
stored in a table and is bound to a text box in the form

Then you've met one requirement for editing the Hyperlink...
(but it isn't
visible).

.... but didn't meet the other requirement. Since one cannot set focus to a
control that isn't visible, the code fails. Either set the control to
visible temporarily just before this subroutine sets focus to the control,
and then make it invisible as soon as a value is assigned to the Hyperlink
field -- or find a different way to edit the Hyperlink to a file.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.
 
Back
Top