Search code examples
vbams-accessms-access-2016

MS Access HyperlinkAddress carries over the last used URL


I have a form where the user enters a company's website (URL) into a text box. A command button is next to the text box so that the URL entered can be opened in a browser. It works perfectly when there is a URL in the text box. However, when the text box is blank, the MsgBox to "try again" appears (as it should), but instead of stopping the subroutine after the user clicks OK, it opens the most recently opened URL. How do I get the subroutine to simply stop when the MsgBox OK button is clicked? I am guessing the URL is being stored somewhere. Where? And how can I clear it?

Private Sub cmd_websitelink_Click()

'***********************************************
'** Opens browser for Website Link
'***********************************************

DoCmd.SetWarnings False

Dim sitestring As String

sitestring = Me.Website & vbNullString

If Len(sitestring) = 0 Then GoTo ErrorHandler

On Error GoTo ErrorHandler
    
    Dim ctl As CommandButton
    Set ctl = Me!cmd_websitelink
    With ctl
        .HyperlinkAddress = sitestring
    End With

Exit Sub

GoTo Finish

ErrorHandler:

MsgBox "Enter a valid URL in the website box and try again."

Finish:

End Sub 

Solution

  • You set the .HyperlinkAddress property of the button in your procedure.

    This property stays set until you close the form or your overwrite the property.

    So you need to add this to the error case:

    Me!cmd_websitelink.HyperlinkAddress = ""
    

    Instead of all this, you could simply open the hyperlink in the Click event, see here:
    How to open a URL from MS Access with parameters